Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / views / src / Plugin / views / display / Attachment.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\display;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ViewExecutable;
7
8 /**
9  * The plugin that handles an attachment display.
10  *
11  * Attachment displays are secondary displays that are 'attached' to a primary
12  * display. Effectively they are a simple way to get multiple views within
13  * the same view. They can share some information.
14  *
15  * @ingroup views_display_plugins
16  *
17  * @ViewsDisplay(
18  *   id = "attachment",
19  *   title = @Translation("Attachment"),
20  *   help = @Translation("Attachments added to other displays to achieve multiple views in the same view."),
21  *   theme = "views_view",
22  *   contextual_links_locations = {""}
23  * )
24  */
25 class Attachment extends DisplayPluginBase {
26
27   /**
28    * Whether the display allows the use of a pager or not.
29    *
30    * @var bool
31    */
32   protected $usesPager = FALSE;
33
34   protected function defineOptions() {
35     $options = parent::defineOptions();
36
37     $options['displays'] = ['default' => []];
38     $options['attachment_position'] = ['default' => 'before'];
39     $options['inherit_arguments'] = ['default' => TRUE];
40     $options['inherit_exposed_filters'] = ['default' => FALSE];
41     $options['inherit_pager'] = ['default' => FALSE];
42     $options['render_pager'] = ['default' => FALSE];
43
44     return $options;
45   }
46
47   public function execute() {
48     return $this->view->render($this->display['id']);
49   }
50
51   public function attachmentPositions($position = NULL) {
52     $positions = [
53       'before' => $this->t('Before'),
54       'after' => $this->t('After'),
55       'both' => $this->t('Both'),
56     ];
57
58     if ($position) {
59       return $positions[$position];
60     }
61
62     return $positions;
63   }
64
65   /**
66    * Provide the summary for attachment options in the views UI.
67    *
68    * This output is returned as an array.
69    */
70   public function optionsSummary(&$categories, &$options) {
71     // It is very important to call the parent function here:
72     parent::optionsSummary($categories, $options);
73
74     $categories['attachment'] = [
75       'title' => $this->t('Attachment settings'),
76       'column' => 'second',
77       'build' => [
78         '#weight' => -10,
79       ],
80     ];
81
82     $displays = array_filter($this->getOption('displays'));
83     if (count($displays) > 1) {
84       $attach_to = $this->t('Multiple displays');
85     }
86     elseif (count($displays) == 1) {
87       $display = array_shift($displays);
88       if ($display = $this->view->storage->getDisplay($display)) {
89         $attach_to = $display['display_title'];
90       }
91     }
92
93     if (!isset($attach_to)) {
94       $attach_to = $this->t('Not defined');
95     }
96
97     $options['displays'] = [
98       'category' => 'attachment',
99       'title' => $this->t('Attach to'),
100       'value' => $attach_to,
101     ];
102
103     $options['attachment_position'] = [
104       'category' => 'attachment',
105       'title' => $this->t('Attachment position'),
106       'value' => $this->attachmentPositions($this->getOption('attachment_position')),
107     ];
108
109     $options['inherit_arguments'] = [
110       'category' => 'attachment',
111       'title' => $this->t('Inherit contextual filters'),
112       'value' => $this->getOption('inherit_arguments') ? $this->t('Yes') : $this->t('No'),
113     ];
114
115     $options['inherit_exposed_filters'] = [
116       'category' => 'attachment',
117       'title' => $this->t('Inherit exposed filters'),
118       'value' => $this->getOption('inherit_exposed_filters') ? $this->t('Yes') : $this->t('No'),
119     ];
120
121     $options['inherit_pager'] = [
122       'category' => 'pager',
123       'title' => $this->t('Inherit pager'),
124       'value' => $this->getOption('inherit_pager') ? $this->t('Yes') : $this->t('No'),
125     ];
126
127     $options['render_pager'] = [
128       'category' => 'pager',
129       'title' => $this->t('Render pager'),
130       'value' => $this->getOption('render_pager') ? $this->t('Yes') : $this->t('No'),
131     ];
132
133   }
134
135   /**
136    * Provide the default form for setting options.
137    */
138   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
139     // It is very important to call the parent function here:
140     parent::buildOptionsForm($form, $form_state);
141
142     switch ($form_state->get('section')) {
143       case 'inherit_arguments':
144         $form['#title'] .= $this->t('Inherit contextual filters');
145         $form['inherit_arguments'] = [
146           '#type' => 'checkbox',
147           '#title' => $this->t('Inherit'),
148           '#description' => $this->t('Should this display inherit its contextual filter values from the parent display to which it is attached?'),
149           '#default_value' => $this->getOption('inherit_arguments'),
150         ];
151         break;
152       case 'inherit_exposed_filters':
153         $form['#title'] .= $this->t('Inherit exposed filters');
154         $form['inherit_exposed_filters'] = [
155           '#type' => 'checkbox',
156           '#title' => $this->t('Inherit'),
157           '#description' => $this->t('Should this display inherit its exposed filter values from the parent display to which it is attached?'),
158           '#default_value' => $this->getOption('inherit_exposed_filters'),
159         ];
160         break;
161       case 'inherit_pager':
162         $form['#title'] .= $this->t('Inherit pager');
163         $form['inherit_pager'] = [
164           '#type' => 'checkbox',
165           '#title' => $this->t('Inherit'),
166           '#description' => $this->t('Should this display inherit its paging values from the parent display to which it is attached?'),
167           '#default_value' => $this->getOption('inherit_pager'),
168         ];
169         break;
170       case 'render_pager':
171         $form['#title'] .= $this->t('Render pager');
172         $form['render_pager'] = [
173           '#type' => 'checkbox',
174           '#title' => $this->t('Render'),
175           '#description' => $this->t('Should this display render the pager values? This is only meaningful if inheriting a pager.'),
176           '#default_value' => $this->getOption('render_pager'),
177         ];
178         break;
179       case 'attachment_position':
180         $form['#title'] .= $this->t('Position');
181         $form['attachment_position'] = [
182           '#title' => $this->t('Position'),
183           '#type' => 'radios',
184           '#description' => $this->t('Attach before or after the parent display?'),
185           '#options' => $this->attachmentPositions(),
186           '#default_value' => $this->getOption('attachment_position'),
187         ];
188         break;
189       case 'displays':
190         $form['#title'] .= $this->t('Attach to');
191         $displays = [];
192         foreach ($this->view->storage->get('display') as $display_id => $display) {
193           if ($this->view->displayHandlers->has($display_id) && $this->view->displayHandlers->get($display_id)->acceptAttachments()) {
194             $displays[$display_id] = $display['display_title'];
195           }
196         }
197         $form['displays'] = [
198           '#title' => $this->t('Displays'),
199           '#type' => 'checkboxes',
200           '#description' => $this->t('Select which display or displays this should attach to.'),
201           '#options' => array_map('\Drupal\Component\Utility\Html::escape', $displays),
202           '#default_value' => $this->getOption('displays'),
203         ];
204         break;
205     }
206   }
207
208   /**
209    * Perform any necessary changes to the form values prior to storage.
210    * There is no need for this function to actually store the data.
211    */
212   public function submitOptionsForm(&$form, FormStateInterface $form_state) {
213     // It is very important to call the parent function here:
214     parent::submitOptionsForm($form, $form_state);
215     $section = $form_state->get('section');
216     switch ($section) {
217       case 'displays':
218         $form_state->setValue($section, array_filter($form_state->getValue($section)));
219       case 'inherit_arguments':
220       case 'inherit_pager':
221       case 'render_pager':
222       case 'inherit_exposed_filters':
223       case 'attachment_position':
224         $this->setOption($section, $form_state->getValue($section));
225         break;
226     }
227   }
228
229   /**
230    * {@inheritdoc}
231    */
232   public function attachTo(ViewExecutable $view, $display_id, array &$build) {
233     $displays = $this->getOption('displays');
234
235     if (empty($displays[$display_id])) {
236       return;
237     }
238
239     if (!$this->access()) {
240       return;
241     }
242
243     $args = $this->getOption('inherit_arguments') ? $this->view->args : [];
244     $view->setArguments($args);
245     $view->setDisplay($this->display['id']);
246     if ($this->getOption('inherit_pager')) {
247       $view->display_handler->usesPager = $this->view->displayHandlers->get($display_id)->usesPager();
248       $view->display_handler->setOption('pager', $this->view->displayHandlers->get($display_id)->getOption('pager'));
249     }
250
251     $attachment = $view->buildRenderable($this->display['id'], $args);
252
253     switch ($this->getOption('attachment_position')) {
254       case 'before':
255         $this->view->attachment_before[] = $attachment;
256         break;
257       case 'after':
258         $this->view->attachment_after[] = $attachment;
259         break;
260       case 'both':
261         $this->view->attachment_before[] = $attachment;
262         $this->view->attachment_after[] = $attachment;
263         break;
264     }
265
266   }
267
268   /**
269    * Attachment displays only use exposed widgets if
270    * they are set to inherit the exposed filter settings
271    * of their parent display.
272    */
273   public function usesExposed() {
274     if (!empty($this->options['inherit_exposed_filters']) && parent::usesExposed()) {
275       return TRUE;
276     }
277     return FALSE;
278   }
279
280   /**
281    * If an attachment is set to inherit the exposed filter
282    * settings from its parent display, then don't render and
283    * display a second set of exposed filter widgets.
284    */
285   public function displaysExposed() {
286     return $this->options['inherit_exposed_filters'] ? FALSE : TRUE;
287   }
288
289   public function renderPager() {
290     return $this->usesPager() && $this->getOption('render_pager');
291   }
292
293 }