3 namespace Drupal\views\Plugin\Block;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Block\BlockBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\views\ViewExecutableFactory;
10 use Drupal\Core\Entity\EntityStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Drupal\Core\Session\AccountInterface;
15 * Base class for Views block plugins.
17 abstract class ViewsBlockBase extends BlockBase implements ContainerFactoryPluginInterface {
20 * The View executable object.
22 * @var \Drupal\views\ViewExecutable
27 * The display ID being used for this View.
34 * Indicates whether the display was successfully set.
38 protected $displaySet;
43 * @var \Drupal\Core\Session\AccountInterface
48 * Constructs a \Drupal\views\Plugin\Block\ViewsBlockBase object.
50 * @param array $configuration
51 * A configuration array containing information about the plugin instance.
52 * @param string $plugin_id
53 * The plugin_id for the plugin instance.
54 * @param mixed $plugin_definition
55 * The plugin implementation definition.
56 * @param \Drupal\views\ViewExecutableFactory $executable_factory
57 * The view executable factory.
58 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
60 * @param \Drupal\Core\Session\AccountInterface $user
63 public function __construct(array $configuration, $plugin_id, $plugin_definition, ViewExecutableFactory $executable_factory, EntityStorageInterface $storage, AccountInterface $user) {
64 $this->pluginId = $plugin_id;
65 $delta = $this->getDerivativeId();
66 list($name, $this->displayID) = explode('-', $delta, 2);
68 $view = $storage->load($name);
69 $this->view = $executable_factory->get($view);
70 $this->displaySet = $this->view->setDisplay($this->displayID);
73 parent::__construct($configuration, $plugin_id, $plugin_definition);
79 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
81 $configuration, $plugin_id, $plugin_definition,
82 $container->get('views.executable'),
83 $container->get('entity.manager')->getStorage('view'),
84 $container->get('current_user')
91 protected function blockAccess(AccountInterface $account) {
92 if ($this->view->access($this->displayID)) {
93 $access = AccessResult::allowed();
96 $access = AccessResult::forbidden();
104 public function defaultConfiguration() {
105 return ['views_label' => ''];
111 public function getPreviewFallbackString() {
112 return $this->t('Placeholder for the "@view" views block', ['@view' => $this->view->storage->label()]);
118 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
119 $form = parent::buildConfigurationForm($form, $form_state);
121 // Set the default label to '' so the views internal title is used.
122 $form['label']['#default_value'] = '';
123 $form['label']['#access'] = FALSE;
125 // Unset the machine_name provided by BlockForm.
126 unset($form['id']['#machine_name']['source']);
127 // Prevent users from changing the auto-generated block machine_name.
128 $form['id']['#access'] = FALSE;
129 $form['#pre_render'][] = '\Drupal\views\Plugin\views\PluginBase::preRenderAddFieldsetMarkup';
131 // Allow to override the label on the actual page.
132 $form['views_label_checkbox'] = [
133 '#type' => 'checkbox',
134 '#title' => $this->t('Override title'),
135 '#default_value' => !empty($this->configuration['views_label']),
138 $form['views_label_fieldset'] = [
139 '#type' => 'fieldset',
143 ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
149 $form['views_label'] = [
150 '#title' => $this->t('Title'),
151 '#type' => 'textfield',
152 '#default_value' => $this->configuration['views_label'] ?: $this->view->getTitle(),
156 ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
160 '#fieldset' => 'views_label_fieldset',
163 if ($this->view->storage->access('edit') && \Drupal::moduleHandler()->moduleExists('views_ui')) {
164 $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' => \Drupal::url('entity.view.edit_display_form', ['view' => $this->view->storage->id(), 'display_id' => $this->displayID]), '@name' => $this->view->storage->label()]);
167 $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore.');
176 public function blockSubmit($form, FormStateInterface $form_state) {
177 if (!$form_state->isValueEmpty('views_label_checkbox')) {
178 $this->configuration['views_label'] = $form_state->getValue('views_label');
181 $this->configuration['views_label'] = '';
183 $form_state->unsetValue('views_label_checkbox');
187 * Converts Views block content to a renderable array with contextual links.
189 * @param string|array $output
190 * An string|array representing the block. This will be modified to be a
191 * renderable array, containing the optional '#contextual_links' property (if
192 * there are any contextual links associated with the block).
193 * @param string $block_type
194 * The type of the block. If it's 'block' it's a regular views display,
195 * but 'exposed_filter' exist as well.
197 protected function addContextualLinks(&$output, $block_type = 'block') {
198 // Do not add contextual links to an empty block.
199 if (!empty($output)) {
200 // Contextual links only work on blocks whose content is a renderable
201 // array, so if the block contains a string of already-rendered markup,
202 // convert it to an array.
203 if (is_string($output)) {
204 $output = ['#markup' => $output];
207 // views_add_contextual_links() needs the following information in
208 // order to be attached to the view.
209 $output['#view_id'] = $this->view->storage->id();
210 $output['#view_display_show_admin_links'] = $this->view->getShowAdminLinks();
211 $output['#view_display_plugin_id'] = $this->view->display_handler->getPluginId();
212 views_add_contextual_links($output, $block_type, $this->displayID);