Mercurial > hg > isophonics-drupal-site
comparison core/modules/views/src/Plugin/Block/ViewsBlock.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\views\Plugin\Block; | |
4 | |
5 use Drupal\Component\Utility\Xss; | |
6 use Drupal\Core\Form\FormStateInterface; | |
7 use Drupal\views\Element\View; | |
8 use Drupal\Core\Entity\EntityInterface; | |
9 | |
10 /** | |
11 * Provides a generic Views block. | |
12 * | |
13 * @Block( | |
14 * id = "views_block", | |
15 * admin_label = @Translation("Views Block"), | |
16 * deriver = "Drupal\views\Plugin\Derivative\ViewsBlock" | |
17 * ) | |
18 */ | |
19 class ViewsBlock extends ViewsBlockBase { | |
20 | |
21 /** | |
22 * {@inheritdoc} | |
23 */ | |
24 public function build() { | |
25 $this->view->display_handler->preBlockBuild($this); | |
26 | |
27 $args = []; | |
28 foreach ($this->view->display_handler->getHandlers('argument') as $argument_name => $argument) { | |
29 // Initialize the argument value. Work around a limitation in | |
30 // \Drupal\views\ViewExecutable::_buildArguments() that skips processing | |
31 // later arguments if an argument with default action "ignore" and no | |
32 // argument is provided. | |
33 $args[$argument_name] = $argument->options['default_action'] == 'ignore' ? 'all' : NULL; | |
34 | |
35 if (!empty($this->context[$argument_name])) { | |
36 if ($value = $this->context[$argument_name]->getContextValue()) { | |
37 | |
38 // Context values are often entities, but views arguments expect to | |
39 // receive just the entity ID, convert it. | |
40 if ($value instanceof EntityInterface) { | |
41 $value = $value->id(); | |
42 } | |
43 $args[$argument_name] = $value; | |
44 } | |
45 } | |
46 } | |
47 | |
48 // We ask ViewExecutable::buildRenderable() to avoid creating a render cache | |
49 // entry for the view output by passing FALSE, because we're going to cache | |
50 // the whole block instead. | |
51 if ($output = $this->view->buildRenderable($this->displayID, array_values($args), FALSE)) { | |
52 // Before returning the block output, convert it to a renderable array | |
53 // with contextual links. | |
54 $this->addContextualLinks($output); | |
55 | |
56 // Block module expects to get a final render array, without another | |
57 // top-level #pre_render callback. So, here we make sure that Views' | |
58 // #pre_render callback has already been applied. | |
59 $output = View::preRenderViewElement($output); | |
60 | |
61 // Override the label to the dynamic title configured in the view. | |
62 if (empty($this->configuration['views_label']) && $this->view->getTitle()) { | |
63 $output['#title'] = ['#markup' => $this->view->getTitle(), '#allowed_tags' => Xss::getHtmlTagList()]; | |
64 } | |
65 | |
66 // When view_build is empty, the actual render array output for this View | |
67 // is going to be empty. In that case, return just #cache, so that the | |
68 // render system knows the reasons (cache contexts & tags) why this Views | |
69 // block is empty, and can cache it accordingly. | |
70 if (empty($output['view_build'])) { | |
71 $output = ['#cache' => $output['#cache']]; | |
72 } | |
73 | |
74 return $output; | |
75 } | |
76 | |
77 return []; | |
78 } | |
79 | |
80 /** | |
81 * {@inheritdoc} | |
82 */ | |
83 public function getConfiguration() { | |
84 $configuration = parent::getConfiguration(); | |
85 | |
86 // Set the label to the static title configured in the view. | |
87 if (!empty($configuration['views_label'])) { | |
88 $configuration['label'] = $configuration['views_label']; | |
89 } | |
90 | |
91 return $configuration; | |
92 } | |
93 | |
94 /** | |
95 * {@inheritdoc} | |
96 */ | |
97 public function defaultConfiguration() { | |
98 $settings = parent::defaultConfiguration(); | |
99 | |
100 if ($this->displaySet) { | |
101 $settings += $this->view->display_handler->blockSettings($settings); | |
102 } | |
103 | |
104 // Set custom cache settings. | |
105 if (isset($this->pluginDefinition['cache'])) { | |
106 $settings['cache'] = $this->pluginDefinition['cache']; | |
107 } | |
108 | |
109 return $settings; | |
110 } | |
111 | |
112 /** | |
113 * {@inheritdoc} | |
114 */ | |
115 public function blockForm($form, FormStateInterface $form_state) { | |
116 if ($this->displaySet) { | |
117 return $this->view->display_handler->blockForm($this, $form, $form_state); | |
118 } | |
119 | |
120 return []; | |
121 } | |
122 | |
123 /** | |
124 * {@inheritdoc} | |
125 */ | |
126 public function blockValidate($form, FormStateInterface $form_state) { | |
127 if ($this->displaySet) { | |
128 $this->view->display_handler->blockValidate($this, $form, $form_state); | |
129 } | |
130 } | |
131 | |
132 /** | |
133 * {@inheritdoc} | |
134 */ | |
135 public function blockSubmit($form, FormStateInterface $form_state) { | |
136 parent::blockSubmit($form, $form_state); | |
137 if ($this->displaySet) { | |
138 $this->view->display_handler->blockSubmit($this, $form, $form_state); | |
139 } | |
140 } | |
141 | |
142 /** | |
143 * {@inheritdoc} | |
144 */ | |
145 public function getMachineNameSuggestion() { | |
146 $this->view->setDisplay($this->displayID); | |
147 return 'views_block__' . $this->view->storage->id() . '_' . $this->view->current_display; | |
148 } | |
149 | |
150 } |