panels_node.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. /**
  3. * @file panels_node.module
  4. *
  5. * This module provides the "panel" node type.
  6. * Panel nodes are useful to add additional content to the content area
  7. * on a per-node base.
  8. */
  9. // ---------------------------------------------------------------------------
  10. // General Drupal hooks
  11. /**
  12. * Implementation of hook_permission().
  13. */
  14. function panels_node_permission() {
  15. return array(
  16. 'administer panel-nodes' => array(
  17. 'title' => t('Administer panel nodes'),
  18. 'description' => t('Full administrative access to panel nodes including create, update and delete all'),
  19. ),
  20. );
  21. }
  22. /**
  23. * Implementation of hook_ctools_plugin_directory().
  24. */
  25. function panels_node_ctools_plugin_directory($module, $plugin) {
  26. if ($module == 'panels' && $plugin == 'panels_storage') {
  27. return 'plugins/' . $plugin;
  28. }
  29. }
  30. /**
  31. * Implementation of hook_menu().
  32. */
  33. function panels_node_menu() {
  34. // Safety: go away if CTools is not at an appropriate version.
  35. if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
  36. return array();
  37. }
  38. $items['admin/structure/panels/settings/panel-node'] = array(
  39. 'title' => 'Panel nodes',
  40. 'description' => 'Configure which content is available to add to panel node displays.',
  41. 'access arguments' => array('administer panel-nodes'),
  42. 'page callback' => 'panels_node_settings',
  43. 'type' => MENU_LOCAL_TASK,
  44. );
  45. // Avoid some repetition on these:
  46. $base = array(
  47. 'access callback' => 'panels_node_edit_node',
  48. 'access arguments' => array(1),
  49. 'page arguments' => array(1),
  50. 'type' => MENU_LOCAL_TASK,
  51. );
  52. $items['node/%node/panel_layout'] = array(
  53. 'title' => 'Panel layout',
  54. 'page callback' => 'panels_node_edit_layout',
  55. 'weight' => 2,
  56. ) + $base;
  57. $items['node/%node/panel_content'] = array(
  58. 'title' => 'Panel content',
  59. 'page callback' => 'panels_node_edit_content',
  60. 'weight' => 3,
  61. ) + $base;
  62. $items['node/add/panel/choose-layout'] = array(
  63. 'title' => 'Choose layout',
  64. 'access callback' => 'panels_add_panel_access_callback',
  65. 'page callback' => 'panels_node_add',
  66. 'type' => MENU_CALLBACK,
  67. );
  68. return $items;
  69. }
  70. /**
  71. * Access callback to determine if a user has edit access
  72. */
  73. function panels_node_edit_node($node) {
  74. if (!isset($node->panels_node)) {
  75. return FALSE;
  76. }
  77. return node_access('update', $node);
  78. }
  79. /**
  80. * Access callback to determine if user has access to add panel nodes.
  81. */
  82. function panels_add_panel_access_callback() {
  83. return user_access('create panel content') || user_access('administer panel-nodes');
  84. }
  85. /**
  86. * Override of node add page to force layout selection prior
  87. * to actually editing a node.
  88. */
  89. function panels_node_add() {
  90. $output = '';
  91. ctools_include('plugins', 'panels');
  92. ctools_include('common', 'panels');
  93. $layouts = panels_common_get_allowed_layouts('panels_node');
  94. return panels_common_print_layout_links($layouts, 'node/add/panel', array('query' => drupal_get_query_parameters()));
  95. }
  96. // ---------------------------------------------------------------------------
  97. // Node hooks
  98. /**
  99. * Implementation of hook_node_info().
  100. */
  101. function panels_node_node_info() {
  102. // Safety: go away if CTools is not at an appropriate version.
  103. if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
  104. return array();
  105. }
  106. return array(
  107. 'panel' => array(
  108. 'name' => t('Panel'),
  109. // We use panels_node_hook so that panels_node private
  110. // callbacks do not get confused with panels versions of
  111. // nodeapi callbacks.
  112. 'base' => 'panels_node_hook',
  113. 'body_label' => t('Teaser'),
  114. 'description' => t("A panel layout broken up into rows and columns."),
  115. ),
  116. );
  117. }
  118. /**
  119. * Implementation of hook_access().
  120. */
  121. function panels_node_node_access($node, $op, $account) {
  122. if ($op == 'create' && $node != 'panel') {
  123. return NODE_ACCESS_IGNORE;
  124. }
  125. if (is_object($node) && $node->type != 'panel') {
  126. return NODE_ACCESS_IGNORE;
  127. }
  128. if (user_access('administer panel-nodes', $account)) {
  129. return NODE_ACCESS_ALLOW;
  130. }
  131. }
  132. /**
  133. * Implementation of hook_form().
  134. */
  135. function panels_node_hook_form(&$node, &$form_state) {
  136. ctools_include('plugins', 'panels');
  137. $form['panels_node']['#tree'] = TRUE;
  138. if (empty($node->nid) && arg(0) == 'node' && arg(1) == 'add') {
  139. // Grab our selected layout from the $node, If it doesn't exist, try arg(3)
  140. // and if that doesn't work present them with a list to pick from.
  141. $panel_layout = isset($node->panel_layout) ? $node->panel_layout : arg(3);
  142. if (empty($panel_layout)) {
  143. drupal_goto('node/add/panel/choose-layout', array('query' => drupal_get_query_parameters()));
  144. }
  145. $layout = panels_get_layout($panel_layout);
  146. if (empty($layout)) {
  147. return MENU_NOT_FOUND;
  148. }
  149. $form['panels_node']['layout'] = array(
  150. '#type' => 'value',
  151. '#value' => $panel_layout,
  152. );
  153. }
  154. $type = node_type_get_type($node);
  155. $form['title'] = array(
  156. '#type' => 'textfield',
  157. '#title' => check_plain($type->title_label),
  158. '#required' => TRUE,
  159. '#default_value' => $node->title,
  160. );
  161. $css_id = '';
  162. if (!empty($node->panels_node['css_id'])) {
  163. $css_id = $node->panels_node['css_id'];
  164. }
  165. $form['panels_node']['css_id'] = array(
  166. '#type' => 'textfield',
  167. '#title' => t('CSS ID'),
  168. '#size' => 30,
  169. '#description' => t('An ID that can be used by CSS to style the panel.'),
  170. '#default_value' => $css_id,
  171. );
  172. // Support for different rendering pipelines
  173. // Mostly borrowed from panel_context.inc
  174. $pipelines = panels_get_renderer_pipelines();
  175. $options = array();
  176. foreach ($pipelines as $name => $pipeline) {
  177. $options[$name] = check_plain($pipeline->admin_title) . '<div class="description">' . check_plain($pipeline->admin_description) . '</div>';
  178. }
  179. $form['panels_node']['pipeline'] = array(
  180. '#type' => 'radios',
  181. '#options' => $options,
  182. '#title' => t('Renderer'),
  183. '#default_value' => isset($node->panels_node['pipeline']) ? $node->panels_node['pipeline'] : variable_get('panels_renderer_default', 'standard'),
  184. );
  185. return $form;
  186. }
  187. /**
  188. * Implementation of hook_validate().
  189. */
  190. function panels_node_hook_validate($node, $form, &$form_state) {
  191. if (!$node->nid && empty($node->panels_node['layout'])) {
  192. form_error($form['panels_node']['layout'], t('Please select a layout.'));
  193. }
  194. }
  195. /**
  196. * Implementation of hook_load().
  197. *
  198. * Panels does not use revisions for nodes because that would open us up
  199. * to have completely separate displays, and we'd have to copy them,
  200. * and that's going to be a LOT of data.
  201. */
  202. function panels_node_hook_load($nodes) {
  203. // We shortcut this because only in some really drastic corruption circumstance will this
  204. // not work.
  205. $result = db_query("SELECT * FROM {panels_node} WHERE nid IN (:nids)", array(':nids' => array_keys($nodes)));
  206. foreach ($result as $record) {
  207. $nodes[$record->nid]->panels_node = (array) $record;
  208. }
  209. }
  210. /**
  211. * Implementation of hook_insert().
  212. */
  213. function panels_node_hook_insert(&$node) {
  214. // Create a new display and record that.
  215. $display = panels_new_display();
  216. $display->layout = $node->panels_node['layout'];
  217. $display->storage_type = 'panels_node';
  218. $display->storage_id = $node->nid;
  219. // Special handling for nodes being imported from an export.module data dump.
  220. if (!empty($node->export_display)) {
  221. // This works by overriding the $display set above
  222. eval($node->export_display);
  223. unset($node->export_display);
  224. }
  225. panels_save_display($display);
  226. $node->panels_node['did'] = $display->did;
  227. db_insert('panels_node')
  228. ->fields(array(
  229. 'nid' => $node->nid,
  230. 'did' => $display->did,
  231. 'css_id' => $node->panels_node['css_id'],
  232. 'pipeline' => $node->panels_node['pipeline'],
  233. ))
  234. ->execute();
  235. }
  236. /**
  237. * Implementation of hook_delete().
  238. */
  239. function panels_node_hook_delete(&$node) {
  240. db_delete('panels_node')->condition('nid', $node->nid)->execute();
  241. if (!empty($node->panels_node['did'])) {
  242. panels_delete_display($node->panels_node['did']);
  243. }
  244. }
  245. /**
  246. * Implementation of hook_update().
  247. */
  248. function panels_node_hook_update($node) {
  249. db_update('panels_node')
  250. ->condition('nid', $node->nid)
  251. ->fields(array(
  252. 'css_id' => $node->panels_node['css_id'],
  253. 'pipeline' => $node->panels_node['pipeline'],
  254. ))
  255. ->execute();
  256. }
  257. /**
  258. * Implementation of hook_view().
  259. */
  260. function panels_node_hook_view($node, $view_mode) {
  261. static $rendering = array();
  262. // Prevent loops if someone foolishly puts the node inside itself:
  263. if (!empty($rendering[$node->nid])) {
  264. return $node;
  265. }
  266. $rendering[$node->nid] = TRUE;
  267. ctools_include('plugins', 'panels');
  268. if ($view_mode == 'teaser') {
  269. // Because our teasier is never the same as our content, *always* provide
  270. // the read more flag.
  271. $node->readmore = TRUE;
  272. }
  273. else {
  274. if (!empty($node->panels_node['did'])) {
  275. $display = panels_load_display($node->panels_node['did']);
  276. $display->css_id = $node->panels_node['css_id'];
  277. // TODO: Find a way to make sure this can't node_view.
  278. $display->context = panels_node_get_context($node);
  279. $display->cache_key = 'panels_node:' . $node->nid;
  280. $renderer = panels_get_renderer($node->panels_node['pipeline'], $display);
  281. $node->content['body'] = array(
  282. '#markup' => panels_render_display($display, $renderer),
  283. '#weight' => 0,
  284. );
  285. }
  286. }
  287. unset($rendering[$node->nid]);
  288. return $node;
  289. }
  290. // ---------------------------------------------------------------------------
  291. // Administrative pages
  292. /**
  293. * Settings for panel nodes.
  294. */
  295. function panels_node_settings() {
  296. ctools_include('common', 'panels');
  297. return drupal_get_form('panels_common_settings', 'panels_node');
  298. }
  299. // ---------------------------------------------------------------------------
  300. // Meat of the Panels API; almost completely passing through to panels.module
  301. /**
  302. * Pass through to the panels layout editor.
  303. */
  304. function panels_node_edit_layout($node) {
  305. // ctools_include('plugins', 'panels');
  306. ctools_include('context');
  307. $display = panels_load_display($node->panels_node['did']);
  308. $display->context = panels_node_get_context($node);
  309. return panels_edit_layout($display, t('Save'), "node/$node->nid/panel_layout", 'panels_node');
  310. }
  311. /**
  312. * Pass through to the panels content editor.
  313. */
  314. function panels_node_edit_content($node) {
  315. ctools_include('context');
  316. $display = panels_load_display($node->panels_node['did']);
  317. $display->context = panels_node_get_context($node);
  318. ctools_include('common', 'panels');
  319. $content_types = panels_common_get_allowed_types('panels_node', $display->context);
  320. return panels_edit($display, "node/$node->nid/panel_content", $content_types);
  321. }
  322. /**
  323. * Build the context to use for a panel node.
  324. */
  325. function panels_node_get_context(&$node) {
  326. ctools_include('context');
  327. $context = ctools_context_create('node', $node);
  328. $context->identifier = t('This node');
  329. $context->keyword = 'node';
  330. return array('panel-node' => $context);
  331. }
  332. /**
  333. * Implementation of hook_export_node_alter()
  334. *
  335. * Integrate with export.module for saving panel_nodes into code.
  336. */
  337. function panels_node_export_node_alter(&$node, $original_node, $method) {
  338. if ($method == 'export') {
  339. $node_export_omitted = variable_get('node_export_omitted', array());
  340. if (variable_get('node_export_method', '') != 'save-edit' && (array_key_exists('panel', $node_export_omitted) && !$node_export_omitted['panel'])) {
  341. drupal_set_message(t("NOTE: in order to import panel_nodes you must first set the export.module settings to \"Save as a new node then edit\", otherwise it won't work."));
  342. }
  343. $display = panels_load_display($node->panels_node['did']);
  344. $export = panels_export_display($display);
  345. $node->export_display = $export;
  346. }
  347. }
  348. /**
  349. * Implementation of hook_panels_dashboard_blocks().
  350. *
  351. * Adds panel nodes information to the Panels dashboard.
  352. */
  353. function panels_node_panels_dashboard_blocks(&$vars) {
  354. $vars['links']['panels_node'] = array(
  355. 'title' => l(t('Panel node'), 'node/add/panel'),
  356. 'description' => t('Panel nodes are node content and appear in your searches, but are more limited than panel pages.'),
  357. 'weight' => -1,
  358. );
  359. }
  360. /**
  361. * Implements hook_panels_ipe_access().
  362. */
  363. function panels_node_panels_ipe_access($display) {
  364. // We only care about Panels displays from panels_node.
  365. if (isset($display->context['panel-node'])) {
  366. // Only allow access to use the IPE if the user has 'update' access to
  367. // the underlying node.
  368. $node = $display->context['panel-node']->data;
  369. return node_access('update', $node);
  370. }
  371. }
  372. // ---------------------------------------------------------------------------
  373. // Callbacks for panel caching.
  374. /**
  375. * Get display edit cache for a panel node being edited.
  376. *
  377. * The key is the second half of the key in this form:
  378. * panels_node:NID;
  379. */
  380. function panels_node_panels_cache_get($nid) {
  381. ctools_include('object-cache');
  382. $cache = ctools_object_cache_get('panels_node_display_cache', $nid);
  383. if (empty($cache)) {
  384. $cache = new stdClass();
  385. $node = node_load($nid);
  386. if (empty($node)) {
  387. return;
  388. }
  389. ctools_include('common', 'panels');
  390. $cache->display = panels_load_display($node->panels_node['did']);
  391. $cache->display->css_id = $node->panels_node['css_id'];
  392. $cache->display->context = panels_node_get_context($node);
  393. $cache->display->cache_key = 'panels_node:' . $node->nid;
  394. $cache->content_types = panels_common_get_allowed_types('panels_node', $cache->display->context);
  395. $cache->allwed_layouts = panels_common_get_allowed_layouts('panels_node');
  396. }
  397. return $cache;
  398. }
  399. /**
  400. * Store a display edit in progress in the panels cache.
  401. */
  402. function panels_node_panels_cache_set($nid, $cache) {
  403. ctools_include('object-cache');
  404. ctools_object_cache_set('panels_node_display_cache', $nid, $cache);
  405. }
  406. /**
  407. * Clear all changes made to a display using the panels cache.
  408. */
  409. function panels_node_panels_cache_clear($nid, $cache) {
  410. ctools_include('object-cache');
  411. ctools_object_cache_clear('panels_node_display_cache', $nid);
  412. }
  413. /**
  414. * React to a cache save and save the display and clear cache.
  415. */
  416. function panels_node_panels_cache_save($nid, $cache) {
  417. panels_save_display($cache->display);
  418. ctools_include('object-cache');
  419. ctools_object_cache_clear('panels_node_display_cache', $nid);
  420. }