feeds.tokens.inc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for feed-related data.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function feeds_token_info() {
  10. // @todo This token could be for any entity type.
  11. $info['tokens']['node']['feed-source'] = array(
  12. 'name' => t('Feed source'),
  13. 'description' => t('The node the feed item was sourced from.'),
  14. 'type' => 'node',
  15. );
  16. return $info;
  17. }
  18. /**
  19. * Implements hook_tokens().
  20. */
  21. function feeds_tokens($type, $tokens, array $data, array $options) {
  22. $replacements = array();
  23. if ($type == 'node' && !empty($data['node']) && !empty($data['node']->nid)) {
  24. $sanitize = !empty($options['sanitize']);
  25. $feed_nid = feeds_get_feed_nid($data['node']->nid, 'node');
  26. if ($feed_nid && $feed_source = node_load($feed_nid)) {
  27. foreach ($tokens as $name => $original) {
  28. switch ($name) {
  29. case 'feed-source':
  30. $replacements[$original] = $sanitize ? check_plain($feed_source->title) : $feed_source->title;
  31. break;
  32. }
  33. }
  34. // Chained node token relationships.
  35. if ($feed_source_tokens = token_find_with_prefix($tokens, 'feed-source')) {
  36. $replacements += token_generate('node', $feed_source_tokens, array('node' => $feed_source), $options);
  37. }
  38. }
  39. }
  40. return $replacements;
  41. }