You are here

function user_reference_field_formatter_view in References 7.2

Implements hook_field_formatter_view().

File

user_reference/user_reference.module, line 414
Defines a field type for referencing a user from a node.

Code

function user_reference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $result = array();

  // @todo Optimisation: use hook_field_formatter_prepare_view() to load.
  // User names or full user entities in 'multiple' mode.
  switch ($display['type']) {
    case 'user_reference_default':
    case 'user_reference_plain':
      foreach ($items as $delta => $item) {
        if ($item['access']) {
          $user = $item['user'];
          $label = entity_label('user', $user);
          if ($display['type'] == 'user_reference_default') {
            $uri = entity_uri('user', $user);
            $router_item = menu_get_item($uri['path']);
            if ($router_item && $router_item['access']) {
              $result[$delta] = array(
                '#type' => 'link',
                '#title' => $label,
                '#href' => $uri['path'],
                '#options' => $uri['options'],
              );
            }
            else {
              $result[$delta] = array(
                '#markup' => check_plain($label),
              );
            }
          }
          else {
            $result[$delta] = array(
              '#markup' => check_plain($label),
            );
          }
        }
      }
      break;
    case 'user_reference_user':

      // To prevent infinite recursion caused by reference cycles, we store
      // diplayed accounts in a recursion queue.
      $recursion_queue =& drupal_static(__FUNCTION__, array());

      // If no 'referencing entity' is set, we are starting a new 'reference
      // thread' and need to reset the queue.
      // @todo Bug: $entity->referencing_entity on accounts referenced in a
      // different thread on the page. E.g: 1 references 1+2 / 2 references 1+2
      // visit homepage.
      // We'd need a more accurate way...
      if (!isset($entity->referencing_entity)) {
        $recursion_queue = array();
      }

      // The recursion queue only needs to track nodes.
      if ($entity_type == 'user') {
        list($id) = entity_extract_ids($entity_type, $entity);
        $recursion_queue[$id] = $id;
      }

      // Check the recursion queue to determine which accounts should be fully
      // displayed, and which accounts will only be displayed as a username.
      $users_display = array();
      foreach ($items as $delta => $item) {
        if ($item['access'] && !isset($recursion_queue[$item['uid']])) {
          $users_display[$item['uid']] = $item['user'];
        }
      }

      // Load and build the fully displayed nodes.
      if ($users_display) {
        $users_built = array(
          'users' => array(
            '#sorted' => TRUE,
          ),
        );
        foreach ($users_display as $uid => $account) {
          $users_display[$uid]->referencing_entity = $entity;
          $users_display[$uid]->referencing_field = $field['field_name'];
          $users_built['users'][$account->uid] = user_view($account, $settings['user_reference_view_mode']);
        }
      }

      // Assemble the render array.
      foreach ($items as $delta => $item) {
        if ($item['access']) {
          if (isset($users_display[$item['uid']])) {
            $result[$delta] = $users_built['users'][$item['uid']];
          }
          else {
            $account = $item['user'];
            $label = entity_label('user', $user);
            $uri = entity_uri('user', $account);
            $result[$delta] = array(
              '#type' => 'link',
              '#title' => $label,
              '#href' => $uri['path'],
              '#options' => $uri['options'],
            );
            if (!$account->status) {
              $result[$delta]['#prefix'] = '<span class="user-unpublished">';
              $result[$delta]['#suffix'] = '</span>';
            }
          }
        }
      }
      break;
    case 'user_reference_uid':
      foreach ($items as $delta => $item) {
        if ($item['access']) {
          $result[$delta] = array(
            '#markup' => $item['uid'],
          );
        }
      }
      break;
    case 'user_reference_path':
      foreach ($items as $delta => $item) {
        if ($item['access']) {
          $uri = entity_uri('user', $item['user']);
          $options = array(
            'absolute' => $settings['absolute'],
            'alias' => !$settings['alias'],
          );
          $options += $uri['options'];
          $result[$delta] = array(
            '#markup' => url($uri['path'], $options),
          );
        }
      }
      break;
  }
  return $result;
}