You are here

function feeds_node_validate in Feeds 7.2

Same name and namespace in other branches
  1. 8.2 feeds.module \feeds_node_validate()
  2. 7 feeds.module \feeds_node_validate()

Implements hook_node_validate().

Related topics

File

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

Code

function feeds_node_validate($node, $form, &$form_state) {
  if (!($importer_id = feeds_get_importer_id($node->type))) {
    return;
  }

  // Keep a copy of the title for subsequent node creation stages.
  // @todo: revisit whether $node still looses all of its properties
  // between validate and insert stage.
  $last_title =& drupal_static('feeds_node_last_title');
  $last_feeds =& drupal_static('feeds_node_last_feeds');

  // 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.
  // configFormValidate may modify $last_feed, smuggle it to update/insert stage
  // through a static variable.
  $last_feeds = $node->feeds;
  $source
    ->configFormValidate($last_feeds);

  // Check if title form element is hidden.
  $title_hidden = isset($form['title']['#access']) && !$form['title']['#access'];

  // If the node title is empty and the title form element wasn't hidden, try to
  // retrieve the title from the feed.
  if (isset($node->title) && trim($node->title) == '' && !$title_hidden) {
    try {
      $source
        ->addConfig($last_feeds);
      if (!($last_title = $source
        ->preview()->title)) {
        throw new Exception();
      }
    } catch (Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
      form_set_error('title', t('Could not retrieve title from feed.'));
    }
  }
}