Projet

Général

Profil

Paste
Télécharger (7,82 ko) Statistiques
| Branche: | Révision:

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

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
    $title = $this->view->get_title();
56
    $info['subject'] = ($title == '<none>') ? '' : filter_xss_admin($title);
57
    if (!empty($this->view->result) || $this->get_option('empty') || !empty($this->view->style_plugin->definition['even empty'])) {
58
      return $info;
59
    }
60
  }
61

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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