views_plugin_display_block.inc 7.9 KB

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