2

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?

1
  • 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
    Commented Jun 16, 2016 at 13:45

3 Answers 3

2

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();
    }
  }
}
2
  • 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. Commented Apr 22, 2020 at 20:54
  • Hm, I suppose you should be able to hook into Drupal using something like an Event Subscriber but in this case, it might make sense to simply use Drush - in fact, I believe this should be possible already - following this update and patch: drupal.org/project/feeds/issues/3060930 So, it really depends on your use case, but I'd likely lean toward Drush. But if there's something else that should trigger the feed, I'd hook into Drupal using an Event Subcriber - more on that here: drupal.org/docs/creating-custom-modules/… Commented Dec 17, 2020 at 20:59
1

This should work:

$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();
$feed->import();
0

Feeds also has drush support, and drush can be scripted quite easily:

  feeds:list-feeds Display all feeds using a drush command.  
  feeds:enable     Enable a feed specified by its id.        
  feeds:disable    Disable a feed specified by its id.       
  feeds:import     Import a feed specified by its id.        
  feeds:import-all Import all feeds.                         
  feeds:lock       Lock a feed specified by its id.          
  feeds:unlock     Unlock a feed specified by its id.

It's as simple as drush feeds 123, where 123 is the entity ID of the feed.

In my case, I find it easier to import feeds by machine name, because machine names will always be the same even across different sites that import the same config, whereas entity IDs can vary among sites that import the same config.

I wrote a patch to add machine name support to feeds drush import.

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.