You are here

function civicrm_case_activity_reference_field_field_formatter_view in CiviCRM Entity 7.2

Implements hook_field_formatter_view().

File

modules/civicrm_case_activity_reference_field/civicrm_case_activity_reference_field.module, line 418
Provide CiviCRM entity reference field type

Code

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

  // Rebuild the items list to contain only those with access.
  foreach ($items as $key => $item) {
    if (empty($item['access'])) {
      unset($items[$key]);
    }
  }
  switch ($display['type']) {
    case 'civicase_activity_reference_default_formatter':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $item['target_id'],
        );
      }
      break;
    case 'civicase_activity_reference_entity_formatter':
      $target_entity_type = 'civicrm_activity';
      foreach ($items as $delta => $item) {
        static $depth = 0;
        $depth++;
        if ($depth > 20) {
          throw new EntityReferenceRecursiveRenderingException(t('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array(
            '@entity_type' => $entity_type,
            '@entity_id' => $item['target_id'],
          )));
        }
        $entity_to_render = $item['entity'];
        unset($entity_to_render->content);
        $view_mode = isset($settings['view_mode']) ? $settings['view_mode'] : 'default';
        $content = entity_view($target_entity_type, array(
          $item['target_id'] => $entity_to_render,
        ), $view_mode, $langcode, FALSE);
        $depth = 0;
        $element[$delta] = $content;
      }
      break;
    case 'civicase_activity_reference_entity_label_formatter':
      $target_entity_type = 'civicrm_activity';
      foreach ($items as $delta => $item) {
        $output_as_link = !empty($settings['link_to_entity']) ? 1 : 0;
        $entity_text = entity_label($target_entity_type, $item['entity']);
        $entity_path = '/' . str_replace('_', '-', $target_entity_type) . '/' . $item['target_id'];
        if (empty($entity_text)) {
          $entity_text = ucwords(str_replace('_', ' ', $target_entity_type)) . 'id:' . $item['target_id'];
        }
        if ($output_as_link) {
          $element[$delta] = array(
            '#markup' => l($entity_text, $entity_path),
          );
        }
        else {
          $element[$delta] = array(
            '#markup' => $entity_text,
          );
        }
      }
      break;
  }
  return $element;
}