1

I'm using Drupal 8 and creating a feed entity like this:

//...    
$feed = \Drupal::entityTypeManager()->getStorage('feeds_feed')->create(array(
            'type' => 'my_feed',
            'uuid' => \Drupal::service('uuid')->generate(),
            'title' => 'RSS feed',
            'uid' => $uid,
            'status' => 1,
            'created' => time(),
            'source' => $form_state->getValue('rss_feed_url'),
          ));
$feed->save();
//...  

That works fine! But how can I execute the import after saving the new feed entity? I've tried the following:

// Batch starts and returns "This webpage has a redirect loop" 
$feed->startBatchImport();

// Returns an error: "Call to undefined method Drupal\feeds\FeedImportHandler::startCronImport()"
$feed->startCronImport();

How can I import the feed items after creating the feed entity?

  • There is some code in the Queue worker plugins in the feeds module that might help you find an answer. The feeds module is still in development so I am not sure how relevant an answer will be 6 months or a year from now. – mradcliffe Jun 16 '16 at 13:45
1

This is working for me. Note: You will need to know the numerical $id of your Feed from the D8 DB table feeds_feed. In my case it is 1 so $id = 1.

use Drupal\feeds\Entity\Feed;

function mymodule_cron() {

  /**
   * Runs single feeds on cron
   */

  $feed = Feed::load($id);
  if (!$feed->isLocked()) {
    $queue = \Drupal::queue('feeds_feed_import:' . $feed->bundle());
    if ($queue->createItem($feed)) {
      // Add timestamp to avoid queueing item more than once.
      $feed->setQueuedTime(REQUEST_TIME);
      $feed->save();
    }
  }
}
| improve this answer | |
  • Hi Brooke - Do you have any thoughts on how to programmatically fetch a feed without running cron? Something similar to the above but fetched on a page load or some such? I'm trying to figure out a way to fetch two feeds consecutively, but the first has to be fetched prior to second. Any thoughts are appreciated. – user1055810 Apr 22 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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