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 buildConfigurationForm(array $form, FormStateInterface $form_state) {
112 $form = parent::buildConfigurationForm($form, $form_state);
114 // Set the default label to '' so the views internal title is used.
115 $form['label']['#default_value'] = '';
116 $form['label']['#access'] = FALSE;
118 // Unset the machine_name provided by BlockForm.
119 unset($form['id']['#machine_name']['source']);
120 // Prevent users from changing the auto-generated block machine_name.
121 $form['id']['#access'] = FALSE;
122 $form['#pre_render'][] = '\Drupal\views\Plugin\views\PluginBase::preRenderAddFieldsetMarkup';
124 // Allow to override the label on the actual page.
125 $form['views_label_checkbox'] = [
126 '#type' => 'checkbox',
127 '#title' => $this->t('Override title'),
128 '#default_value' => !empty($this->configuration['views_label']),
131 $form['views_label_fieldset'] = [
132 '#type' => 'fieldset',
136 ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
142 $form['views_label'] = [
143 '#title' => $this->t('Title'),
144 '#type' => 'textfield',
145 '#default_value' => $this->configuration['views_label'] ?: $this->view->getTitle(),
149 ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
153 '#fieldset' => 'views_label_fieldset',
156 if ($this->view->storage->access('edit') && \Drupal::moduleHandler()->moduleExists('views_ui')) {
157 $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()]);
160 $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore.');
169 public function blockSubmit($form, FormStateInterface $form_state) {
170 if (!$form_state->isValueEmpty('views_label_checkbox')) {
171 $this->configuration['views_label'] = $form_state->getValue('views_label');
174 $this->configuration['views_label'] = '';
176 $form_state->unsetValue('views_label_checkbox');
180 * Converts Views block content to a renderable array with contextual links.
182 * @param string|array $output
183 * An string|array representing the block. This will be modified to be a
184 * renderable array, containing the optional '#contextual_links' property (if
185 * there are any contextual links associated with the block).
186 * @param string $block_type
187 * The type of the block. If it's 'block' it's a regular views display,
188 * but 'exposed_filter' exist as well.
190 protected function addContextualLinks(&$output, $block_type = 'block') {
191 // Do not add contextual links to an empty block.
192 if (!empty($output)) {
193 // Contextual links only work on blocks whose content is a renderable
194 // array, so if the block contains a string of already-rendered markup,
195 // convert it to an array.
196 if (is_string($output)) {
197 $output = ['#markup' => $output];
200 // views_add_contextual_links() needs the following information in
201 // order to be attached to the view.
202 $output['#view_id'] = $this->view->storage->id();
203 $output['#view_display_show_admin_links'] = $this->view->getShowAdminLinks();
204 $output['#view_display_plugin_id'] = $this->view->display_handler->getPluginId();
205 views_add_contextual_links($output, $block_type, $this->displayID);