Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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 buildConfigurationForm(array $form, FormStateInterface $form_state) {
112     $form = parent::buildConfigurationForm($form, $form_state);
113
114     // Set the default label to '' so the views internal title is used.
115     $form['label']['#default_value'] = '';
116     $form['label']['#access'] = FALSE;
117
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';
123
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']),
129     ];
130
131     $form['views_label_fieldset'] = [
132       '#type' => 'fieldset',
133       '#states' => [
134         'visible' => [
135           [
136             ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
137           ],
138         ],
139       ],
140     ];
141
142     $form['views_label'] = [
143       '#title' => $this->t('Title'),
144       '#type' => 'textfield',
145       '#default_value' => $this->configuration['views_label'] ?: $this->view->getTitle(),
146       '#states' => [
147         'visible' => [
148           [
149             ':input[name="settings[views_label_checkbox]"]' => ['checked' => TRUE],
150           ],
151         ],
152       ],
153       '#fieldset' => 'views_label_fieldset',
154     ];
155
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()]);
158     }
159     else {
160       $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore.');
161     }
162
163     return $form;
164   }
165
166   /**
167    * {@inheritdoc}
168    */
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');
172     }
173     else {
174       $this->configuration['views_label'] = '';
175     }
176     $form_state->unsetValue('views_label_checkbox');
177   }
178
179   /**
180    * Converts Views block content to a renderable array with contextual links.
181    *
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.
189    */
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];
198       }
199
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);
206     }
207   }
208
209 }