43d1058c8761f7f4f4ce2e5f14a4ef6fb35c8410
[yaffs-website] / web / core / modules / views / src / Element / View.php
1 <?php
2
3 namespace Drupal\views\Element;
4
5 use Drupal\Core\Render\Element\RenderElement;
6 use Drupal\views\Views;
7
8 /**
9  * Provides a render element to display a view.
10  *
11  * @RenderElement("view")
12  */
13 class View extends RenderElement {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getInfo() {
19     $class = get_class($this);
20     return [
21       '#pre_render' => [
22         [$class, 'preRenderViewElement'],
23       ],
24       '#name' => NULL,
25       '#display_id' => 'default',
26       '#arguments' => [],
27       '#embed' => TRUE,
28       '#cache' => [],
29     ];
30   }
31
32   /**
33    * View element pre render callback.
34    */
35   public static function preRenderViewElement($element) {
36     // Allow specific Views displays to explicitly perform pre-rendering, for
37     // those displays that need to be able to know the fully built render array.
38     if (!empty($element['#pre_rendered'])) {
39       return $element;
40     }
41
42     if (!isset($element['#view'])) {
43       $view = Views::getView($element['#name']);
44     }
45     else {
46       $view = $element['#view'];
47     }
48
49     $element += $view->element;
50     $view->element = &$element;
51     // Mark the element as being prerendered, so other code like
52     // \Drupal\views\ViewExecutable::setCurrentPage knows that its no longer
53     // possible to manipulate the $element.
54     $view->element['#pre_rendered'] = TRUE;
55
56
57     if (isset($element['#response'])) {
58       $view->setResponse($element['#response']);
59     }
60
61     if ($view && $view->access($element['#display_id'])) {
62       if (!empty($element['#embed'])) {
63         $element['view_build'] = $view->preview($element['#display_id'], $element['#arguments']);
64       }
65       else {
66         // Add contextual links to the view. We need to attach them to the dummy
67         // $view_array variable, since contextual_preprocess() requires that they
68         // be attached to an array (not an object) in order to process them. For
69         // our purposes, it doesn't matter what we attach them to, since once they
70         // are processed by contextual_preprocess() they will appear in the
71         // $title_suffix variable (which we will then render in
72         // views-view.html.twig).
73         $view->setDisplay($element['#display_id']);
74         // Add the result of the executed view as a child element so any
75         // #pre_render elements for the view will get processed. A #pre_render
76         // element cannot be added to the main element as this is already inside
77         // a #pre_render callback.
78         $element['view_build'] = $view->executeDisplay($element['#display_id'], $element['#arguments']);
79
80         if (isset($element['view_build']['#title'])) {
81           $element['#title'] = &$element['view_build']['#title'];
82         }
83
84         if (empty($view->display_handler->getPluginDefinition()['returns_response'])) {
85           // views_add_contextual_links() needs the following information in
86           // order to be attached to the view.
87           $element['#view_id'] = $view->storage->id();
88           $element['#view_display_show_admin_links'] = $view->getShowAdminLinks();
89           $element['#view_display_plugin_id'] = $view->display_handler->getPluginId();
90           views_add_contextual_links($element, 'view', $view->current_display);
91         }
92       }
93       if (empty($view->display_handler->getPluginDefinition()['returns_response'])) {
94         $element['#attributes']['class'][] = 'views-element-container';
95         $element['#theme_wrappers'] = ['container'];
96       }
97     }
98
99     return $element;
100   }
101
102 }