annotate core/modules/views/src/Plugin/Block/ViewsBlock.php @ 19:fa3358dc1485 tip

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