Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / src / Plugin / views / display / Block.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\display;
4
5 use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
6 use Drupal\Core\Block\BlockManagerInterface;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\views\Plugin\Block\ViewsBlock;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * The plugin that handles a block.
14  *
15  * @ingroup views_display_plugins
16  *
17  * @ViewsDisplay(
18  *   id = "block",
19  *   title = @Translation("Block"),
20  *   help = @Translation("Display the view as a block."),
21  *   theme = "views_view",
22  *   register_theme = FALSE,
23  *   uses_hook_block = TRUE,
24  *   contextual_links_locations = {"block"},
25  *   admin = @Translation("Block")
26  * )
27  *
28  * @see \Drupal\views\Plugin\Block\ViewsBlock
29  * @see \Drupal\views\Plugin\Derivative\ViewsBlock
30  */
31 class Block extends DisplayPluginBase {
32
33   /**
34    * Whether the display allows attachments.
35    *
36    * @var bool
37    */
38   protected $usesAttachments = TRUE;
39
40   /**
41    * The entity manager.
42    *
43    * @var \Drupal\Core\Entity\EntityManagerInterface
44    */
45   protected $entityManager;
46
47   /**
48    * The block manager.
49    *
50    * @var \Drupal\Core\Block\BlockManagerInterface
51    */
52   protected $blockManager;
53
54   /**
55    * Constructs a new Block instance.
56    *
57    * @param array $configuration
58    *   A configuration array containing information about the plugin instance.
59    * @param string $plugin_id
60    *   The plugin_id for the plugin instance.
61    * @param mixed $plugin_definition
62    *   The plugin implementation definition.
63    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
64    *   The entity manager.
65    * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
66    *   The block manager.
67    */
68   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, BlockManagerInterface $block_manager) {
69     parent::__construct($configuration, $plugin_id, $plugin_definition);
70
71     $this->entityManager = $entity_manager;
72     $this->blockManager = $block_manager;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
79     return new static(
80       $configuration,
81       $plugin_id,
82       $plugin_definition,
83       $container->get('entity.manager'),
84       $container->get('plugin.manager.block')
85     );
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   protected function defineOptions() {
92     $options = parent::defineOptions();
93
94     $options['block_description'] = ['default' => ''];
95     $options['block_category'] = ['default' => $this->t('Lists (Views)')];
96     $options['block_hide_empty'] = ['default' => FALSE];
97
98     $options['allow'] = [
99       'contains' => [
100         'items_per_page' => ['default' => 'items_per_page'],
101       ],
102     ];
103
104     return $options;
105   }
106
107   /**
108    * Returns plugin-specific settings for the block.
109    *
110    * @param array $settings
111    *   The settings of the block.
112    *
113    * @return array
114    *   An array of block-specific settings to override the defaults provided in
115    *   \Drupal\views\Plugin\Block\ViewsBlock::defaultConfiguration().
116    *
117    * @see \Drupal\views\Plugin\Block\ViewsBlock::defaultConfiguration()
118    */
119   public function blockSettings(array $settings) {
120     $settings['items_per_page'] = 'none';
121     return $settings;
122   }
123
124   /**
125    * The display block handler returns the structure necessary for a block.
126    */
127   public function execute() {
128     // Prior to this being called, the $view should already be set to this
129     // display, and arguments should be set on the view.
130     $element = $this->view->render();
131     if ($this->outputIsEmpty() && $this->getOption('block_hide_empty') && empty($this->view->style_plugin->definition['even empty'])) {
132       return [];
133     }
134     else {
135       return $element;
136     }
137   }
138
139   /**
140    * Provide the summary for page options in the views UI.
141    *
142    * This output is returned as an array.
143    */
144   public function optionsSummary(&$categories, &$options) {
145     parent::optionsSummary($categories, $options);
146
147     $categories['block'] = [
148       'title' => $this->t('Block settings'),
149       'column' => 'second',
150       'build' => [
151         '#weight' => -10,
152       ],
153     ];
154
155     $block_description = strip_tags($this->getOption('block_description'));
156     if (empty($block_description)) {
157       $block_description = $this->t('None');
158     }
159     $block_category = $this->getOption('block_category');
160
161     $options['block_description'] = [
162       'category' => 'block',
163       'title' => $this->t('Block name'),
164       'value' => views_ui_truncate($block_description, 24),
165     ];
166     $options['block_category'] = [
167       'category' => 'block',
168       'title' => $this->t('Block category'),
169       'value' => views_ui_truncate($block_category, 24),
170     ];
171
172     $filtered_allow = array_filter($this->getOption('allow'));
173
174     $options['allow'] = [
175       'category' => 'block',
176       'title' => $this->t('Allow settings'),
177       'value' => empty($filtered_allow) ? $this->t('None') : $this->t('Items per page'),
178     ];
179
180     $options['block_hide_empty'] = [
181       'category' => 'other',
182       'title' => $this->t('Hide block if the view output is empty'),
183       'value' => $this->getOption('block_hide_empty') ? $this->t('Yes') : $this->t('No'),
184     ];
185   }
186
187   /**
188    * Provide the default form for setting options.
189    */
190   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
191     parent::buildOptionsForm($form, $form_state);
192
193     switch ($form_state->get('section')) {
194       case 'block_description':
195         $form['#title'] .= $this->t('Block admin description');
196         $form['block_description'] = [
197           '#type' => 'textfield',
198           '#description' => $this->t('This will appear as the name of this block in administer >> structure >> blocks.'),
199           '#default_value' => $this->getOption('block_description'),
200         ];
201         break;
202       case 'block_category':
203         $form['#title'] .= $this->t('Block category');
204         $form['block_category'] = [
205           '#type' => 'textfield',
206           '#autocomplete_route_name' => 'block.category_autocomplete',
207           '#description' => $this->t('The category this block will appear under on the <a href=":href">blocks placement page</a>.', [':href' => \Drupal::url('block.admin_display')]),
208           '#default_value' => $this->getOption('block_category'),
209         ];
210         break;
211       case 'block_hide_empty':
212         $form['#title'] .= $this->t('Block empty settings');
213
214         $form['block_hide_empty'] = [
215           '#title' => $this->t('Hide block if no result/empty text'),
216           '#type' => 'checkbox',
217           '#description' => $this->t('Hide the block if there is no result and no empty text and no header/footer which is shown on empty result'),
218           '#default_value' => $this->getOption('block_hide_empty'),
219         ];
220         break;
221       case 'exposed_form_options':
222         $this->view->initHandlers();
223         if (!$this->usesExposed() && parent::usesExposed()) {
224           $form['exposed_form_options']['warning'] = [
225             '#weight' => -10,
226             '#markup' => '<div class="messages messages--warning">' . $this->t('Exposed filters in block displays require "Use AJAX" to be set to work correctly.') . '</div>',
227           ];
228         }
229         break;
230       case 'allow':
231         $form['#title'] .= $this->t('Allow settings in the block configuration');
232
233         $options = [
234           'items_per_page' => $this->t('Items per page'),
235         ];
236
237         $allow = array_filter($this->getOption('allow'));
238         $form['allow'] = [
239           '#type' => 'checkboxes',
240           '#default_value' => $allow,
241           '#options' => $options,
242         ];
243         break;
244     }
245   }
246
247   /**
248    * Perform any necessary changes to the form values prior to storage.
249    * There is no need for this function to actually store the data.
250    */
251   public function submitOptionsForm(&$form, FormStateInterface $form_state) {
252     parent::submitOptionsForm($form, $form_state);
253     $section = $form_state->get('section');
254     switch ($section) {
255       case 'block_description':
256       case 'block_category':
257       case 'allow':
258       case 'block_hide_empty':
259         $this->setOption($section, $form_state->getValue($section));
260         break;
261     }
262   }
263
264   /**
265    * Adds the configuration form elements specific to this views block plugin.
266    *
267    * This method allows block instances to override the views items_per_page.
268    *
269    * @param \Drupal\views\Plugin\Block\ViewsBlock $block
270    *   The ViewsBlock plugin.
271    * @param array $form
272    *   The form definition array for the block configuration form.
273    * @param \Drupal\Core\Form\FormStateInterface $form_state
274    *   The current state of the form.
275    *
276    * @return array
277    *   The renderable form array representing the entire configuration form.
278    *
279    * @see \Drupal\views\Plugin\Block\ViewsBlock::blockForm()
280    */
281   public function blockForm(ViewsBlock $block, array &$form, FormStateInterface $form_state) {
282     $allow_settings = array_filter($this->getOption('allow'));
283
284     $block_configuration = $block->getConfiguration();
285
286     foreach ($allow_settings as $type => $enabled) {
287       if (empty($enabled)) {
288         continue;
289       }
290       switch ($type) {
291         case 'items_per_page':
292           $form['override']['items_per_page'] = [
293             '#type' => 'select',
294             '#title' => $this->t('Items per block'),
295             '#options' => [
296               'none' => $this->t('@count (default setting)', ['@count' => $this->getPlugin('pager')->getItemsPerPage()]),
297               5 => 5,
298               10 => 10,
299               20 => 20,
300               40 => 40,
301             ],
302             '#default_value' => $block_configuration['items_per_page'],
303           ];
304           break;
305       }
306     }
307
308     return $form;
309   }
310
311   /**
312    * Handles form validation for the views block configuration form.
313    *
314    * @param \Drupal\views\Plugin\Block\ViewsBlock $block
315    *   The ViewsBlock plugin.
316    * @param array $form
317    *   The form definition array for the block configuration form.
318    * @param \Drupal\Core\Form\FormStateInterface $form_state
319    *   The current state of the form.
320    *
321    * @see \Drupal\views\Plugin\Block\ViewsBlock::blockValidate()
322    */
323   public function blockValidate(ViewsBlock $block, array $form, FormStateInterface $form_state) {
324   }
325
326   /**
327    * Handles form submission for the views block configuration form.
328    *
329    * @param \Drupal\views\Plugin\Block\ViewsBlock $block
330    *   The ViewsBlock plugin.
331    * @param array $form
332    *   The form definition array for the full block configuration form.
333    * @param \Drupal\Core\Form\FormStateInterface $form_state
334    *   The current state of the form.
335    *
336    * @see \Drupal\views\Plugin\Block\ViewsBlock::blockSubmit()
337    */
338   public function blockSubmit(ViewsBlock $block, $form, FormStateInterface $form_state) {
339     if ($items_per_page = $form_state->getValue(['override', 'items_per_page'])) {
340       $block->setConfigurationValue('items_per_page', $items_per_page);
341     }
342     $form_state->unsetValue(['override', 'items_per_page']);
343   }
344
345   /**
346    * Allows to change the display settings right before executing the block.
347    *
348    * @param \Drupal\views\Plugin\Block\ViewsBlock $block
349    *   The block plugin for views displays.
350    */
351   public function preBlockBuild(ViewsBlock $block) {
352     $config = $block->getConfiguration();
353     if ($config['items_per_page'] !== 'none') {
354       $this->view->setItemsPerPage($config['items_per_page']);
355     }
356   }
357
358   /**
359    * Block views use exposed widgets only if AJAX is set.
360    */
361   public function usesExposed() {
362     if ($this->ajaxEnabled()) {
363       return parent::usesExposed();
364     }
365     return FALSE;
366   }
367
368   /**
369    * {@inheritdoc}
370    */
371   public function remove() {
372     parent::remove();
373
374     if ($this->entityManager->hasDefinition('block')) {
375       $plugin_id = 'views_block:' . $this->view->storage->id() . '-' . $this->display['id'];
376       foreach ($this->entityManager->getStorage('block')->loadByProperties(['plugin' => $plugin_id]) as $block) {
377         $block->delete();
378       }
379     }
380     if ($this->blockManager instanceof CachedDiscoveryInterface) {
381       $this->blockManager->clearCachedDefinitions();
382     }
383   }
384
385 }