You are here

function feeds_nodeapi in Feeds 6

Implementation of hook_nodeapi().

@todo For Drupal 7, revisit static cache based shuttling of values between 'validate' and 'update'/'insert'.

Related topics

File

./feeds.module, line 329
Feeds - basic API functions and hook implementations.

Code

function feeds_nodeapi(&$node, $op, $form) {

  // $node looses any changes after 'validate' stage (see node_form_validate()).
  // Keep a copy of title and feeds array between 'validate' and subsequent
  // stages. This allows for automatically populating the title of the node form
  // and modifying the $form['feeds'] array on node validation just like on the
  // standalone form.
  static $last_title;
  static $node_feeds;

  // Break out node processor related nodeapi functionality.
  _feeds_nodeapi_node_processor($node, $op);
  if (!in_array($op, array(
    'validate',
    'presave',
    'insert',
    'update',
    'delete',
  ))) {
    return;
  }
  if ($importer_id = feeds_get_importer_id($node->type)) {
    switch ($op) {
      case 'validate':

        // On validation stage we are working with a FeedsSource object that is
        // not tied to a nid - when creating a new node there is no
        // $node->nid at this stage.
        $source = feeds_source($importer_id);

        // Node module magically moved $form['feeds'] to $node->feeds :P
        $node_feeds = $node->feeds;
        $source
          ->configFormValidate($node_feeds);

        // If node title is empty, try to retrieve title from feed.
        if (trim($node->title) == '') {
          try {
            $source
              ->addConfig($node_feeds);
            if (!($last_title = $source
              ->preview()
              ->getTitle())) {
              throw new Exception();
            }
          } catch (Exception $e) {
            drupal_set_message($e
              ->getMessage(), 'error');
            form_set_error('title', t('Could not retrieve title from feed.'));
          }
        }
        break;
      case 'presave':
        if (!empty($last_title)) {
          $node->title = $last_title;
        }
        $last_title = NULL;
        break;
      case 'insert':
      case 'update':

        // A node may not have been validated, make sure $node_feeds is present.
        if (empty($node_feeds)) {

          // If $node->feeds is empty here, nodes are being programatically
          // created in some fashion without Feeds stuff being added.
          if (empty($node->feeds)) {
            return;
          }
          $node_feeds = $node->feeds;
        }

        // Add configuration to feed source and save.
        $source = feeds_source($importer_id, $node->nid);
        $source
          ->addConfig($node_feeds);
        $source
          ->save();

        // Refresh feed if import on create is selected and suppress_import is
        // not set.
        if ($op == 'insert' && feeds_importer($importer_id)->config['import_on_create'] && !isset($node_feeds['suppress_import'])) {
          feeds_batch_set(t('Importing'), 'import', $importer_id, $node->nid);
        }

        // Add source to schedule, make sure importer is scheduled, too.
        if ($op == 'insert') {
          $source
            ->schedule();
          $source->importer
            ->schedule();
        }
        $node_feeds = NULL;
        break;
      case 'delete':
        $source = feeds_source($importer_id, $node->nid);
        if (!empty($source->importer->processor->config['delete_with_source'])) {
          feeds_batch_set(t('Deleting'), 'clear', $importer_id, $node->nid);
        }

        // Remove attached source.
        $source
          ->delete();
        break;
    }
  }
}