Block.php 10.4 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\display\Block.
6
 * Definition of Drupal\block\Plugin\views\display\Block.
merlinofchaos's avatar
merlinofchaos committed
7
8
 */

9
namespace Drupal\block\Plugin\views\display;
10

11
use Drupal\views\Annotation\ViewsDisplay;
12
use Drupal\Core\Annotation\Translation;
13
use Drupal\views\Plugin\Block\ViewsBlock;
14
use Drupal\views\Plugin\views\display\DisplayPluginBase;
15

merlinofchaos's avatar
merlinofchaos committed
16
17
18
19
/**
 * The plugin that handles a block.
 *
 * @ingroup views_display_plugins
20
 *
21
 * @ViewsDisplay(
aspilicious's avatar
aspilicious committed
22
 *   id = "block",
23
24
25
 *   title = @Translation("Block"),
 *   help = @Translation("Display the view as a block."),
 *   theme = "views_view",
26
 *   register_theme = FALSE,
27
28
 *   uses_hook_block = TRUE,
 *   contextual_links_locations = {"block"},
29
 *   admin = @Translation("Block")
30
 * )
31
32
33
 *
 * @see \Drupal\views\Plugin\block\block\ViewsBlock
 * @see \Drupal\views\Plugin\Derivative\ViewsBlock
34
 */
35
class Block extends DisplayPluginBase {
36

37
38
39
40
41
42
43
  /**
   * Whether the display allows attachments.
   *
   * @var bool
   */
  protected $usesAttachments = TRUE;

44
45
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
46
47
48
49

    $options['block_description'] = array('default' => '', 'translatable' => TRUE);
    $options['block_caching'] = array('default' => DRUPAL_NO_CACHE);

50
51
52
53
54
55
    $options['allow'] = array(
      'contains' => array(
        'items_per_page' => array('default' => 'items_per_page'),
      ),
    );

merlinofchaos's avatar
merlinofchaos committed
56
57
58
    return $options;
  }

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  /**
   * Returns plugin-specific settings for the block.
   *
   * @param array $settings
   *   The settings of the block.
   *
   * @return array
   *   An array of block-specific settings to override the defaults provided in
   *   \Drupal\views\Plugin\Block\ViewsBlock::settings().
   *
   * @see \Drupal\views\Plugin\Block\ViewsBlock::settings().
   */
  public function blockSettings(array $settings) {
    $settings['items_per_page'] = 'none';
    return $settings;
  }

merlinofchaos's avatar
merlinofchaos committed
76
77
78
  /**
   * The display block handler returns the structure necessary for a block.
   */
79
  public function execute() {
merlinofchaos's avatar
merlinofchaos committed
80
81
    // Prior to this being called, the $view should already be set to this
    // display, and arguments should be set on the view.
82
    $element = $this->view->render();
83
    if (!empty($this->view->result) || $this->getOption('empty') || !empty($this->view->style_plugin->definition['even empty'])) {
84
      return $element;
merlinofchaos's avatar
merlinofchaos committed
85
    }
86
87

    return array();
merlinofchaos's avatar
merlinofchaos committed
88
89
90
91
92
93
94
  }

  /**
   * Provide the summary for page options in the views UI.
   *
   * This output is returned as an array.
   */
95
96
  public function optionsSummary(&$categories, &$options) {
    parent::optionsSummary($categories, $options);
merlinofchaos's avatar
merlinofchaos committed
97
98
99
100
101
102
103
104
105

    $categories['block'] = array(
      'title' => t('Block settings'),
      'column' => 'second',
      'build' => array(
        '#weight' => -10,
      ),
    );

106
    $block_description = strip_tags($this->getOption('block_description'));
merlinofchaos's avatar
merlinofchaos committed
107
108
109
110
111
112
113
114
115
116
    if (empty($block_description)) {
      $block_description = t('None');
    }

    $options['block_description'] = array(
      'category' => 'block',
      'title' => t('Block name'),
      'value' => views_ui_truncate($block_description, 24),
    );

117
118
119
120
121
122
123
124
    $filtered_allow = array_filter($this->getOption('allow'));

    $options['allow'] = array(
      'category' => 'block',
      'title' => t('Allow settings'),
      'value' => empty($filtered_allow) ? t('None') : t('Items per page'),
    );

125
    $types = $this->blockCachingModes();
merlinofchaos's avatar
merlinofchaos committed
126
127
128
    $options['block_caching'] = array(
      'category' => 'other',
      'title' => t('Block caching'),
129
      'value' => $types[$this->getCacheType()],
merlinofchaos's avatar
merlinofchaos committed
130
131
132
133
134
135
    );
  }

  /**
   * Provide a list of core's block caching modes.
   */
136
  protected function blockCachingModes() {
merlinofchaos's avatar
merlinofchaos committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    return array(
      DRUPAL_NO_CACHE => t('Do not cache'),
      DRUPAL_CACHE_GLOBAL => t('Cache once for everything (global)'),
      DRUPAL_CACHE_PER_PAGE => t('Per page'),
      DRUPAL_CACHE_PER_ROLE => t('Per role'),
      DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE => t('Per role per page'),
      DRUPAL_CACHE_PER_USER => t('Per user'),
      DRUPAL_CACHE_PER_USER | DRUPAL_CACHE_PER_PAGE => t('Per user per page'),
    );
  }

  /**
   * Provide a single method to figure caching type, keeping a sensible default
   * for when it's unset.
   */
152
  public function getCacheType() {
153
    $cache_type = $this->getOption('block_caching');
merlinofchaos's avatar
merlinofchaos committed
154
155
156
157
158
159
160
161
162
    if (empty($cache_type)) {
      $cache_type = DRUPAL_NO_CACHE;
    }
    return $cache_type;
  }

  /**
   * Provide the default form for setting options.
   */
163
164
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
165
166
167
168
169
170
171

    switch ($form_state['section']) {
      case 'block_description':
        $form['#title'] .= t('Block admin description');
        $form['block_description'] = array(
          '#type' => 'textfield',
          '#description' => t('This will appear as the name of this block in administer >> structure >> blocks.'),
172
          '#default_value' => $this->getOption('block_description'),
merlinofchaos's avatar
merlinofchaos committed
173
174
175
176
177
178
179
180
        );
        break;
      case 'block_caching':
        $form['#title'] .= t('Block caching type');

        $form['block_caching'] = array(
          '#type' => 'radios',
          '#description' => t("This sets the default status for Drupal's built-in block caching method; this requires that caching be turned on in block administration, and be careful because you have little control over when this cache is flushed."),
181
182
          '#options' => $this->blockCachingModes(),
          '#default_value' => $this->getCacheType(),
merlinofchaos's avatar
merlinofchaos committed
183
184
        );
        break;
185
      case 'exposed_form_options':
186
        $this->view->initHandlers();
187
        if (!$this->usesExposed() && parent::usesExposed()) {
188
189
          $form['exposed_form_options']['warning'] = array(
            '#weight' => -10,
190
            '#markup' => '<div class="messages messages--warning">' . t('Exposed filters in block displays require "Use AJAX" to be set to work correctly.') . '</div>',
191
192
          );
        }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
        break;
      case 'allow':
        $form['#title'] .= t('Allow settings in the block configuration');

        $options = array(
          'items_per_page' => t('Items per page'),
        );

        $allow = array_filter($this->getOption('allow'));
        $form['allow'] = array(
          '#type' => 'checkboxes',
          '#default_value' => $allow,
          '#options' => $options,
        );
        break;
merlinofchaos's avatar
merlinofchaos committed
208
209
210
211
212
213
214
    }
  }

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
215
216
  public function submitOptionsForm(&$form, &$form_state) {
    parent::submitOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
217
218
219
    switch ($form_state['section']) {
      case 'block_description':
      case 'block_caching':
220
221
      case 'allow':
        $this->setOption($form_state['section'], $form_state['values'][$form_state['section']]);
merlinofchaos's avatar
merlinofchaos committed
222
223
224
225
        break;
    }
  }

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  /**
   * Adds the configuration form elements specific to this views block plugin.
   *
   * This method allows block instances to override the views items_per_page.
   *
   * @param \Drupal\views\Plugin\Block\ViewsBlock $block
   *   The ViewsBlock plugin.
   * @param array $form
   *   The form definition array for the block configuration form.
   * @param array $form_state
   *   An array containing the current state of the configuration form.
   *
   * @return array $form
   *   The renderable form array representing the entire configuration form.
   *
   * @see \Drupal\views\Plugin\Block\ViewsBlock::blockForm()
   */
  public function blockForm(ViewsBlock $block, array &$form, array &$form_state) {
    $allow_settings = array_filter($this->getOption('allow'));

246
    $block_configuration = $block->getConfiguration();
247
248
249
250
251
252
253
254
255

    foreach ($allow_settings as $type => $enabled) {
      if (empty($enabled)) {
        continue;
      }
      switch ($type) {
        case 'items_per_page':
          $form['override']['items_per_page'] = array(
            '#type' => 'select',
256
            '#title' => t('Items per block'),
257
            '#options' => array(
258
              'none' => t('@count (default setting)', array('@count' => $this->getPlugin('pager')->getItemsPerPage())),
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
              5 => 5,
              10 => 10,
              20 => 20,
              40 => 40,
            ),
            '#default_value' => $block_configuration['items_per_page'],
          );
          break;
      }
    }

    return $form;
  }

  /**
   * Handles form validation for the views block configuration form.
   *
   * @param \Drupal\views\Plugin\Block\ViewsBlock $block
   *   The ViewsBlock plugin.
   * @param array $form
   *   The form definition array for the block configuration form.
   * @param array $form_state
   *   An array containing the current state of the configuration form.
   *
   * @see \Drupal\views\Plugin\Block\ViewsBlock::blockValidate()
   */
  public function blockValidate(ViewsBlock $block, array $form, array &$form_state) {
  }

  /**
   * Handles form submission for the views block configuration form.
   *
   * @param \Drupal\views\Plugin\Block\ViewsBlock $block
   *   The ViewsBlock plugin.
   * @param array $form
   *   The form definition array for the full block configuration form.
   * @param array $form_state
   *   An array containing the current state of the configuration form.
   *
   * * @see \Drupal\views\Plugin\Block\ViewsBlock::blockSubmit()
   */
  public function blockSubmit(ViewsBlock $block, $form, &$form_state) {
    if (isset($form_state['values']['override']['items_per_page'])) {
302
      $block->setConfigurationValue('items_per_page', $form_state['values']['override']['items_per_page']);
303
304
305
306
307
308
309
310
311
312
    }
  }

  /**
   * Allows to change the display settings right before executing the block.
   *
   * @param \Drupal\views\Plugin\Block\ViewsBlock $block
   *   The block plugin for views displays.
   */
  public function preBlockBuild(ViewsBlock $block) {
313
    $config = $block->getConfiguration();
314
315
316
317
318
    if ($config['items_per_page'] !== 'none') {
      $this->view->setItemsPerPage($config['items_per_page']);
    }
  }

merlinofchaos's avatar
merlinofchaos committed
319
320
321
  /**
   * Block views use exposed widgets only if AJAX is set.
   */
322
  public function usesExposed() {
323
      if ($this->ajaxEnabled()) {
324
        return parent::usesExposed();
merlinofchaos's avatar
merlinofchaos committed
325
326
327
328
      }
      return FALSE;
    }

329
330
331
332
333
334
335
336
337
338
339
340
  /**
   * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::remove().
   */
  public function remove() {
    parent::remove();

    $plugin_id = 'views_block:' . $this->view->storage->id() . '-' . $this->display['id'];
    foreach (entity_load_multiple_by_properties('block', array('plugin' => $plugin_id)) as $block) {
      $block->delete();
    }
  }

merlinofchaos's avatar
merlinofchaos committed
341
}