You are here

function nodereferrer_field_formatter_view in NodeReferrer 7

Implements hook_field_formatter_view().

File

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

Code

function nodereferrer_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $result = array();
  switch ($display['type']) {
    case 'nodereferrer_default':
    case 'nodereferrer_plain':
      foreach ($items as $delta => $item) {

        // Sometimes the values in $item['items'] is not an array,
        // in situations where there are no referring nodes.
        // array_keys() will throw an error in that situation,
        // so to avoid unnecessary messages in watchdog - we check.
        $nodes = array();
        if (is_array($item['items'])) {
          $nodes = node_load_multiple(array_keys($item['items']));
        }
        $rendered_nodes = array();
        foreach ($nodes as $node) {
          if ($display['type'] == 'nodereferrer_default') {
            $uri = entity_uri('node', $node);
            $rendered_nodes[$node->nid] = l($node->title, $uri['path'], $uri['options']);
          }
          else {
            $rendered_nodes[$node->nid] = check_plain($node->title);
          }

          /*if (!$node->status) {
              $rendered_nodes[$node->nid]['#prefix'] = '<span class="node-unpublished">';
              $rendered_nodes[$node->nid]['#suffix'] = '</span>';
            }*/
        }
        $result[$delta] = array(
          '#theme' => 'item_list',
          '#items' => $rendered_nodes,
        );
      }
      break;
    case 'nodereferrer_node':
      $view_mode = $display['settings']['nodereferrer_view_mode'];
      foreach ($items as $delta => $item) {
        $nodes = node_load_multiple(array_keys($item['items']));
        $result[$delta] = node_view_multiple($nodes, $view_mode);
      }
      break;
    case 'nodereferrer_count':

      // Assemble the render array.
      foreach ($items as $delta => $item) {
        $result[$delta] = array(
          '#markup' => count($item['items']),
          '#prefix' => $instance['settings']['prefix'] . ' ',
          '#suffix' => ' ' . $instance['settings']['suffix'],
        );
      }
  }
  return $result;
}