You are here

function render_entity_embed_display_plugin in Entity Embed 7.3

Same name and namespace in other branches
  1. 7 includes/entity_embed.display.inc \render_entity_embed_display_plugin()
  2. 7.2 includes/entity_embed.display.inc \render_entity_embed_display_plugin()

Renders an entity using an Entity Embed Display plugin.

Parameters

$entity_type: The type of entity, i.e. 'node', 'user'.

$entity: The entity to be rendered.

string $plugin_id: The Entity Embed Display plugin ID.

array $plugin_configuration: (optional) Array of plugin configuration values.

array $context: (optional) Array of additional context values, usually the embed HTML tag's attributes.

Return value

array A render array for the Entity Embed Display plugin.

1 call to render_entity_embed_display_plugin()
entity_embed_render_entity in includes/entity_embed.display.inc
Renders an embedded entity.

File

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

Code

function render_entity_embed_display_plugin($entity_type, $entity, $plugin_id, array $plugin_configuration = array(), array $context = array()) {

  // Check if the display plugin is accessible.
  if (!entity_access('view', $entity_type, $entity)) {
    return array();
  }

  // Split the formatter into separate parts.
  $formatter_parts = explode(':', $plugin_id);
  $formatter_module = $formatter_parts[0];
  $formatter_type = $formatter_parts[1];
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  $field_formatter_info = field_info_formatter_types($formatter_type);

  // With the File Entity module enabled, file entities require some special
  // handling. Unlike standard field formatters which specify a list of
  // compatible entity types and will work with any entity as long as it is the
  // correct type, file formatters may only support files with specific MIME
  // types.
  if ($entity_type == 'file' && module_exists('file_entity')) {
    if (isset($field_formatter_info['file formatter']['mime types'])) {
      if (!file_entity_match_mimetypes($field_formatter_info['file formatter']['mime types'], $entity->filemime)) {
        $file_type = file_type_load($entity->type);

        // Inform the user that the file they are trying to embed cannot be
        // displayed using the selected formatter.
        return array(
          '#markup' => '<p>' . t('The %type file %filename is unable to be displayed as %formatter. Please select a different formatter.', array(
            '%type' => $file_type->label,
            '%filename' => $entity->filename,
            '%formatter' => $field_formatter_info['label'],
          )) . '</p>',
        );
      }
    }
  }
  if (isset($field_formatter_info['module'])) {

    // Set $display['type'] and $display['settings'] to what
    // hook_field_formatter_*() expects.
    $display['type'] = $formatter_type;
    $display['settings'] = $plugin_configuration += field_info_formatter_settings($formatter_type);

    // Use default settings if no settings are available.
    // Set $items to what the field formatter expects. See hook_field_load(),
    // and note that, here, $entity is already a fully loaded entity.
    $items = entity_embed_field_get_items($entity_type, $id, $field_formatter_info['module']);

    // Invoke hook_field_formatter_prepare_view() and
    // hook_field_formatter_view(). Note that we are reusing field formatter
    // functions, but we are not displaying a Field API field, so we set
    // $field and $instance accordingly, and do not invoke
    // hook_field_prepare_view(). This assumes that the formatter functions do
    // not rely on $field or $instance. A module that implements formatter
    // functions that rely on $field or $instance (and therefore, can only be
    // used for real fields) can add support for being used on the pseudo-field
    // by adding in any missing information using the entity_embed_field_info
    // hooks.
    $field = entity_embed_field_info_field($entity_type, $field_formatter_info['module']);
    $instance = entity_embed_field_info_instance($formatter_type, $plugin_configuration);
    if (($function = $field_formatter_info['module'] . '_field_formatter_prepare_view') && function_exists($function)) {

      // hook_field_formatter_prepare_view() alters $items by reference.
      $grouped_items = array(
        $id => &$items,
      );
      $function($entity_type, array(
        $id => $entity,
      ), $field, array(
        $id => $instance,
      ), $context['data-langcode'], $grouped_items, array(
        $id => $display,
      ));
    }
    if (($function = $field_formatter_info['module'] . '_field_formatter_view') && function_exists($function)) {
      $element = $function($entity_type, $entity, $field, $instance, $context['data-langcode'], $items, $display);

      // We passed the entity as $items[0], so return the corresponding element.
      if (isset($element[0])) {
        return $element[0];
      }
    }
  }
}