4 * Contains the block display plugin.
8 * The plugin that handles a block.
10 * @ingroup views_display_plugins
12 class views_plugin_display_block
extends views_plugin_display
{
13 function option_definition() {
14 $options = parent
::option_definition();
16 $options['block_description'] = array('default' => '', 'translatable' => TRUE
);
17 $options['block_caching'] = array('default' => BLOCK_NO_CACHE
);
23 * The default block handler doesn't support configurable items,
24 * but extended block handlers might be able to do interesting
27 function execute_hook_block($op = 'list', $delta = 0, $edit = array()) {
29 $delta = $this->view
->name .
'-' .
$this->display
->id
;
30 $desc = $this->get_option('block_description');
33 $desc = t('@view: @display', array('@view' => $this->view
->name
, '@display' => $this->display
->display_title
));
38 'cache' => $this->get_cache_type()
45 * The display block handler returns the structure necessary for a block.
48 // Prior to this being called, the $view should already be set to this
49 // display, and arguments should be set on the view.
50 $info['content'] = $this->view
->render();
51 $info['subject'] = filter_xss_admin($this->view
->get_title());
52 if (!empty($this->view
->result
) || $this->get_option('empty') || !empty($this->view
->style_plugin
->definition
['even empty'])) {
58 * Provide the summary for page options in the views UI.
60 * This output is returned as an array.
62 function options_summary(&$categories, &$options) {
63 // It is very important to call the parent function here:
64 parent
::options_summary($categories, $options);
66 $categories['block'] = array(
67 'title' => t('Block settings'),
70 $block_description = strip_tags($this->get_option('block_description'));
71 if (empty($block_description)) {
72 $block_description = t('None');
75 if (strlen($block_description) > 16) {
76 $block_description = substr($block_description, 0, 16) .
'...';
79 $options['block_description'] = array(
80 'category' => 'block',
81 'title' => t('Admin'),
82 'value' => $block_description,
85 $cache_type = $this->get_option('block_caching');
86 if (empty($cache_type)) {
87 $cache_type = BLOCK_NO_CACHE
;
90 $types = $this->block_caching_modes();
91 $options['block_caching'] = array(
92 'category' => 'block',
93 'title' => t('Caching'),
94 'value' => $types[$this->get_cache_type()],
99 * Provide a list of core's block caching modes.
101 function block_caching_modes() {
103 BLOCK_NO_CACHE
=> t('Do not cache'),
104 BLOCK_CACHE_GLOBAL
=> t('Cache once for everything (global)'),
105 BLOCK_CACHE_PER_PAGE
=> t('Per page'),
106 BLOCK_CACHE_PER_ROLE
=> t('Per role'),
107 BLOCK_CACHE_PER_ROLE
| BLOCK_CACHE_PER_PAGE
=> t('Per role per page'),
108 BLOCK_CACHE_PER_USER
=> t('Per user'),
109 BLOCK_CACHE_PER_USER
| BLOCK_CACHE_PER_PAGE
=> t('Per user per page'),
114 * Provide a single method to figure caching type, keeping a sensible default
115 * for when it's unset.
117 function get_cache_type() {
118 $cache_type = $this->get_option('block_caching');
119 if (empty($cache_type)) {
120 $cache_type = BLOCK_NO_CACHE
;
126 * Provide the default form for setting options.
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);
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 >> site building >> blocks.'),
138 '#default_value' => $this->get_option('block_description'),
141 case
'block_caching':
142 $form['#title'] .
= t('Block caching type');
144 $form['block_caching'] = array(
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(),
155 * Perform any necessary changes to the form values prior to storage.
156 * There is no need for this function to actually store the data.
158 function options_submit(&$form, &$form_state) {
159 // It is very important to call the parent function here:
160 parent
::options_submit($form, $form_state);
161 switch ($form_state['section']) {
162 case
'block_description':
163 $this->set_option('block_description', $form_state['values']['block_description']);
165 case
'block_caching':
166 $this->set_option('block_caching', $form_state['values']['block_caching']);
167 $this->save_block_cache($form_state['view']->name.
'-'.
$form_state['display_id'], $form_state['values']['block_caching']);
173 * Block views use exposed widgets only if AJAX is set.
175 function uses_exposed() {
176 if ($this->use_ajax()) {
177 return parent
::uses_exposed();
184 * Save the block cache setting in the blocks table if this block allready
185 * exists in the blocks table. Dirty fix untill http://drupal.org/node/235673 gets in.
187 function save_block_cache($delta, $cache_setting) {
188 if ($bid = db_fetch_object(db_query("SELECT bid, cache FROM {blocks} WHERE module = 'views' AND delta = '%s'", $delta))) {
189 db_query("UPDATE {blocks} set cache = %d WHERE module = 'views' AND delta = '%s'", $cache_setting, $delta);