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

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\views\Plugin\Block;
Chris@0 4
Chris@18 5 use Drupal\Core\Url;
Chris@0 6 use Drupal\Core\Access\AccessResult;
Chris@0 7 use Drupal\Core\Block\BlockBase;
Chris@0 8 use Drupal\Core\Form\FormStateInterface;
Chris@0 9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Chris@0 10 use Drupal\views\ViewExecutableFactory;
Chris@0 11 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 12 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 13 use Drupal\Core\Session\AccountInterface;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Base class for Views block plugins.
Chris@0 17 */
Chris@0 18 abstract class ViewsBlockBase extends BlockBase implements ContainerFactoryPluginInterface {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * The View executable object.
Chris@0 22 *
Chris@0 23 * @var \Drupal\views\ViewExecutable
Chris@0 24 */
Chris@0 25 protected $view;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * The display ID being used for this View.
Chris@0 29 *
Chris@0 30 * @var string
Chris@0 31 */
Chris@0 32 protected $displayID;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Indicates whether the display was successfully set.
Chris@0 36 *
Chris@0 37 * @var bool
Chris@0 38 */
Chris@0 39 protected $displaySet;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * The current user.
Chris@0 43 *
Chris@0 44 * @var \Drupal\Core\Session\AccountInterface
Chris@0 45 */
Chris@0 46 protected $user;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Constructs a \Drupal\views\Plugin\Block\ViewsBlockBase object.
Chris@0 50 *
Chris@0 51 * @param array $configuration
Chris@0 52 * A configuration array containing information about the plugin instance.
Chris@0 53 * @param string $plugin_id
Chris@0 54 * The plugin_id for the plugin instance.
Chris@0 55 * @param mixed $plugin_definition
Chris@0 56 * The plugin implementation definition.
Chris@0 57 * @param \Drupal\views\ViewExecutableFactory $executable_factory
Chris@0 58 * The view executable factory.
Chris@0 59 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
Chris@0 60 * The views storage.
Chris@0 61 * @param \Drupal\Core\Session\AccountInterface $user
Chris@0 62 * The current user.
Chris@0 63 */
Chris@0 64 public function __construct(array $configuration, $plugin_id, $plugin_definition, ViewExecutableFactory $executable_factory, EntityStorageInterface $storage, AccountInterface $user) {
Chris@0 65 $this->pluginId = $plugin_id;
Chris@0 66 $delta = $this->getDerivativeId();
Chris@0 67 list($name, $this->displayID) = explode('-', $delta, 2);
Chris@0 68 // Load the view.
Chris@0 69 $view = $storage->load($name);
Chris@0 70 $this->view = $executable_factory->get($view);
Chris@0 71 $this->displaySet = $this->view->setDisplay($this->displayID);
Chris@0 72 $this->user = $user;
Chris@0 73
Chris@0 74 parent::__construct($configuration, $plugin_id, $plugin_definition);
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * {@inheritdoc}
Chris@0 79 */
Chris@0 80 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
Chris@0 81 return new static(
Chris@0 82 $configuration, $plugin_id, $plugin_definition,
Chris@0 83 $container->get('views.executable'),
Chris@0 84 $container->get('entity.manager')->getStorage('view'),
Chris@0 85 $container->get('current_user')
Chris@0 86 );
Chris@0 87 }
Chris@0 88
Chris@0 89 /**
Chris@0 90 * {@inheritdoc}
Chris@0 91 */
Chris@0 92 protected function blockAccess(AccountInterface $account) {
Chris@0 93 if ($this->view->access($this->displayID)) {
Chris@0 94 $access = AccessResult::allowed();
Chris@0 95 }
Chris@0 96 else {
Chris@0 97 $access = AccessResult::forbidden();
Chris@0 98 }
Chris@0 99 return $access;
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * {@inheritdoc}
Chris@0 104 */
Chris@0 105 public function defaultConfiguration() {
Chris@0 106 return ['views_label' => ''];
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * {@inheritdoc}
Chris@0 111 */
Chris@17 112 public function getPreviewFallbackString() {
Chris@18 113 return $this->t('"@view" views block', ['@view' => $this->view->storage->label()]);
Chris@17 114 }
Chris@17 115
Chris@17 116 /**
Chris@17 117 * {@inheritdoc}
Chris@17 118 */
Chris@0 119 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
Chris@0 120 $form = parent::buildConfigurationForm($form, $form_state);
Chris@0 121
Chris@0 122 // Set the default label to '' so the views internal title is used.
Chris@0 123 $form['label']['#default_value'] = '';
Chris@0 124 $form['label']['#access'] = FALSE;
Chris@0 125
Chris@0 126 // Unset the machine_name provided by BlockForm.
Chris@0 127 unset($form['id']['#machine_name']['source']);
Chris@0 128 // Prevent users from changing the auto-generated block machine_name.
Chris@0 129 $form['id']['#access'] = FALSE;
Chris@0 130 $form['#pre_render'][] = '\Drupal\views\Plugin\views\PluginBase::preRenderAddFieldsetMarkup';
Chris@0 131
Chris@0 132 // Allow to override the label on the actual page.
Chris@0 133 $form['views_label_checkbox'] = [
Chris@0 134 '#type' => 'checkbox',
Chris@0 135 '#title' => $this->t('Override title'),
Chris@0 136 '#default_value' => !empty($this->configuration['views_label']),
Chris@0 137 ];
Chris@0 138
Chris@0 139 $form['views_label_fieldset'] = [
Chris@0 140 '#type' => 'fieldset',
Chris@0 141 '#states' => [
Chris@0 142 'visible' => [
Chris@0 143 [
Chris@0 144 ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
Chris@0 145 ],
Chris@0 146 ],
Chris@0 147 ],
Chris@0 148 ];
Chris@0 149
Chris@0 150 $form['views_label'] = [
Chris@0 151 '#title' => $this->t('Title'),
Chris@0 152 '#type' => 'textfield',
Chris@0 153 '#default_value' => $this->configuration['views_label'] ?: $this->view->getTitle(),
Chris@0 154 '#states' => [
Chris@0 155 'visible' => [
Chris@0 156 [
Chris@0 157 ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
Chris@0 158 ],
Chris@0 159 ],
Chris@0 160 ],
Chris@0 161 '#fieldset' => 'views_label_fieldset',
Chris@0 162 ];
Chris@0 163
Chris@0 164 if ($this->view->storage->access('edit') && \Drupal::moduleHandler()->moduleExists('views_ui')) {
Chris@18 165 $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore. (Try changing it directly in <a href=":url">@name</a>.)', [':url' => Url::fromRoute('entity.view.edit_display_form', ['view' => $this->view->storage->id(), 'display_id' => $this->displayID])->toString(), '@name' => $this->view->storage->label()]);
Chris@0 166 }
Chris@0 167 else {
Chris@0 168 $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore.');
Chris@0 169 }
Chris@0 170
Chris@0 171 return $form;
Chris@0 172 }
Chris@0 173
Chris@0 174 /**
Chris@0 175 * {@inheritdoc}
Chris@0 176 */
Chris@0 177 public function blockSubmit($form, FormStateInterface $form_state) {
Chris@0 178 if (!$form_state->isValueEmpty('views_label_checkbox')) {
Chris@0 179 $this->configuration['views_label'] = $form_state->getValue('views_label');
Chris@0 180 }
Chris@0 181 else {
Chris@0 182 $this->configuration['views_label'] = '';
Chris@0 183 }
Chris@0 184 $form_state->unsetValue('views_label_checkbox');
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * Converts Views block content to a renderable array with contextual links.
Chris@0 189 *
Chris@0 190 * @param string|array $output
Chris@0 191 * An string|array representing the block. This will be modified to be a
Chris@0 192 * renderable array, containing the optional '#contextual_links' property (if
Chris@0 193 * there are any contextual links associated with the block).
Chris@0 194 * @param string $block_type
Chris@0 195 * The type of the block. If it's 'block' it's a regular views display,
Chris@0 196 * but 'exposed_filter' exist as well.
Chris@0 197 */
Chris@0 198 protected function addContextualLinks(&$output, $block_type = 'block') {
Chris@0 199 // Do not add contextual links to an empty block.
Chris@0 200 if (!empty($output)) {
Chris@0 201 // Contextual links only work on blocks whose content is a renderable
Chris@0 202 // array, so if the block contains a string of already-rendered markup,
Chris@0 203 // convert it to an array.
Chris@0 204 if (is_string($output)) {
Chris@0 205 $output = ['#markup' => $output];
Chris@0 206 }
Chris@0 207
Chris@0 208 // views_add_contextual_links() needs the following information in
Chris@0 209 // order to be attached to the view.
Chris@0 210 $output['#view_id'] = $this->view->storage->id();
Chris@0 211 $output['#view_display_show_admin_links'] = $this->view->getShowAdminLinks();
Chris@0 212 $output['#view_display_plugin_id'] = $this->view->display_handler->getPluginId();
Chris@0 213 views_add_contextual_links($output, $block_type, $this->displayID);
Chris@0 214 }
Chris@0 215 }
Chris@0 216
Chris@18 217 /**
Chris@18 218 * Gets the view executable.
Chris@18 219 *
Chris@18 220 * @return \Drupal\views\ViewExecutable
Chris@18 221 * The view executable.
Chris@18 222 *
Chris@18 223 * @todo revisit after https://www.drupal.org/node/3027653. This method was
Chris@18 224 * added in https://www.drupal.org/node/3002608, but should not be
Chris@18 225 * necessary once block plugins can determine if they are being previewed.
Chris@18 226 */
Chris@18 227 public function getViewExecutable() {
Chris@18 228 return $this->view;
Chris@18 229 }
Chris@18 230
Chris@0 231 }