Pull merge.
[yaffs-website] / web / core / modules / views / src / Plugin / Block / ViewsBlockBase.php
1 <?php
2
3 namespace Drupal\views\Plugin\Block;
4
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;
13
14 /**
15  * Base class for Views block plugins.
16  */
17 abstract class ViewsBlockBase extends BlockBase implements ContainerFactoryPluginInterface {
18
19   /**
20    * The View executable object.
21    *
22    * @var \Drupal\views\ViewExecutable
23    */
24   protected $view;
25
26   /**
27    * The display ID being used for this View.
28    *
29    * @var string
30    */
31   protected $displayID;
32
33   /**
34    * Indicates whether the display was successfully set.
35    *
36    * @var bool
37    */
38   protected $displaySet;
39
40   /**
41    * The current user.
42    *
43    * @var \Drupal\Core\Session\AccountInterface
44    */
45   protected $user;
46
47   /**
48    * Constructs a \Drupal\views\Plugin\Block\ViewsBlockBase object.
49    *
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
59    *   The views storage.
60    * @param \Drupal\Core\Session\AccountInterface $user
61    *   The current user.
62    */
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);
67     // Load the view.
68     $view = $storage->load($name);
69     $this->view = $executable_factory->get($view);
70     $this->displaySet = $this->view->setDisplay($this->displayID);
71     $this->user = $user;
72
73     parent::__construct($configuration, $plugin_id, $plugin_definition);
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
80     return new static(
81       $configuration, $plugin_id, $plugin_definition,
82       $container->get('views.executable'),
83       $container->get('entity.manager')->getStorage('view'),
84       $container->get('current_user')
85     );
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   protected function blockAccess(AccountInterface $account) {
92     if ($this->view->access($this->displayID)) {
93       $access = AccessResult::allowed();
94     }
95     else {
96       $access = AccessResult::forbidden();
97     }
98     return $access;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function defaultConfiguration() {
105     return ['views_label' => ''];
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function getPreviewFallbackString() {
112     return $this->t('Placeholder for the "@view" views block', ['@view' => $this->view->storage->label()]);
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
119     $form = parent::buildConfigurationForm($form, $form_state);
120
121     // Set the default label to '' so the views internal title is used.
122     $form['label']['#default_value'] = '';
123     $form['label']['#access'] = FALSE;
124
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';
130
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']),
136     ];
137
138     $form['views_label_fieldset'] = [
139       '#type' => 'fieldset',
140       '#states' => [
141         'visible' => [
142           [
143             ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
144           ],
145         ],
146       ],
147     ];
148
149     $form['views_label'] = [
150       '#title' => $this->t('Title'),
151       '#type' => 'textfield',
152       '#default_value' => $this->configuration['views_label'] ?: $this->view->getTitle(),
153       '#states' => [
154         'visible' => [
155           [
156             ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
157           ],
158         ],
159       ],
160       '#fieldset' => 'views_label_fieldset',
161     ];
162
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()]);
165     }
166     else {
167       $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore.');
168     }
169
170     return $form;
171   }
172
173   /**
174    * {@inheritdoc}
175    */
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');
179     }
180     else {
181       $this->configuration['views_label'] = '';
182     }
183     $form_state->unsetValue('views_label_checkbox');
184   }
185
186   /**
187    * Converts Views block content to a renderable array with contextual links.
188    *
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.
196    */
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];
205       }
206
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);
213     }
214   }
215
216 }