3 namespace Drupal\views\Plugin\Block;
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Element\View;
8 use Drupal\Core\Entity\EntityInterface;
11 * Provides a generic Views block.
15 * admin_label = @Translation("Views Block"),
16 * deriver = "Drupal\views\Plugin\Derivative\ViewsBlock"
19 class ViewsBlock extends ViewsBlockBase {
24 public function build() {
25 $this->view->display_handler->preBlockBuild($this);
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;
35 if (!empty($this->context[$argument_name])) {
36 if ($value = $this->context[$argument_name]->getContextValue()) {
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();
43 $args[$argument_name] = $value;
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);
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);
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()];
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']];
83 public function getConfiguration() {
84 $configuration = parent::getConfiguration();
86 // Set the label to the static title configured in the view.
87 if (!empty($configuration['views_label'])) {
88 $configuration['label'] = $configuration['views_label'];
91 return $configuration;
97 public function defaultConfiguration() {
98 $settings = parent::defaultConfiguration();
100 if ($this->displaySet) {
101 $settings += $this->view->display_handler->blockSettings($settings);
104 // Set custom cache settings.
105 if (isset($this->pluginDefinition['cache'])) {
106 $settings['cache'] = $this->pluginDefinition['cache'];
115 public function blockForm($form, FormStateInterface $form_state) {
116 if ($this->displaySet) {
117 return $this->view->display_handler->blockForm($this, $form, $form_state);
126 public function blockValidate($form, FormStateInterface $form_state) {
127 if ($this->displaySet) {
128 $this->view->display_handler->blockValidate($this, $form, $form_state);
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);
145 public function getMachineNameSuggestion() {
146 $this->view->setDisplay($this->displayID);
147 return 'views_block__' . $this->view->storage->id() . '_' . $this->view->current_display;