views_content_plugin_display_ctools_context.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_content_plugin_display_ctools_context extends views_plugin_display {
  12. /**
  13. * If this variable is true, this display counts as a context. We use this
  14. * variable so that we can easily build plugins against this display type.
  15. */
  16. var $context_display = TRUE;
  17. function get_style_type() { return 'context'; }
  18. function defaultable_sections($section = NULL) {
  19. if (in_array($section, array('style_options', 'style_plugin', 'row_options', 'row_plugin',))) {
  20. return FALSE;
  21. }
  22. return parent::defaultable_sections($section);
  23. }
  24. function option_definition() {
  25. $options = parent::option_definition();
  26. $options['admin_title'] = array('default' => '', 'translatable' => TRUE);
  27. // Overrides for standard stuff:
  28. $options['style_plugin']['default'] = 'ctools_context';
  29. $options['row_plugin']['default'] = 'fields';
  30. $options['defaults']['default']['style_plugin'] = FALSE;
  31. $options['defaults']['default']['style_options'] = FALSE;
  32. $options['defaults']['default']['row_plugin'] = FALSE;
  33. $options['defaults']['default']['row_options'] = FALSE;
  34. $options['inherit_panels_path'] = array('default' => 0);
  35. $options['argument_input'] = array('default' => array());
  36. return $options;
  37. }
  38. /**
  39. * The display block handler returns the structure necessary for a block.
  40. */
  41. function execute() {
  42. $this->executing = TRUE;
  43. return $this->view->render();
  44. }
  45. function preview() {
  46. $this->previewing = TRUE;
  47. return $this->view->render();
  48. }
  49. /**
  50. * Render this display.
  51. */
  52. function render() {
  53. if (!empty($this->previewing)) {
  54. return theme($this->theme_functions(), array('view' => $this->view));
  55. }
  56. else {
  57. // We want to process the view like we're theming it, but not actually
  58. // use the template part. Therefore we run through all the preprocess
  59. // functions which will populate the variables array.
  60. $hooks = theme_get_registry();
  61. $info = $hooks[$this->definition['theme']];
  62. if (!empty($info['file'])) {
  63. @include_once('./' . $info['path'] . '/' . $info['file']);
  64. }
  65. $this->variables = array('view' => &$this->view);
  66. if (isset($info['preprocess functions']) && is_array($info['preprocess functions'])) {
  67. foreach ($info['preprocess functions'] as $preprocess_function) {
  68. if (function_exists($preprocess_function)) {
  69. $preprocess_function($this->variables, $this->definition['theme']);
  70. }
  71. }
  72. }
  73. }
  74. return $this->variables;
  75. }
  76. /**
  77. * Provide the summary for page options in the views UI.
  78. *
  79. * This output is returned as an array.
  80. */
  81. function options_summary(&$categories, &$options) {
  82. // It is very important to call the parent function here:
  83. parent::options_summary($categories, $options);
  84. $categories['context'] = array(
  85. 'title' => t('Context settings'),
  86. 'column' => 'second',
  87. 'build' => array(
  88. '#weight' => -10,
  89. ),
  90. );
  91. $admin_title = $this->get_option('admin_title');
  92. if (empty($admin_title)) {
  93. $admin_title = t('Use view name');
  94. }
  95. if (drupal_strlen($admin_title) > 16) {
  96. $admin_title = drupal_substr($admin_title, 0, 16) . '...';
  97. }
  98. $options['admin_title'] = array(
  99. 'category' => 'context',
  100. 'title' => t('Admin title'),
  101. 'value' => $admin_title,
  102. );
  103. $options['inherit_panels_path'] = array(
  104. 'category' => 'context',
  105. 'title' => t('Use Panel path'),
  106. 'value' => $this->get_option('inherit_panels_path') ? t('Yes') : t('No'),
  107. );
  108. $options['argument_input'] = array(
  109. 'category' => 'context',
  110. 'title' => t('Argument input'),
  111. 'value' => t('Edit'),
  112. );
  113. }
  114. /**
  115. * Provide the default form for setting options.
  116. */
  117. function options_form(&$form, &$form_state) {
  118. // It is very important to call the parent function here:
  119. parent::options_form($form, $form_state);
  120. switch ($form_state['section']) {
  121. case 'row_plugin':
  122. // This just overwrites the existing row_plugin which is using the wrong options.
  123. $form['row_plugin']['#options'] = views_fetch_plugin_names('row', 'normal', array($this->view->base_table));
  124. break;
  125. case 'admin_title':
  126. $form['#title'] .= t('Administrative title');
  127. $form['admin_title'] = array(
  128. '#type' => 'textfield',
  129. '#default_value' => $this->get_option('admin_title'),
  130. '#description' => t('This is the title that will appear for this view context in the configure context dialog. If left blank, the view name will be used.'),
  131. );
  132. break;
  133. case 'inherit_panels_path':
  134. $form['#title'] .= t('Inherit path from panel display');
  135. $form['inherit_panels_path'] = array(
  136. '#type' => 'select',
  137. '#options' => array(1 => t('Yes'), 0 => t('No')),
  138. '#default_value' => $this->get_option('inherit_panels_path'),
  139. '#description' => t('If yes, all links generated by Views, such as more links, summary links, and exposed input links will go to the panels display path, not the view, if the display has a path.'),
  140. );
  141. break;
  142. case 'argument_input':
  143. $form['#title'] .= t('Choose the data source for view arguments');
  144. $argument_input = $this->get_argument_input();
  145. ctools_include('context');
  146. ctools_include('dependent');
  147. $form['argument_input']['#tree'] = TRUE;
  148. $converters = ctools_context_get_all_converters();
  149. ksort($converters);
  150. foreach ($argument_input as $id => $argument) {
  151. $form['argument_input'][$id] = array(
  152. '#tree' => TRUE,
  153. );
  154. $safe = str_replace(array('][', '_', ' ', ':'), '-', $id);
  155. $type_id = 'edit-argument-input-' . $safe;
  156. $form['argument_input'][$id]['type'] = array(
  157. '#type' => 'select',
  158. '#options' => array(
  159. 'none' => t('No argument'),
  160. 'context' => t('From context'),
  161. ),
  162. '#id' => $type_id,
  163. '#title' => t('@arg source', array('@arg' => $argument['name'])),
  164. '#default_value' => $argument['type'],
  165. );
  166. $form['argument_input'][$id]['context'] = array(
  167. '#type' => 'select',
  168. '#title' => t('Required context'),
  169. '#description' => t('If "From context" is selected, which type of context to use.'),
  170. '#default_value' => $argument['context'],
  171. '#options' => $converters,
  172. '#dependency' => array($type_id => array('context')),
  173. );
  174. $form['argument_input'][$id]['context_optional'] = array(
  175. '#type' => 'checkbox',
  176. '#title' => t('Context is optional'),
  177. '#description' => t('This context need not be present for the pane to function. If you plan to use this, ensure that the argument handler can handle empty values gracefully.'),
  178. '#default_value' => $argument['context_optional'],
  179. '#dependency' => array($type_id => array('context')),
  180. );
  181. }
  182. break;
  183. }
  184. }
  185. /**
  186. * Perform any necessary changes to the form values prior to storage.
  187. * There is no need for this function to actually store the data.
  188. */
  189. function options_submit(&$form, &$form_state) {
  190. // It is very important to call the parent function here:
  191. parent::options_submit($form, $form_state);
  192. switch ($form_state['section']) {
  193. case 'admin_title':
  194. case 'argument_input':
  195. case 'inherit_panels_path':
  196. $this->set_option($form_state['section'], $form_state['values'][$form_state['section']]);
  197. break;
  198. }
  199. }
  200. /**
  201. * Adjust the array of argument input to match the current list of
  202. * arguments available for this display. This ensures that changing
  203. * the arguments doesn't cause the argument input field to just
  204. * break.
  205. */
  206. function get_argument_input() {
  207. $arguments = $this->get_option('argument_input');
  208. $handlers = $this->get_handlers('argument');
  209. // We use a separate output so as to seamlessly discard info for
  210. // arguments that no longer exist.
  211. $output = array();
  212. foreach ($handlers as $id => $handler) {
  213. if (empty($arguments[$id])) {
  214. $output[$id] = array(
  215. 'type' => 'none',
  216. 'context' => 'any',
  217. 'context_optional' => FALSE,
  218. 'name' => $handler->ui_name(),
  219. );
  220. }
  221. else {
  222. $output[$id] = $arguments[$id];
  223. $output[$id]['name'] = $handler->ui_name();
  224. }
  225. }
  226. return $output;
  227. }
  228. function get_path() {
  229. if ($this->get_option('link_display') == 'custom_url' && $override_path = $this->get_option('link_url')) {
  230. return $override_path;
  231. }
  232. if ($this->get_option('inherit_panels_path')) {
  233. return $_GET['q'];
  234. }
  235. return parent::get_path();
  236. }
  237. }