Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_plugin_display_block.
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

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20

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

    
24
    return $options;
25
  }
26

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

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

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

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

    
75
    $categories['block'] = array(
76
      'title' => t('Block settings'),
77
      'column' => 'second',
78
      'build' => array(
79
        '#weight' => -10,
80
      ),
81
    );
82

    
83
    $block_description = strip_tags($this->get_option('block_description'));
84
    if (empty($block_description)) {
85
      $block_description = t('None');
86
    }
87

    
88
    $options['block_description'] = array(
89
      'category' => 'block',
90
      'title' => t('Block name'),
91
      'value' => views_ui_truncate($block_description, 24),
92
    );
93

    
94
    $types = $this->block_caching_modes();
95
    $options['block_caching'] = array(
96
      'category' => 'other',
97
      'title' => t('Block caching'),
98
      'value' => $types[$this->get_cache_type()],
99
    );
100
  }
101

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

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

    
129
  /**
130
   * Provide the default form for setting options.
131
   */
132
  public function options_form(&$form, &$form_state) {
133
    // It is very important to call the parent function here.
134
    parent::options_form($form, $form_state);
135

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

    
146
      case 'block_caching':
147
        $form['#title'] .= t('Block caching type');
148

    
149
        $form['block_caching'] = array(
150
          '#type' => 'radios',
151
          '#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."),
152
          '#options' => $this->block_caching_modes(),
153
          '#default_value' => $this->get_cache_type(),
154
        );
155
        break;
156

    
157
      case 'exposed_form_options':
158
        $this->view->init_handlers();
159
        if (!$this->uses_exposed() && parent::uses_exposed()) {
160
          $form['exposed_form_options']['warning'] = array(
161
            '#weight' => -10,
162
            '#markup' => '<div class="messages warning">' . t('Exposed filters in block displays require "Use AJAX" to be set to work correctly.') . '</div>',
163
          );
164
        }
165
        break;
166
    }
167
  }
168

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

    
181
      case 'block_description':
182
        $this->set_option('block_description', $form_state['values']['block_description']);
183
        break;
184

    
185
      case 'block_caching':
186
        $this->set_option('block_caching', $form_state['values']['block_caching']);
187
        $this->save_block_cache($form_state['view']->name . '-' . $form_state['display_id'], $form_state['values']['block_caching']);
188
        break;
189
    }
190
  }
191

    
192
  /**
193
   * Block views use exposed widgets only if AJAX is set.
194
   */
195
  public function uses_exposed() {
196
    if ($this->use_ajax()) {
197
      return parent::uses_exposed();
198
    }
199
    return FALSE;
200
  }
201

    
202
  /**
203
   * Update the block delta when the machine name of the display changes.
204
   */
205
  public function update_block_bid($name, $old_delta, $delta) {
206
    $old_hashes = $hashes = variable_get('views_block_hashes', array());
207

    
208
    $old_delta = $name . '-' . $old_delta;
209
    $delta = $name . '-' . $delta;
210
    if (strlen($old_delta) >= 32) {
211
      $old_delta = md5($old_delta);
212
      unset($hashes[$old_delta]);
213
    }
214
    if (strlen($delta) >= 32) {
215
      $md5_delta = md5($delta);
216
      $hashes[$md5_delta] = $delta;
217
      $delta = $md5_delta;
218
    }
219

    
220
    // Maybe people don't have block module installed, so let's skip this.
221
    if (db_table_exists('block')) {
222
      db_update('block')
223
        ->fields(array('delta' => $delta))
224
        ->condition('delta', $old_delta)
225
        ->execute();
226
    }
227

    
228
    // Update the hashes if needed.
229
    if ($hashes != $old_hashes) {
230
      variable_set('views_block_hashes', $hashes);
231
    }
232
  }
233

    
234
  /**
235
   * Save the block cache setting in the blocks table if this block already
236
   * exists in the blocks table. Dirty fix until http://drupal.org/node/235673
237
   * gets in.
238
   */
239
  public function save_block_cache($delta, $cache_setting) {
240
    if (strlen($delta) >= 32) {
241
      $delta = md5($delta);
242
    }
243
    if (db_table_exists('block') && $bid = db_query("SELECT bid FROM {block} WHERE module = 'views' AND delta = :delta", array(':delta' => $delta))->fetchField()) {
244
      db_update('block')
245
        ->fields(array(
246
          'cache' => $cache_setting,
247
        ))
248
        ->condition('module', 'views')
249
        ->condition('delta', $delta)
250
        ->execute();
251
    }
252
  }
253

    
254
}