views_plugin_display_block.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @file
  4. * Contains the block display plugin.
  5. */
  6. /**
  7. * The plugin that handles a block.
  8. *
  9. * @ingroup views_display_plugins
  10. */
  11. class views_plugin_display_block extends views_plugin_display {
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['block_description'] = array('default' => '', 'translatable' => TRUE);
  15. $options['block_caching'] = array('default' => DRUPAL_NO_CACHE);
  16. return $options;
  17. }
  18. /**
  19. * The default block handler doesn't support configurable items,
  20. * but extended block handlers might be able to do interesting
  21. * stuff with it.
  22. */
  23. function execute_hook_block_list($delta = 0, $edit = array()) {
  24. $delta = $this->view->name . '-' . $this->display->id;
  25. $desc = $this->get_option('block_description');
  26. if (empty($desc)) {
  27. if ($this->display->display_title == $this->definition['title']) {
  28. $desc = t('View: !view', array('!view' => $this->view->get_human_name()));
  29. }
  30. else {
  31. $desc = t('View: !view: !display', array('!view' => $this->view->get_human_name(), '!display' => $this->display->display_title));
  32. }
  33. }
  34. return array(
  35. $delta => array(
  36. 'info' => $desc,
  37. 'cache' => $this->get_cache_type()
  38. ),
  39. );
  40. }
  41. /**
  42. * The display block handler returns the structure necessary for a block.
  43. */
  44. function execute() {
  45. // Prior to this being called, the $view should already be set to this
  46. // display, and arguments should be set on the view.
  47. $info['content'] = $this->view->render();
  48. $title = $this->view->get_title();
  49. $info['subject'] = ($title == '<none>') ? '' : filter_xss_admin($title);
  50. if (!empty($this->view->result) || $this->get_option('empty') || !empty($this->view->style_plugin->definition['even empty'])) {
  51. return $info;
  52. }
  53. }
  54. /**
  55. * Provide the summary for page options in the views UI.
  56. *
  57. * This output is returned as an array.
  58. */
  59. function options_summary(&$categories, &$options) {
  60. // It is very important to call the parent function here:
  61. parent::options_summary($categories, $options);
  62. $categories['block'] = array(
  63. 'title' => t('Block settings'),
  64. 'column' => 'second',
  65. 'build' => array(
  66. '#weight' => -10,
  67. ),
  68. );
  69. $block_description = strip_tags($this->get_option('block_description'));
  70. if (empty($block_description)) {
  71. $block_description = t('None');
  72. }
  73. $options['block_description'] = array(
  74. 'category' => 'block',
  75. 'title' => t('Block name'),
  76. 'value' => views_ui_truncate($block_description, 24),
  77. );
  78. $types = $this->block_caching_modes();
  79. $options['block_caching'] = array(
  80. 'category' => 'other',
  81. 'title' => t('Block caching'),
  82. 'value' => $types[$this->get_cache_type()],
  83. );
  84. }
  85. /**
  86. * Provide a list of core's block caching modes.
  87. */
  88. function block_caching_modes() {
  89. return array(
  90. DRUPAL_NO_CACHE => t('Do not cache'),
  91. DRUPAL_CACHE_GLOBAL => t('Cache once for everything (global)'),
  92. DRUPAL_CACHE_PER_PAGE => t('Per page'),
  93. DRUPAL_CACHE_PER_ROLE => t('Per role'),
  94. DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE => t('Per role per page'),
  95. DRUPAL_CACHE_PER_USER => t('Per user'),
  96. DRUPAL_CACHE_PER_USER | DRUPAL_CACHE_PER_PAGE => t('Per user per page'),
  97. );
  98. }
  99. /**
  100. * Provide a single method to figure caching type, keeping a sensible default
  101. * for when it's unset.
  102. */
  103. function get_cache_type() {
  104. $cache_type = $this->get_option('block_caching');
  105. if (empty($cache_type)) {
  106. $cache_type = DRUPAL_NO_CACHE;
  107. }
  108. return $cache_type;
  109. }
  110. /**
  111. * Provide the default form for setting options.
  112. */
  113. function options_form(&$form, &$form_state) {
  114. // It is very important to call the parent function here:
  115. parent::options_form($form, $form_state);
  116. switch ($form_state['section']) {
  117. case 'block_description':
  118. $form['#title'] .= t('Block admin description');
  119. $form['block_description'] = array(
  120. '#type' => 'textfield',
  121. '#description' => t('This will appear as the name of this block in administer >> structure >> blocks.'),
  122. '#default_value' => $this->get_option('block_description'),
  123. );
  124. break;
  125. case 'block_caching':
  126. $form['#title'] .= t('Block caching type');
  127. $form['block_caching'] = array(
  128. '#type' => 'radios',
  129. '#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."),
  130. '#options' => $this->block_caching_modes(),
  131. '#default_value' => $this->get_cache_type(),
  132. );
  133. break;
  134. case 'exposed_form_options':
  135. $this->view->init_handlers();
  136. if (!$this->uses_exposed() && parent::uses_exposed()) {
  137. $form['exposed_form_options']['warning'] = array(
  138. '#weight' => -10,
  139. '#markup' => '<div class="messages warning">' . t('Exposed filters in block displays require "Use AJAX" to be set to work correctly.') . '</div>',
  140. );
  141. }
  142. }
  143. }
  144. /**
  145. * Perform any necessary changes to the form values prior to storage.
  146. * There is no need for this function to actually store the data.
  147. */
  148. function options_submit(&$form, &$form_state) {
  149. // It is very important to call the parent function here:
  150. parent::options_submit($form, $form_state);
  151. switch ($form_state['section']) {
  152. case 'display_id':
  153. $this->update_block_bid($form_state['view']->name, $this->display->id, $this->display->new_id);
  154. break;
  155. case 'block_description':
  156. $this->set_option('block_description', $form_state['values']['block_description']);
  157. break;
  158. case 'block_caching':
  159. $this->set_option('block_caching', $form_state['values']['block_caching']);
  160. $this->save_block_cache($form_state['view']->name . '-'. $form_state['display_id'], $form_state['values']['block_caching']);
  161. break;
  162. }
  163. }
  164. /**
  165. * Block views use exposed widgets only if AJAX is set.
  166. */
  167. function uses_exposed() {
  168. if ($this->use_ajax()) {
  169. return parent::uses_exposed();
  170. }
  171. return FALSE;
  172. }
  173. /**
  174. * Update the block delta when you change the machine readable name of the display.
  175. */
  176. function update_block_bid($name, $old_delta, $delta) {
  177. $old_hashes = $hashes = variable_get('views_block_hashes', array());
  178. $old_delta = $name . '-' . $old_delta;
  179. $delta = $name . '-' . $delta;
  180. if (strlen($old_delta) >= 32) {
  181. $old_delta = md5($old_delta);
  182. unset($hashes[$old_delta]);
  183. }
  184. if (strlen($delta) >= 32) {
  185. $md5_delta = md5($delta);
  186. $hashes[$md5_delta] = $delta;
  187. $delta = $md5_delta;
  188. }
  189. // Maybe people don't have block module installed, so let's skip this.
  190. if (db_table_exists('block')) {
  191. db_update('block')
  192. ->fields(array('delta' => $delta))
  193. ->condition('delta', $old_delta)
  194. ->execute();
  195. }
  196. // Update the hashes if needed.
  197. if ($hashes != $old_hashes) {
  198. variable_set('views_block_hashes', $hashes);
  199. }
  200. }
  201. /**
  202. * Save the block cache setting in the blocks table if this block already
  203. * exists in the blocks table. Dirty fix until http://drupal.org/node/235673 gets in.
  204. */
  205. function save_block_cache($delta, $cache_setting) {
  206. if (strlen($delta) >= 32) {
  207. $delta = md5($delta);
  208. }
  209. if (db_table_exists('block') && $bid = db_query("SELECT bid FROM {block} WHERE module = 'views' AND delta = :delta", array(
  210. ':delta' => $delta))->fetchField()) {
  211. db_update('block')
  212. ->fields(array(
  213. 'cache' => $cache_setting,
  214. ))
  215. ->condition('module','views')
  216. ->condition('delta', $delta)
  217. ->execute();
  218. }
  219. }
  220. }