You are here

function civicrm_case_activity_reference_field_field_formatter_prepare_view in CiviCRM Entity 7.2

Implement hook_field_formatter_prepare_view().

Parameters

$entity_type:

$entities:

$field:

$instances:

$langcode:

$items:

$displays:

File

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

Code

function civicrm_case_activity_reference_field_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {

  // this function borrowed pretty much straight from the entityreference module :)
  $target_ids = array();
  $target_entity_type = 'civicrm_activity';

  // Collect every possible entity attached to any of the entities.
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {
      if (isset($item['target_id'])) {
        $target_ids[] = $item['target_id'];
      }
    }
  }
  if ($target_ids) {
    $target_entities = entity_load($target_entity_type, $target_ids);
  }
  else {
    $target_entities = array();
  }

  // Iterate through the fieldable entities again to attach the loaded data.
  foreach ($entities as $id => $entity) {
    $rekey = FALSE;
    foreach ($items[$id] as $delta => $item) {

      // Check whether the referenced entity could be loaded.
      if (isset($target_entities[$item['target_id']])) {

        // Replace the instance value with the term data.
        $items[$id][$delta]['entity'] = $target_entities[$item['target_id']];

        // Check whether the user has access to the referenced entity.
        $has_view_access = entity_access('view', $target_entity_type, $target_entities[$item['target_id']]) !== FALSE;
        $has_update_access = entity_access('update', $target_entity_type, $target_entities[$item['target_id']]) !== FALSE;
        $items[$id][$delta]['access'] = $has_view_access || $has_update_access;
      }
      else {
        unset($items[$id][$delta]);
        $rekey = TRUE;
      }
    }
    if ($rekey) {

      // Rekey the items array.
      $items[$id] = array_values($items[$id]);
    }
  }
}