View.php 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Contains \Drupal\views\Element\View.
 */

namespace Drupal\views\Element;

use Drupal\Core\Render\Element\RenderElement;
use Drupal\views\Views;

/**
 * Provides a render element to display a view.
 *
16
 * @RenderElement("view")
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 */
class View extends RenderElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return array(
      '#pre_render' => array(
        array($class, 'preRenderViewElement'),
      ),
      '#name' => NULL,
      '#display_id' => 'default',
      '#arguments' => array(),
32
      '#embed' => TRUE,
33
      '#cache' => [],
34 35 36 37 38 39 40
    );
  }

  /**
   * View element pre render callback.
   */
  public static function preRenderViewElement($element) {
41 42 43 44 45 46
    // Allow specific Views displays to explicitly perform pre-rendering, for
    // those displays that need to be able to know the fully built render array.
    if (!empty($element['#pre_rendered'])) {
      return $element;
    }

47 48 49 50 51 52 53
    if (!isset($element['#view'])) {
      $view = Views::getView($element['#name']);
    }
    else {
      $view = $element['#view'];
    }

54
    $element += $view->element;
55 56 57 58 59 60 61 62 63 64 65
    $view->element = &$element;
    // Mark the element as being prerendered, so other code like
    // \Drupal\views\ViewExecutable::setCurrentPage knows that its no longer
    // possible to manipulate the $element.
    $view->element['#pre_rendered'] = TRUE;


    if (isset($element['#response'])) {
      $view->setResponse($element['#response']);
    }

66
    if ($view && $view->access($element['#display_id'])) {
67
      if (!empty($element['#embed'])) {
68
        $element['view_build'] = $view->preview($element['#display_id'], $element['#arguments']);
69 70 71 72 73 74 75 76 77 78
      }
      else {
        // Add contextual links to the view. We need to attach them to the dummy
        // $view_array variable, since contextual_preprocess() requires that they
        // be attached to an array (not an object) in order to process them. For
        // our purposes, it doesn't matter what we attach them to, since once they
        // are processed by contextual_preprocess() they will appear in the
        // $title_suffix variable (which we will then render in
        // views-view.html.twig).
        $view->setDisplay($element['#display_id']);
79 80 81 82 83 84 85 86 87 88
        // Add the result of the executed view as a child element so any
        // #pre_render elements for the view will get processed. A #pre_render
        // element cannot be added to the main element as this is already inside
        // a #pre_render callback.
        $element['view_build'] = $view->executeDisplay($element['#display_id'], $element['#arguments']);

        if (isset($element['view_build']['#title'])) {
          $element['#title'] = &$element['view_build']['#title'];
        }

89 90 91 92 93 94 95 96 97 98 99 100
        if (empty($view->display_handler->getPluginDefinition()['returns_response'])) {
          // views_add_contextual_links() needs the following information in
          // order to be attached to the view.
          $element['#view_id'] = $view->storage->id();
          $element['#view_display_show_admin_links'] = $view->getShowAdminLinks();
          $element['#view_display_plugin_id'] = $view->display_handler->getPluginId();
          views_add_contextual_links($element, 'view', $view->current_display);
        }
      }
      if (empty($view->display_handler->getPluginDefinition()['returns_response'])) {
        $element['#attributes']['class'][] = 'views-element-container';
        $element['#theme_wrappers'] = array('container');
101
      }
102 103 104 105 106 107
    }

    return $element;
  }

}