I've made a module which will parse a few RSS feeds, and automatically creates new content. But I've got an issue creating images.

Since D7, cron are run anonymously, so I think my problem comes from here.
Is there a way to allow anonymous user to create images?

Here's the way I'm doing it. It works if I call FeedController::import via a route, and connected ; but not if I call it through Drupal's cron task.


use Drupal\rssfeed\Controller\FeedController;

function rssfeed_cron() {


public function import()
  $params = array();
  // some stuff to fill $params array

  $hash = ...;
  $src  = ...;
  $alt  = ...;

  $data = file_get_contents($src);
  $size = getimagesize($src);
  $extension = image_type_to_extension($size[2]);
  $file = file_save_data(
    sprintf('public://rss/%s.%s', $hash, $extension), 

  if ($file) {
    $params['field_image_main'] = array(
      'target_id' => $file->id(),
      'alt' => $alt

  $node = Node::create($params);

1 Answer 1


As a possible (but possibly not the best) solution it might be the 'account_switcher' service could help. It would elevate permissions for your cron job.

use Drupal\Core\Session\UserSession;

// call the account switcher service
$accountSwitcher = \Drupal::service('account_switcher');
// switch to the admin user
$accountSwitcher->switchTo(new UserSession(['uid' => 1]));

// run your code here

// switch back to old session

zessx, please share your solution, if you've found one.

See original Q&A here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.