views_content.module 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * @file views_content.module
  4. *
  5. * Provides views as panels content, configurable by the administrator.
  6. * Each view provided as panel content must be configured in advance,
  7. * but once configured, building panels with views is a little bit simpler.
  8. */
  9. /**
  10. * Implements hook_menu().
  11. */
  12. function views_content_menu() {
  13. $items = array();
  14. if (!module_exists('panels')) {
  15. $items['admin/config/content-views'] = array(
  16. 'title' => 'Views panes',
  17. 'access arguments' => array('administer views content plugin'),
  18. 'page callback' => 'drupal_get_form',
  19. 'page arguments' => array('views_content_admin_page'),
  20. 'description' => 'Configure Views to be used as CTools content.',
  21. 'type' => MENU_NORMAL_ITEM,
  22. );
  23. }
  24. return $items;
  25. }
  26. /**
  27. * Implementation of hook_ctools_plugin_dierctory() to let the system know
  28. * where our content_type plugins are.
  29. */
  30. function views_content_ctools_plugin_directory($owner, $plugin_type) {
  31. if ($owner == 'ctools') {
  32. return 'plugins/' . $plugin_type;
  33. }
  34. }
  35. /**
  36. * Don't show Views' blocks; we expose them already.
  37. */
  38. function views_ctools_block_info($module, $delta, &$info) {
  39. if (strlen($delta) == 32) {
  40. $hashes = variable_get('views_block_hashes', array());
  41. if (!empty($hashes[$delta])) {
  42. $delta = $hashes[$delta];
  43. }
  44. }
  45. if (substr($delta, 0, 1) != '-') {
  46. $info = NULL;
  47. }
  48. else {
  49. $info['category'] = t('Views');
  50. $info['icon'] = 'icon_views_block_legacy.png';
  51. $info['path'] = drupal_get_path('module', 'views_content');
  52. $info['edit form'] = 'views_content_exposed_form_pane_edit';
  53. $info['add form'] = 'views_content_exposed_form_pane_edit';
  54. $info['render callback'] = 'views_content_exposed_form_pane_render';
  55. }
  56. }
  57. /**
  58. * Add settings to the "exposed form in block" views.
  59. */
  60. function views_content_exposed_form_pane_edit($form, &$form_state) {
  61. // This is a cheesy way to add defaults only to new versions of the block
  62. // but leave older blocks without the setting alone. We can tell because
  63. // all older content will have something set for override_title which is
  64. // the only pre-existing setting.
  65. if (!isset($form_state['conf']['inherit_path']) && !isset($form_state['conf']['override_title'])) {
  66. $form_state['conf']['inherit_path'] = TRUE;
  67. }
  68. $form['inherit_path'] = array(
  69. '#type' => 'checkbox',
  70. '#title' => t('Inherit path'),
  71. '#default_value' => !empty($form_state['conf']['inherit_path']),
  72. );
  73. return $form;
  74. }
  75. /**
  76. * Store data for the exposed form in block settings page.
  77. */
  78. function views_content_exposed_form_pane_edit_submit($form, &$form_state) {
  79. $form_state['conf']['inherit_path'] = $form_state['values']['inherit_path'];
  80. }
  81. /**
  82. * Render function for 'special' view blocks.
  83. *
  84. * We took over the render for the special view blocks so that we could
  85. * add options to it.
  86. */
  87. function views_content_exposed_form_pane_render($subtype, $conf, $panel_args, $contexts) {
  88. $delta = str_replace('views-', '', $subtype);
  89. if (strlen($delta) == 32) {
  90. $hashes = variable_get('views_block_hashes', array());
  91. if (!empty($hashes[$delta])) {
  92. $delta = $hashes[$delta];
  93. }
  94. }
  95. list($nothing, $type, $name, $display_id) = explode('-', $delta);
  96. // Put the - back on. For views special blocks, the first character is always - but
  97. // the explode killed it. Note that this code is mostly copied from views_block().
  98. $type = '-' . $type;
  99. if ($view = views_get_view($name)) {
  100. if ($view->access($display_id)) {
  101. if (!empty($conf['inherit_path'])) {
  102. $view->override_path = $_GET['q'];
  103. }
  104. $view->set_display($display_id);
  105. if (isset($view->display_handler)) {
  106. $block = (object) $view->display_handler->view_special_blocks($type);
  107. return $block;
  108. }
  109. }
  110. $view->destroy();
  111. }
  112. }
  113. /**
  114. * Implements hook_views_api().
  115. *
  116. * This one is used as the base to reduce errors when updating.
  117. */
  118. function views_content_views_api() {
  119. return array(
  120. 'api' => 2,
  121. 'path' => drupal_get_path('module', 'views_content') . '/plugins/views',
  122. );
  123. }
  124. /**
  125. * Page callback to provide the basic administration form.
  126. */
  127. function views_content_admin_page() {
  128. $form = array();
  129. views_content_admin_form($form);
  130. return system_settings_form($form);
  131. }
  132. function views_content_admin_form(&$form) {
  133. $form['ctools_content_all_views'] = array(
  134. '#type' => 'checkbox',
  135. '#title' => t('Make all views available as panes'),
  136. '#description' => t("If checked, all views will be made available as content panes to be added to content types. If not checked, only Views that have a 'Content pane' display will be available as content panes. Uncheck this if you want to be able to more carefully control what view content is available to users using the panels layout UI."),
  137. '#default_value' => variable_get('ctools_content_all_views', TRUE),
  138. );
  139. }
  140. /**
  141. * API function to get the view.
  142. */
  143. function views_content_context_get_view(&$context) {
  144. if (empty($context->view) || get_class($context->view) == '__PHP_Incomplete_Class') {
  145. $context->view = views_get_view($context->data['name']);
  146. if ($context->view) {
  147. $context->view->set_display($context->data['display']);
  148. $context->view->set_arguments($context->data['args']);
  149. }
  150. }
  151. return $context->view;
  152. }
  153. /**
  154. * API function to get the view.
  155. */
  156. function views_content_context_get_output(&$context) {
  157. if (empty($context->output)) {
  158. $view = views_content_context_get_view($context);
  159. $context->output = $view->execute_display($context->data['display']);
  160. }
  161. return $context->output;
  162. }
  163. /**
  164. * Get the title to display for a views content display for pane or context.
  165. */
  166. function views_content_get_display_title($view, $display_id, $option = 'pane_title') {
  167. $handler = $view->display[$display_id]->handler;
  168. $title = $handler->get_option($option);
  169. if (!$title) {
  170. if ($handler->display->display_title == $handler->definition['title']) {
  171. $title = t('View: @view', array('@view' => $view->get_human_name()));
  172. }
  173. else {
  174. $title = t('View: @view: @display', array('@view' => $view->get_human_name(), '@display' => $handler->display->display_title));
  175. }
  176. }
  177. return $title;
  178. }
  179. /**
  180. * Get the proper label for a display.
  181. *
  182. * Views renamed the default to Master, but it can still have a conflicting
  183. * display title. Fix that.
  184. */
  185. function views_content_get_display_label($view, $display_id) {
  186. $title = $display_id == 'default' ? t('Master') : $view->display[$display_id]->display_title;
  187. return $title;
  188. }
  189. /**
  190. * Get the child plugin for a view context display.
  191. *
  192. * This can return both the context and relationship style. The
  193. * $required parameter is used to distinguish if context is required
  194. * or not, so we know whether we need it suitable as a pure context
  195. * (i.e, no context required) or a relationship (i.e, context required).
  196. */
  197. function _views_content_get_context_from_display($view, $id, $parent, $required = TRUE) {
  198. $title = views_content_get_display_title($view, $id, 'admin_title');
  199. $description = $view->description;
  200. $contexts = array();
  201. $arguments = $view->display_handler->get_argument_input();
  202. ctools_include('views');
  203. foreach ($arguments as $argument) {
  204. $argument['label'] = $argument['name'] ? $argument['name'] : '';
  205. $contexts[] = ctools_views_get_argument_context($argument);
  206. }
  207. $pass = FALSE;
  208. if ($required) {
  209. // If context is required, make sure we have at least one optional
  210. // or required context.
  211. foreach ($contexts as $context) {
  212. if (is_object($context)) {
  213. $pass = TRUE;
  214. break;
  215. }
  216. }
  217. if (!$pass) {
  218. return;
  219. }
  220. }
  221. else {
  222. // If context is not required, then having any required context
  223. // causes a fail.
  224. foreach ($contexts as $context) {
  225. if (is_object($context) && get_class($context) == 'ctools_context_required') {
  226. return;
  227. }
  228. }
  229. // Since we know we don't want contexts, we an unset this now.
  230. $contexts = array();
  231. }
  232. if ($required) {
  233. $function = 'views_content_view_from_argument_context';
  234. }
  235. else {
  236. $function = 'views_content_context_view_create';
  237. }
  238. return array(
  239. 'title' => $title,
  240. 'description' => filter_xss_admin($description),
  241. 'required context' => $contexts,
  242. 'keyword' => 'view',
  243. 'context' => $function,
  244. 'context name' => $view->name,
  245. 'name' => $parent . ':' . $view->name . '-' . $id,
  246. 'view name' => $view->name,
  247. 'view display id' => $id,
  248. );
  249. }
  250. /**
  251. * Implements hook_get_pane_links_alter().
  252. */
  253. function views_content_get_pane_links_alter(&$links, $pane, $content_type) {
  254. if ($pane->type === 'views_panes') {
  255. list($view_name, $display_name) = explode('-', $pane->subtype);
  256. $destination = array('destination' => current_path());
  257. $links['top'][] = array(
  258. 'title' => t('Edit view'),
  259. 'href' => url('admin/structure/views/view/' . $view_name . '/edit/' . $display_name, array('query' => $destination, 'absolute' => TRUE)),
  260. );
  261. }
  262. }