You are here

function entity_embed_field_get_items in Entity Embed 7.3

Our own field-less and entity-less version of field_get_items().

Parameters

string $entity_type: The entity type of the entity we are trying to embed.

int $entity_id: The entity id of the entity we are trying to embed.

string $module: The module that implements the field formatter we are going to use.

In our version of field_get_items() we don't care at all about the field or the entity that it is attached to. All we want is the output of the formatter. So the point of this is return an $items array in the format expected by the passed module's particular implementation of hook_field_formatter_view().

Ideally this would be a simple hook letting other modules do the work, but we will hard-code support for the major players here.

1 call to entity_embed_field_get_items()
render_entity_embed_display_plugin in includes/entity_embed.display.inc
Renders an entity using an Entity Embed Display plugin.

File

includes/entity_embed.display.inc, line 307
Display functions.

Code

function entity_embed_field_get_items($entity_type, $entity_id, $module) {

  // Whatever module is involved, we're almost definitely going to need the
  // entity loaded and an access check.
  $entity = entity_load_single($entity_type, $entity_id);
  $access = entity_access('view', $entity_type, $entity);
  $item = array(
    'access' => $access,
  );

  // Support the entityreference module.
  if ('entityreference' == $module) {
    $item += array(
      'entity' => $entity,
      'target_id' => $entity_id,
    );
  }
  elseif ('taxonomy' == $module) {
    $item += array(
      'tid' => $entity_id,
      'name' => entity_label($entity_type, $entity_id),
      'taxonomy_term' => $entity,
    );
  }
  elseif ('file' == $module || 'image' == $module) {

    // The file and image modules just expect an array version of the object.
    $item += (array) $entity;
  }
  elseif ('user_reference' == $module) {
    $item += array(
      'uid' => $entity_id,
      'user' => $entity,
    );
  }
  elseif ('node_reference' == $module) {
    $item += array(
      'nid' => $entity_id,
      'node' => $entity,
    );
  }
  elseif ('file_entity' == $module) {

    // In general the file_entity module expects items to be
    // file objects, but in array form, similar to core image/file.
    $item += (array) $entity;
  }
  else {
    $contrib_items = module_invoke_all('entity_embed_field_get_items', $entity_type, $entity_id, $module);

    // Make sure we only get 1 item.
    if (!empty($contrib_items)) {
      $item += array_pop($contrib_items);
    }
  }
  return array(
    $item,
  );
}