/**
 * Implementation of hook_token_info().
 */
/**
 * Implements hook_token_info().
 */
function feeds_token_info() {
  $info = array();

  // Node tokens.
  $info['tokens']['node']['feed_source'] = array(
    'name' => t('Feeds: Source node'),
    'description' => t("The node of the feeds source."),
    'type' => 'node',
  );

  return $info;
}

/**
 * Implements hook_tokens().
 */
function feeds_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  // Node tokens.
  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];

    // Chained token relationships.
    if (($source_tokens = token_find_with_prefix($tokens, 'feed_source')) && ($feed_item = feeds_item_info_load('node', $node->nid)) && ($source_node = node_load($feed_item->feed_nid))) {
      $replacements += token_generate('node', $source_tokens, array('node' => $source_node), $options);
    }
  }

  return $replacements;
}

Comments

syp’s picture

Tested and works perfect.
The feature I was searching for and was there ready to use! :)
Very usefull in conjunction with Pathauto and Node Hierarchy modules.
This piece of code should be include in development release!

Ayesh’s picture

+1!
Subscribing.

Dave Reid’s picture

Status: Needs review » Fixed

Fixed this up and committed the [node:feed-source] token in http://drupalcode.org/project/feeds.git/commit/51600e9

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

sylvanos’s picture

I tried it and i'm unable to make it work. I have node A importing data and creating new nodes B (through node processor). I'm using Automatic NodeTitles to retrieve the title of node A to put it in node B, then I add the date at the end of the title of node B. The date token shows up but not the "feed-source"

Now this could be an issue with Automatic NodeTitles or with Token but I'm able to retrieve many tokens like date or fields but no luck with anything that as to do with [node:feed-source:xxx]

Feeds 7.x-2.0-alpha3
Automatic Nodetitles 7.x-1.0
Token 7.x-1.0-beta2
Drupal 7.2

Dave Reid’s picture

The tokens are not available in Feeds 7.x-2.0-alpha3. They're available in 7.x-2.x-dev only right now.

sylvanos’s picture

Thanks, reinstalled the dev version and cleared the cache: tokens show up. I observed a little bug so I thought I'd document it. Here's how to reproduce it:

1. node A has a feed (JSON)
2. when I import a feed from node A, it creates one feed item (node B) using node processor
3. node B gets its title form the title of node A plus a date token
4. After import, the title of node B looks like this : [node:feed-source:title] - Stats Jun 23 2011 - 11:45
5. When I open node B and save it, then the token is rendered

So the date token gets rendered on import but not the feed-source.

AndrewKWilson’s picture

@sylvanos I've also noted that the feed-source token does not "render" on import. You must edit and save the new feed item before it will render.

mareks’s picture

I am still having this problem with the latest version of Feeds.