Project

General

Profile

Paste
Download (7.77 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / plugins / views_plugin_display_block.inc @ d719f12f

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the block display plugin.
6
 */
7

    
8
/**
9
 * The plugin that handles a block.
10
 *
11
 * @ingroup views_display_plugins
12
 */
13
class views_plugin_display_block extends views_plugin_display {
14
  function option_definition() {
15
    $options = parent::option_definition();
16

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

    
20
    return $options;
21
  }
22

    
23
  /**
24
   * The default block handler doesn't support configurable items,
25
   * but extended block handlers might be able to do interesting
26
   * stuff with it.
27
   */
28
  function execute_hook_block_list($delta = 0, $edit = array()) {
29
    $delta = $this->view->name . '-' . $this->display->id;
30
    $desc = $this->get_option('block_description');
31

    
32
    if (empty($desc)) {
33
      if ($this->display->display_title == $this->definition['title']) {
34
        $desc = t('View: !view', array('!view' => $this->view->get_human_name()));
35
      }
36
      else {
37
        $desc = t('View: !view: !display', array('!view' => $this->view->get_human_name(), '!display' => $this->display->display_title));
38
      }
39
    }
40
    return array(
41
      $delta => array(
42
        'info' => $desc,
43
        'cache' => $this->get_cache_type()
44
      ),
45
    );
46
  }
47

    
48
  /**
49
   * The display block handler returns the structure necessary for a block.
50
   */
51
  function execute() {
52
    // Prior to this being called, the $view should already be set to this
53
    // display, and arguments should be set on the view.
54
    $info['content'] = $this->view->render();
55
    $info['subject'] = filter_xss_admin($this->view->get_title());
56
    if (!empty($this->view->result) || $this->get_option('empty') || !empty($this->view->style_plugin->definition['even empty'])) {
57
      return $info;
58
    }
59
  }
60

    
61
  /**
62
   * Provide the summary for page options in the views UI.
63
   *
64
   * This output is returned as an array.
65
   */
66
  function options_summary(&$categories, &$options) {
67
    // It is very important to call the parent function here:
68
    parent::options_summary($categories, $options);
69

    
70
    $categories['block'] = array(
71
      'title' => t('Block settings'),
72
      'column' => 'second',
73
      'build' => array(
74
        '#weight' => -10,
75
      ),
76
    );
77

    
78
    $block_description = strip_tags($this->get_option('block_description'));
79
    if (empty($block_description)) {
80
      $block_description = t('None');
81
    }
82

    
83
    $options['block_description'] = array(
84
      'category' => 'block',
85
      'title' => t('Block name'),
86
      'value' => views_ui_truncate($block_description, 24),
87
    );
88

    
89
    $types = $this->block_caching_modes();
90
    $options['block_caching'] = array(
91
      'category' => 'other',
92
      'title' => t('Block caching'),
93
      'value' => $types[$this->get_cache_type()],
94
    );
95
  }
96

    
97
  /**
98
   * Provide a list of core's block caching modes.
99
   */
100
  function block_caching_modes() {
101
    return array(
102
      DRUPAL_NO_CACHE => t('Do not cache'),
103
      DRUPAL_CACHE_GLOBAL => t('Cache once for everything (global)'),
104
      DRUPAL_CACHE_PER_PAGE => t('Per page'),
105
      DRUPAL_CACHE_PER_ROLE => t('Per role'),
106
      DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE => t('Per role per page'),
107
      DRUPAL_CACHE_PER_USER => t('Per user'),
108
      DRUPAL_CACHE_PER_USER | DRUPAL_CACHE_PER_PAGE => t('Per user per page'),
109
    );
110
  }
111

    
112
  /**
113
   * Provide a single method to figure caching type, keeping a sensible default
114
   * for when it's unset.
115
   */
116
  function get_cache_type() {
117
    $cache_type = $this->get_option('block_caching');
118
    if (empty($cache_type)) {
119
      $cache_type = DRUPAL_NO_CACHE;
120
    }
121
    return $cache_type;
122
  }
123

    
124
  /**
125
   * Provide the default form for setting options.
126
   */
127
  function options_form(&$form, &$form_state) {
128
    // It is very important to call the parent function here:
129
    parent::options_form($form, $form_state);
130

    
131
    switch ($form_state['section']) {
132
      case 'block_description':
133
        $form['#title'] .= t('Block admin description');
134
        $form['block_description'] = array(
135
          '#type' => 'textfield',
136
          '#description' => t('This will appear as the name of this block in administer >> structure >> blocks.'),
137
          '#default_value' => $this->get_option('block_description'),
138
        );
139
        break;
140
      case 'block_caching':
141
        $form['#title'] .= t('Block caching type');
142

    
143
        $form['block_caching'] = array(
144
          '#type' => 'radios',
145
          '#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."),
146
          '#options' => $this->block_caching_modes(),
147
          '#default_value' => $this->get_cache_type(),
148
        );
149
        break;
150
      case 'exposed_form_options':
151
        $this->view->init_handlers();
152
        if (!$this->uses_exposed() && parent::uses_exposed()) {
153
          $form['exposed_form_options']['warning'] = array(
154
            '#weight' => -10,
155
            '#markup' => '<div class="messages warning">' . t('Exposed filters in block displays require "Use AJAX" to be set to work correctly.') . '</div>',
156
          );
157
        }
158
    }
159
  }
160

    
161
  /**
162
   * Perform any necessary changes to the form values prior to storage.
163
   * There is no need for this function to actually store the data.
164
   */
165
  function options_submit(&$form, &$form_state) {
166
    // It is very important to call the parent function here:
167
    parent::options_submit($form, $form_state);
168
    switch ($form_state['section']) {
169
      case 'display_id':
170
        $this->update_block_bid($form_state['view']->name, $this->display->id, $this->display->new_id);
171
        break;
172
      case 'block_description':
173
        $this->set_option('block_description', $form_state['values']['block_description']);
174
        break;
175
      case 'block_caching':
176
        $this->set_option('block_caching', $form_state['values']['block_caching']);
177
        $this->save_block_cache($form_state['view']->name . '-'. $form_state['display_id'], $form_state['values']['block_caching']);
178
        break;
179
    }
180
  }
181

    
182
  /**
183
   * Block views use exposed widgets only if AJAX is set.
184
   */
185
    function uses_exposed() {
186
      if ($this->use_ajax()) {
187
        return parent::uses_exposed();
188
      }
189
      return FALSE;
190
    }
191

    
192
  /**
193
   * Update the block delta when you change the machine readable name of the display.
194
   */
195
  function update_block_bid($name, $old_delta, $delta) {
196
    $old_hashes = $hashes = variable_get('views_block_hashes', array());
197

    
198
    $old_delta = $name . '-' . $old_delta;
199
    $delta = $name . '-' . $delta;
200
    if (strlen($old_delta) >= 32) {
201
      $old_delta = md5($old_delta);
202
      unset($hashes[$old_delta]);
203
    }
204
    if (strlen($delta) >= 32) {
205
      $md5_delta = md5($delta);
206
      $hashes[$md5_delta] = $delta;
207
      $delta = $md5_delta;
208
    }
209

    
210
    // Maybe people don't have block module installed, so let's skip this.
211
    if (db_table_exists('block')) {
212
      db_update('block')
213
        ->fields(array('delta' => $delta))
214
        ->condition('delta', $old_delta)
215
        ->execute();
216
    }
217

    
218
    // Update the hashes if needed.
219
    if ($hashes != $old_hashes) {
220
      variable_set('views_block_hashes', $hashes);
221
    }
222
  }
223

    
224
  /**
225
   * Save the block cache setting in the blocks table if this block already
226
   * exists in the blocks table. Dirty fix until http://drupal.org/node/235673 gets in.
227
   */
228
  function save_block_cache($delta, $cache_setting) {
229
    if (strlen($delta) >= 32) {
230
      $delta = md5($delta);
231
    }
232
    if (db_table_exists('block') && $bid = db_query("SELECT bid FROM {block} WHERE module = 'views' AND delta = :delta", array(
233
        ':delta' => $delta))->fetchField()) {
234
      db_update('block')
235
        ->fields(array(
236
        'cache' => $cache_setting,
237
        ))
238
        ->condition('module','views')
239
        ->condition('delta', $delta)
240
        ->execute();
241
    }
242
  }
243
}