Block.php 5.94 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\Component\Annotation\Plugin;
12
use Drupal\Core\Annotation\Translation;
13
use Drupal\views\Plugin\views\display\DisplayPluginBase;
14

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

33 34 35 36 37 38 39
  /**
   * Whether the display allows attachments.
   *
   * @var bool
   */
  protected $usesAttachments = TRUE;

40 41
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
42 43 44 45 46 47 48 49 50 51

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

    return $options;
  }

  /**
   * The display block handler returns the structure necessary for a block.
   */
52
  public function execute() {
merlinofchaos's avatar
merlinofchaos committed
53 54
    // Prior to this being called, the $view should already be set to this
    // display, and arguments should be set on the view.
55
    $element = $this->view->render();
56
    if (!empty($this->view->result) || $this->getOption('empty') || !empty($this->view->style_plugin->definition['even empty'])) {
57
      return drupal_render($element);
merlinofchaos's avatar
merlinofchaos committed
58 59 60 61 62 63 64 65
    }
  }

  /**
   * Provide the summary for page options in the views UI.
   *
   * This output is returned as an array.
   */
66 67
  public function optionsSummary(&$categories, &$options) {
    parent::optionsSummary($categories, $options);
merlinofchaos's avatar
merlinofchaos committed
68 69 70 71 72 73 74 75 76

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

77
    $block_description = strip_tags($this->getOption('block_description'));
merlinofchaos's avatar
merlinofchaos committed
78 79 80 81 82 83 84 85 86 87
    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),
    );

88
    $types = $this->blockCachingModes();
merlinofchaos's avatar
merlinofchaos committed
89 90 91
    $options['block_caching'] = array(
      'category' => 'other',
      'title' => t('Block caching'),
92
      'value' => $types[$this->getCacheType()],
merlinofchaos's avatar
merlinofchaos committed
93 94 95 96 97 98
    );
  }

  /**
   * Provide a list of core's block caching modes.
   */
99
  protected function blockCachingModes() {
merlinofchaos's avatar
merlinofchaos committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    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.
   */
115
  public function getCacheType() {
116
    $cache_type = $this->getOption('block_caching');
merlinofchaos's avatar
merlinofchaos committed
117 118 119 120 121 122 123 124 125
    if (empty($cache_type)) {
      $cache_type = DRUPAL_NO_CACHE;
    }
    return $cache_type;
  }

  /**
   * Provide the default form for setting options.
   */
126 127
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
128 129 130 131 132 133 134

    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.'),
135
          '#default_value' => $this->getOption('block_description'),
merlinofchaos's avatar
merlinofchaos committed
136 137 138 139 140 141 142 143
        );
        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."),
144 145
          '#options' => $this->blockCachingModes(),
          '#default_value' => $this->getCacheType(),
merlinofchaos's avatar
merlinofchaos committed
146 147
        );
        break;
148
      case 'exposed_form_options':
149
        $this->view->initHandlers();
150
        if (!$this->usesExposed() && parent::usesExposed()) {
151 152
          $form['exposed_form_options']['warning'] = array(
            '#weight' => -10,
153
            '#markup' => '<div class="messages messages--warning">' . t('Exposed filters in block displays require "Use AJAX" to be set to work correctly.') . '</div>',
154 155
          );
        }
merlinofchaos's avatar
merlinofchaos committed
156 157 158 159 160 161 162
    }
  }

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
163 164
  public function submitOptionsForm(&$form, &$form_state) {
    parent::submitOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
165 166
    switch ($form_state['section']) {
      case 'block_description':
167
        $this->setOption('block_description', $form_state['values']['block_description']);
merlinofchaos's avatar
merlinofchaos committed
168 169
        break;
      case 'block_caching':
170
        $this->setOption('block_caching', $form_state['values']['block_caching']);
merlinofchaos's avatar
merlinofchaos committed
171 172 173 174 175 176 177
        break;
    }
  }

  /**
   * Block views use exposed widgets only if AJAX is set.
   */
178
  public function usesExposed() {
179
      if ($this->ajaxEnabled()) {
180
        return parent::usesExposed();
merlinofchaos's avatar
merlinofchaos committed
181 182 183 184
      }
      return FALSE;
    }

185 186 187 188 189 190 191 192 193 194 195 196
  /**
   * 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
197
}