You are here

function _eva_build_extra_fields in EVA: Entity Views Attachment 7

Adds the EVA to the build, which is either the rendered entity or the form.

Parameters

array $build: The rendered entity or the entity form.

string $entity_type: The entity type.

object $entity: The entity object. This is used by the display plugin for processing.

array $fields: The array of extra fields.

string $name: The name of the view.

string $display_id: The display ID of the view.

string $context: Where this EVA is being used, either 'form' or 'display'.

2 calls to _eva_build_extra_fields()
eva_entity_view_alter in ./eva.module
Implements hook_entity_view_alter().
eva_field_attach_form in ./eva.module
Implements hook_field_attach_form().

File

./eva.module, line 126

Code

function _eva_build_extra_fields(&$build, $entity_type, $entity, $fields, $name, $display_id, $context) {
  if ($view = views_get_view($name)) {
    $view
      ->set_display($display_id);
    if ($view
      ->access($display_id)) {
      $view->current_entity = $entity;
      $view->current_entity_type = $entity_type;
      $view->current_entity_view_mode = $context === 'form' ? 'form' : $build['#view_mode'];
      $longname = $name . '_' . $display_id;
      if ($context == 'display' && isset($fields[$longname . '_form'])) {
        $view
          ->init_handlers();
        $exposed_form = $view->display_handler
          ->get_plugin('exposed_form');
        $build[$longname . '_form'] = array(
          '#markup' => $exposed_form
            ->render_exposed_form(TRUE),
        );
      }
      $arg_mode = $view->display_handler
        ->get_option('argument_mode');
      if ($arg_mode == 'token') {
        $args = array();
        if ($token_string = $view->display_handler
          ->get_option('default_argument')) {

          // Now do the token replacement.
          $token_values = eva_get_arguments_from_token_string($token_string, $entity_type, $entity);

          // We have to be careful to only replace arguments that have tokens.
          foreach ($token_values as $key => $value) {
            $args[$key] = $value;
          }
        }
      }
      elseif ($arg_mode == 'id') {
        list($id, , ) = entity_extract_ids($entity_type, $entity);
        $args = array(
          $id,
        );
      }
      else {

        // We should not get here, but if we are, prevent notices.
        $args = array();
      }
      $result = $view
        ->execute_display($display_id, $args);
      if (!empty($result)) {
        $build[$longname] = array(
          '#markup' => $result,
          '#weight' => $fields[$longname]['weight'],
        );
      }
    }
  }
}