Drupal Feeds

   Andriy Podanenko aka @podarok
    http://drupal.org/user/116002
    Kyiv Drupal Cafe`


            feeds developer keynotes


     08.09.2011         Kyiv Drupal Cafe`
POI

   FeedsSource extends FeedsConfigurable
    FeedsSource::import

   Plugins (FeedsFetcher, FeedsParser,
    FeedsProcessor)
   Hooks
   Mappings
    hook_feeds_processor_targets_alter()
    hook_feeds_parser_sources_alter()



       08.09.2011               Kyiv Drupal Cafe`
Manual process
startImport()
 $config = $this->importer->getConfig();
  if ($config['process_in_background']) {
    $this->startBackgroundJob('import');
  }
  else {
    $this->startBatchAPIJob(t('Importing'), 'import'); //method
  }
}
 function startBatchAPIJob(){
 $batch = array(
      'title' => $title,
      'operations' => array(
        array('feeds_batch', array($method, $this->id, $this->feed_nid)),
      ),
    );
    batch_set($batch);
  }

     08.09.2011                  Kyiv Drupal Cafe`
Manual process
    feeds_batch()

   function feeds_batch($method, $importer_id, $feed_nid = 0, &$context) {
  $context['finished'] = FEEDS_BATCH_COMPLETE;
  try {
 // main work doing here
    $context['finished'] = feeds_source($importer_id, $feed_nid)->$method();
  }
  catch (Exception $e) {
    drupal_set_message($e->getMessage(), 'error');
  }
}




        08.09.2011               Kyiv Drupal Cafe`
FeedsSource::import
$this->acquireLock();
 try {
     // Fetch.
     if (empty($this->fetcher_result) || FEEDS_BATCH_COMPLETE == $this-
>progressParsing()) {
       $this->fetcher_result = $this->importer->fetcher->fetch($this);
//….
     }

   // Parse.
   $parser_result = $this->importer->parser->parse($this, $this->fetcher_result);
   module_invoke_all('feeds_after_parse', $this, $parser_result);

   // Process.
   $this->importer->processor->process($this, $parser_result);
  }
  catch (Exception $e) {
// ….
  }
  $this->releaseLock();

     08.09.2011                     Kyiv Drupal Cafe`
Feeds hooks
function feeds_hook_info() {
  $hooks = array(
    'feeds_after_parse',
    'feeds_after_import',
    'feeds_after_clear',
    'feeds_processor_targets_alter',
    'feeds_parser_sources_alter',
  );
 08.09.2011    Kyiv Drupal Cafe`
Feeds plugins hook_feeds_plugins()
 function ofeeds_feeds_plugins() {
  $info = array();
  $info['OpenERP_Fetcher'] = array(
    'name' => 'OpenERP Fetcher',
    'description' => 'Fetches OpenERP objects from Models.',
    'help' => 'Fetches OpenERP objects from Models. Additional help will be
in a feature',
    'handler' => array(
      'parent' => 'FeedsFetcher',
      'class' => 'OpenERP_Fetcher',
      'file' => 'OpenERP_Fetcher.inc',
      'path' => drupal_get_path('module', 'ofeeds') . '/fetchers',
// Feeds will look for OpenERP_Fetcher.inc in the ofeeds/fetchers directory.
      ),
    );

    return $info;
}
           08.09.2011                 Kyiv Drupal Cafe`
Feeds plugins
●class FeedsPlugin extends FeedsConfigurable implements
FeedsSourceInterface
 methods: save, sourceForm, configForm, loadMappers...

FeedsFetcher extends FeedsPlugin
●

methods: clear, fetch...

FeedsParser extends FeedsPlugin
●

methods: parse, clear...

●FeedsProcessor extends FeedsPlugin
 methods: process, entityType, newEntity, entityLoad,
entitySave, entityDeleteMultiple


     08.09.2011           Kyiv Drupal Cafe`
Feeds plugins forms
● configForm
 // forms displayed at feeds instance settings tab (admin level)
 FE: OpenERP_Fetcher::configForm($fs) – select model,
setting for limiting count of fetched data etc.
 Exportable as config array !!! (via import export clone)

// configFormSubmit,configFormValidate

● sourceForm
 // forms displayed at /import/$feeds_instance page and at
content type creation forms (user level)
 FE: FeedsFileFetcher::sourceForm($sc) – upload file form,
form for listing already uploaded files etc.

// sourceFormValidate, sourceFormSubmit
      08.09.2011               Kyiv Drupal Cafe`
Feeds tricks
    function ofeeds_ctools_plugin_api($owner, $api) {
      if ($owner == 'feeds' && $api == 'plugins') {
        return array('version' => 1);
      }
    }

    function ofeeds_enable() {
      //clear the cache to display in Feeds as available plugin.
      cache_clear_all('plugins:feeds:plugins', 'cache');
    }

    // Do not use variable_set as instances config !!!



                                       
Thanks

    Andriy Podanenko @podarok

    podarokua@gmail.com
    http://twitter.com/podarok
    http://facebook.com/podarok
    http://drupal.org/user/116002




                          

Feeds drupal cafe

  • 1.
    Drupal Feeds  Andriy Podanenko aka @podarok http://drupal.org/user/116002 Kyiv Drupal Cafe` feeds developer keynotes 08.09.2011 Kyiv Drupal Cafe`
  • 2.
    POI  FeedsSource extends FeedsConfigurable FeedsSource::import  Plugins (FeedsFetcher, FeedsParser, FeedsProcessor)  Hooks  Mappings hook_feeds_processor_targets_alter() hook_feeds_parser_sources_alter() 08.09.2011 Kyiv Drupal Cafe`
  • 3.
    Manual process startImport() $config= $this->importer->getConfig(); if ($config['process_in_background']) { $this->startBackgroundJob('import'); } else { $this->startBatchAPIJob(t('Importing'), 'import'); //method } } function startBatchAPIJob(){ $batch = array( 'title' => $title, 'operations' => array( array('feeds_batch', array($method, $this->id, $this->feed_nid)), ), ); batch_set($batch); } 08.09.2011 Kyiv Drupal Cafe`
  • 4.
    Manual process feeds_batch() function feeds_batch($method, $importer_id, $feed_nid = 0, &$context) { $context['finished'] = FEEDS_BATCH_COMPLETE; try { // main work doing here $context['finished'] = feeds_source($importer_id, $feed_nid)->$method(); } catch (Exception $e) { drupal_set_message($e->getMessage(), 'error'); } } 08.09.2011 Kyiv Drupal Cafe`
  • 5.
    FeedsSource::import $this->acquireLock(); try { // Fetch. if (empty($this->fetcher_result) || FEEDS_BATCH_COMPLETE == $this- >progressParsing()) { $this->fetcher_result = $this->importer->fetcher->fetch($this); //…. } // Parse. $parser_result = $this->importer->parser->parse($this, $this->fetcher_result); module_invoke_all('feeds_after_parse', $this, $parser_result); // Process. $this->importer->processor->process($this, $parser_result); } catch (Exception $e) { // …. } $this->releaseLock(); 08.09.2011 Kyiv Drupal Cafe`
  • 6.
    Feeds hooks function feeds_hook_info(){ $hooks = array( 'feeds_after_parse', 'feeds_after_import', 'feeds_after_clear', 'feeds_processor_targets_alter', 'feeds_parser_sources_alter', ); 08.09.2011 Kyiv Drupal Cafe`
  • 7.
    Feeds plugins hook_feeds_plugins() function ofeeds_feeds_plugins() { $info = array(); $info['OpenERP_Fetcher'] = array( 'name' => 'OpenERP Fetcher', 'description' => 'Fetches OpenERP objects from Models.', 'help' => 'Fetches OpenERP objects from Models. Additional help will be in a feature', 'handler' => array( 'parent' => 'FeedsFetcher', 'class' => 'OpenERP_Fetcher', 'file' => 'OpenERP_Fetcher.inc', 'path' => drupal_get_path('module', 'ofeeds') . '/fetchers', // Feeds will look for OpenERP_Fetcher.inc in the ofeeds/fetchers directory. ), ); return $info; } 08.09.2011 Kyiv Drupal Cafe`
  • 8.
    Feeds plugins ●class FeedsPluginextends FeedsConfigurable implements FeedsSourceInterface methods: save, sourceForm, configForm, loadMappers... FeedsFetcher extends FeedsPlugin ● methods: clear, fetch... FeedsParser extends FeedsPlugin ● methods: parse, clear... ●FeedsProcessor extends FeedsPlugin methods: process, entityType, newEntity, entityLoad, entitySave, entityDeleteMultiple 08.09.2011 Kyiv Drupal Cafe`
  • 9.
    Feeds plugins forms ●configForm // forms displayed at feeds instance settings tab (admin level) FE: OpenERP_Fetcher::configForm($fs) – select model, setting for limiting count of fetched data etc. Exportable as config array !!! (via import export clone) // configFormSubmit,configFormValidate ● sourceForm // forms displayed at /import/$feeds_instance page and at content type creation forms (user level) FE: FeedsFileFetcher::sourceForm($sc) – upload file form, form for listing already uploaded files etc. // sourceFormValidate, sourceFormSubmit 08.09.2011 Kyiv Drupal Cafe`
  • 10.
    Feeds tricks function ofeeds_ctools_plugin_api($owner, $api) { if ($owner == 'feeds' && $api == 'plugins') { return array('version' => 1); } } function ofeeds_enable() { //clear the cache to display in Feeds as available plugin. cache_clear_all('plugins:feeds:plugins', 'cache'); } // Do not use variable_set as instances config !!!    
  • 11.
    Thanks Andriy Podanenko @podarok podarokua@gmail.com http://twitter.com/podarok http://facebook.com/podarok http://drupal.org/user/116002