0

Is it possible to run one feed importer on several sources?

Currently my code is as follow:

My importer module file is

function myModule_import_feeds_plugins() {
  $info = array();
  $info['xml_parser'] = array(
    'name' => 'XML feed parser',
    'description' => 'Imports XML feed.',
    'help' => '',
    'handler' => array(
      'parent' => 'FeedsParser',
      'class' => 'myXmlParser',
      'file' => 'my_xml_parser.inc',
      'path' => drupal_get_path('module', 'myModule_import') . '/plugins/feeds',
    ),
  );

My parser is:

class myXmlParser extends FeedsParser {

  /**
   * Implements FeedsParser::parse().
   */
  public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
    $feed = new SimpleXMLElement($fetcher_result->getRaw());
    $source_feed = $source->config['FeedsHTTPFetcher'];
    $user_tags = isset($source_feed['user_tags'][0]) ? explode(',', $source_feed['user_tags']) : array('tag1', 'tag2');

    $result = new FeedsParserResult();
    $result->title = 'feed importer';
    $result->items = array();
    #$fetcher_result->
    foreach ($feed->channel->item as $item) {
      // Skip if title doesn't
      if ((string) $item->title) {
        $result->items[] = array(
          'title' => (string) $item->title,
          'callout' => '<p>'.(string) $item->description.'</p>', // wrap light_html in P tags
          'pubdate' => (string) $item->pubDate, // _parser_common_syndication_parse_date()
          'url' => (string) $item->link,
          'image_url' => (string) $item->thumb, // http://placekitten.com/g/200/300
          'image_alt' => (string) $item->alt,
          'published' => 1,
          'tags' => $user_tags,
        );
      }
    }

    return $result;
  }

feed screen looks like this enter image description here

I would like to add an additional field for an optional URL. This way the user can include several sources and import from different places at the same time.

I could not find a way to do so. Ideas?

  • No ideas? suggestions? anything? – Quantico Feb 23 '17 at 13:46
1

Yes, there is. The supported way of importing multiple sources for one importer is to attach the importer to a content type (which can be configured under the "Basic settings"). Attaching an importer to a content type means that a node must be created to import content. Since you can create multiple nodes, you can import multiple sources with one importer configuration.

Be sure though that if you are importing nodes (using the Node processor) that the nodes you create are of a different type than the node type that you attach an importer to. It is wise to create a new content type that is specifically only used for your importer.

See also my answer at https://www.drupal.org/node/2854548#comment-11948926 for more information about why you should not attach an importer to the same content type as the one you select on the processor settings.

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.