You are here

function nodereferrer_field in NodeReferrer 5

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

Implementation of hook_field().

File

./nodereferrer.module, line 88
Defines a field type for backlinking referencing nodes. @todo -clear content cache with nodeapi. -query nids for access on load/view..

Code

function nodereferrer_field($op, &$node, $field, &$items, $teaser, $page) {
  static $element = 3;
  switch ($op) {
    case 'view':
      $pager = '';
      if ($field['referrer_nodes_per_page']) {

        // Unique Element for the pager.
        $element++;
        $limit = $field['referrer_nodes_per_page'];

        // Fake the values set by pager query...
        global $pager_page_array, $pager_total, $pager_total_items;
        $page = isset($_GET['page']) ? $_GET['page'] : '';

        // Convert comma-separated $page to an array, used by other functions.
        $pager_page_array = explode(',', $page);

        // We calculate the total of pages as ceil(items / limit).
        $pager_total_items[$element] = count($items);
        $pager_total[$element] = ceil($pager_total_items[$element] / $limit);
        $pager_page_array[$element] = max(0, min((int) $pager_page_array[$element], (int) $pager_total[$element] - 1));

        // only display the select elements.
        if (is_array($items)) {
          $items = array_slice($items, $pager_page_array[$element] * $limit, $limit);
        }
        $pager = theme('pager', array(), $limit, $element);
      }

      // get the context
      $context = $teaser ? 'teaser' : 'full';

      // get the formatter
      $formatter = isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default';
      foreach ($items as $delta => $item) {
        $access_node = node_load($item['nid']);
        if ($access_node && node_access('view', $access_node)) {
          $items[$delta]['view'] = content_format($field, $item, $formatter);
        }
      }
      return theme('field', $node, $field, $items, $teaser, $page) . $pager;
    case 'load':
      $types = array_values(array_filter($field['referrer_types']));
      $fields = array_values(array_filter($field['referrer_fields']));
      $order = $field['referrer_order'];
      $values = nodereferrer_referrers($node->nid, $fields, $types, $order);

      //$node_field = array(); -- do I really need to initialize... do I get node_field

      // even though this module doesn't have any db fields.
      $items = array();

      // Pass referring node objects into CCK content_load() cache. 24/08/2006 sun
      foreach ($values as $nid => $rnode) {
        $items[] = $rnode;
      }
      return array(
        $field['field_name'] => $items,
      );
    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) as $delta => $item) {
        $cid = 'content:' . $item['nid'] . ':' . $item['vid'];
        cache_clear_all($cid, 'cache_page');
      }
      return;
  }
}