832e23185ddff0eb04580943c6f916ebc736e996
[yaffs-website] / web / core / modules / views / src / Plugin / views / area / View.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\area;
4
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Views;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Views area handlers. Insert a view inside of an area.
12  *
13  * @ingroup views_area_handlers
14  *
15  * @ViewsArea("view")
16  */
17 class View extends AreaPluginBase {
18
19   /**
20    * Stores whether the embedded view is actually empty.
21    *
22    * @var bool
23    */
24   protected $isEmpty;
25
26   /**
27    * The view storage.
28    *
29    * @var \Drupal\Core\Entity\EntityStorageInterface
30    */
31   protected $viewStorage;
32
33   /**
34    * Constructs a View object.
35    *
36    * @param array $configuration
37    *   A configuration array containing information about the plugin instance.
38    * @param string $plugin_id
39    *   The plugin_id for the plugin instance.
40    * @param mixed $plugin_definition
41    *   The plugin implementation definition.
42    * @param \Drupal\Core\Entity\EntityStorageInterface $view_storage
43    *   The view storage.
44    */
45   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $view_storage) {
46     parent::__construct($configuration, $plugin_id, $plugin_definition);
47
48     $this->viewStorage = $view_storage;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
55     return new static(
56       $configuration,
57       $plugin_id,
58       $plugin_definition,
59       $container->get('entity.manager')->getStorage('view')
60     );
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   protected function defineOptions() {
67     $options = parent::defineOptions();
68
69     $options['view_to_insert'] = ['default' => ''];
70     $options['inherit_arguments'] = ['default' => FALSE];
71     return $options;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
78     parent::buildOptionsForm($form, $form_state);
79
80     $view_display = $this->view->storage->id() . ':' . $this->view->current_display;
81
82     $options = ['' => $this->t('-Select-')];
83     $options += Views::getViewsAsOptions(FALSE, 'all', $view_display, FALSE, TRUE);
84     $form['view_to_insert'] = [
85       '#type' => 'select',
86       '#title' => $this->t('View to insert'),
87       '#default_value' => $this->options['view_to_insert'],
88       '#description' => $this->t('The view to insert into this area.'),
89       '#options' => $options,
90     ];
91
92     $form['inherit_arguments'] = [
93       '#type' => 'checkbox',
94       '#title' => $this->t('Inherit contextual filters'),
95       '#default_value' => $this->options['inherit_arguments'],
96       '#description' => $this->t('If checked, this view will receive the same contextual filters as its parent.'),
97     ];
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function render($empty = FALSE) {
104     if (!empty($this->options['view_to_insert'])) {
105       list($view_name, $display_id) = explode(':', $this->options['view_to_insert']);
106
107       $view = $this->viewStorage->load($view_name)->getExecutable();
108
109       if (empty($view) || !$view->access($display_id)) {
110         return [];
111       }
112       $view->setDisplay($display_id);
113
114       // Avoid recursion
115       $view->parent_views += $this->view->parent_views;
116       $view->parent_views[] = "$view_name:$display_id";
117
118       // Check if the view is part of the parent views of this view
119       $search = "$view_name:$display_id";
120       if (in_array($search, $this->view->parent_views)) {
121         drupal_set_message(t("Recursion detected in view @view display @display.", ['@view' => $view_name, '@display' => $display_id]), 'error');
122       }
123       else {
124         if (!empty($this->options['inherit_arguments']) && !empty($this->view->args)) {
125           $output = $view->preview($display_id, $this->view->args);
126         }
127         else {
128           $output = $view->preview($display_id);
129         }
130         $this->isEmpty = $view->display_handler->outputIsEmpty();
131         return $output;
132       }
133     }
134     return [];
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function isEmpty() {
141     if (isset($this->isEmpty)) {
142       return $this->isEmpty;
143     }
144     else {
145       return parent::isEmpty();
146     }
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function calculateDependencies() {
153     $dependencies = parent::calculateDependencies();
154
155     list($view_id) = explode(':', $this->options['view_to_insert'], 2);
156     // Don't call the current view, as it would result into an infinite recursion.
157     if ($view_id && $this->view->storage->id() != $view_id) {
158       $view = $this->viewStorage->load($view_id);
159       $dependencies[$view->getConfigDependencyKey()][] = $view->getConfigDependencyName();
160     }
161
162     return $dependencies;
163   }
164
165 }