You are here

function nodereferrer_field in NodeReferrer 6

Same name and namespace in other branches
  1. 5 nodereferrer.module \nodereferrer_field()

Implementation of hook_field().

File

./nodereferrer.module, line 131
Defines a field type for backlinking referencing nodes.

Code

function nodereferrer_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'load':
      $types = isset($field['referrer_types']) ? array_values(array_filter($field['referrer_types'])) : array();
      $fields = isset($field['referrer_fields']) ? array_values(array_filter($field['referrer_fields'])) : array();
      $order = $field['referrer_order'];
      $translations = isset($field['referrer_translations']) ? $field['referrer_translations'] : 0;
      $values = nodereferrer_referrers($node->nid, $fields, $types, $translations, $order);

      // Pass referring node objects into CCK content_load() cache. 24/08/2006 sun
      $items = array();
      foreach ($values as $nid => $rnode) {
        $items[] = $rnode;
      }
      if (count($items) == 0) {
        return array(
          $field['field_name'] => array(),
        );
      }
      $output = array(
        'items' => $items,
        'limit' => empty($field['referrer_nodes_per_page']) ? 0 : $field['referrer_nodes_per_page'],
        'element' => empty($field['referrer_pager_element']) ? 0 : $field['referrer_pager_element'],
        'pager' => '',
      );
      return array(
        $field['field_name'] => array(
          $output,
        ),
      );
    case 'delete':
    case 'update':

      // clear cache on nodes that refer to me.
      $types = array_values(array_filter($field['referrer_types']));
      $fields = array_values(array_filter($field['referrer_fields']));

      // clear any modules referring to me as my title or other data may change.
      // and nodereference doesn't clear the cache yet.
      foreach (nodereferrer_referrers($node->nid, $fields, $types, false) as $delta => $item) {
        $cid = 'content:' . $item['nid'] . ':' . $item['vid'];
        cache_clear_all($cid, 'cache_page');
      }
      return;
  }
}