You are here

function eva_entity_view in EVA: Entity Views Attachment 8.2

Same name and namespace in other branches
  1. 8 eva.module \eva_entity_view()

Implements hook_entity_view().

File

./eva.module, line 49
Module implementing EVA extra field and views display.

Code

function eva_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $type = $entity
    ->getEntityTypeId();
  $views = \Drupal::service('eva.view_displays')
    ->get($type);
  $token_handler = \Drupal::service('eva.token_handler');
  foreach ($views as $info) {
    $longname = $info['name'] . '_' . $info['display'];
    if ($display
      ->getComponent($longname)) {
      if ($view = Views::getView($info['name'])) {
        $view
          ->setDisplay($info['display']);
        if ((empty($info['bundles']) || in_array($display
          ->getTargetBundle(), $info['bundles'])) && $view
          ->access($info['display'])) {

          // Save the entity for path calculation.
          $view->current_entity = $entity;

          // Gather info about the attached-to entity.
          $entity_type = $view->display_handler
            ->getOption('entity_type');
          $arg_mode = $view->display_handler
            ->getOption('argument_mode');
          if ($arg_mode == 'token') {
            if ($token_string = $view->display_handler
              ->getOption('default_argument')) {

              // Now do the token replacement.
              $token_values = $token_handler
                ->getArgumentsFromTokenString($token_string, $entity_type, $entity);
              $new_args = [];

              // We have to be careful to only replace arguments that have
              // tokens.
              foreach ($token_values as $key => $value) {
                $new_args[Html::escape($key)] = Html::escape($value);
              }
              $view->args = $new_args;
            }
          }
          elseif ($arg_mode == 'id') {
            $view->args = [
              $entity
                ->id(),
            ];
          }

          // Add an argument cache key
          // If there are more than one of the same Eva on the same page,
          // the first one gets cached.
          // Presumably they should vary by contextual argument, so this
          // adds a cache key for the argument(s).
          // see https://www.drupal.org/node/2873385
          if ($view->args) {
            $view->element['#cache'] += [
              'keys' => [],
            ];
            $view->element['#cache']['keys'] = array_merge([
              implode(':', $view->args),
            ], $view->element['#cache']['keys']);
          }

          // Now that arguments are set, build the exposed form.
          if ($info['uses exposed'] && $display
            ->getComponent($longname . '_form')) {
            $view
              ->initHandlers();
            $exposed_form = $view->display_handler
              ->getPlugin('exposed_form');
            $build[$longname . '_form'] = $exposed_form
              ->renderExposedForm(TRUE);
          }

          // Build the render.
          $element = $view
            ->buildRenderable($info['display']);
          if (!empty($element)) {
            $build[$longname] = $element;
          }
        }
      }
    }
  }
}