node.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to handle the 'node' content type which allows individual nodes
  5. * to be embedded into a panel.
  6. */
  7. /**
  8. * Plugins are described by creating a $plugin array which will be used
  9. * by the system that includes this file.
  10. */
  11. $plugin = array(
  12. 'title' => t('Existing node'),
  13. 'single' => TRUE,
  14. 'defaults' => array(
  15. 'nid' => '',
  16. 'links' => TRUE,
  17. 'leave_node_title' => FALSE,
  18. 'identifier' => '',
  19. 'build_mode' => 'teaser',
  20. ),
  21. 'icon' => 'icon_node.png',
  22. 'description' => t('Add a node from your site as content.'),
  23. 'category' => t('Custom'),
  24. 'top level' => TRUE,
  25. 'js' => array('misc/autocomplete.js'),
  26. );
  27. /**
  28. * Output function for the 'node' content type.
  29. *
  30. * Outputs a node based on the module and delta supplied in the configuration.
  31. */
  32. function ctools_node_content_type_render($subtype, $conf, $panel_args) {
  33. $nid = $conf['nid'];
  34. $block = new stdClass();
  35. foreach (explode('/', $_GET['q']) as $id => $arg) {
  36. $nid = str_replace("%$id", $arg, $nid);
  37. }
  38. foreach ($panel_args as $id => $arg) {
  39. if (is_string($arg)) {
  40. $nid = str_replace("@$id", $arg, $nid);
  41. }
  42. }
  43. // Support node translation
  44. if (module_exists('translation')) {
  45. if ($translations = module_invoke('translation', 'node_get_translations', $nid)) {
  46. if (isset($translations[$GLOBALS['language']->language])) {
  47. $nid = $translations[$GLOBALS['language']->language]->nid;
  48. }
  49. }
  50. }
  51. if (!is_numeric($nid)) {
  52. return;
  53. }
  54. $node = node_load($nid);
  55. if (!node_access('view', $node)) {
  56. return;
  57. }
  58. // Don't store viewed node data on the node, this can mess up other
  59. // views of the node.
  60. $node = clone $node;
  61. $block->module = 'node';
  62. $block->delta = $node->nid;
  63. // Set block->title to the plain node title, then additionally set block->title_link to
  64. // the node url if required. The actual link is rendered in ctools_content_render().
  65. $block->title = check_plain($node->title);
  66. if (!empty($conf['link_node_title'])) {
  67. $block->title_link = 'node/' . $node->nid;
  68. }
  69. if (empty($conf['leave_node_title'])) {
  70. $node->title = NULL;
  71. }
  72. if (!empty($conf['identifier'])) {
  73. $node->ctools_template_identifier = $conf['identifier'];
  74. }
  75. // Handle existing configurations with the deprecated 'teaser' option.
  76. if (isset($conf['teaser'])) {
  77. $conf['build_mode'] = $conf['teaser'] ? 'teaser' : 'full';
  78. }
  79. $block->content = node_view($node, $conf['build_mode']);
  80. // Hide links if they've been suppressed.
  81. if (empty($conf['links'])) {
  82. $block->content['links']['#access'] = FALSE;
  83. }
  84. return $block;
  85. }
  86. /**
  87. * The form to add or edit a node as content.
  88. */
  89. function ctools_node_content_type_edit_form($form, &$form_state) {
  90. $conf = $form_state['conf'];
  91. $form['leave_node_title'] = array(
  92. '#type' => 'checkbox',
  93. '#default_value' => !empty($conf['leave_node_title']),
  94. '#title' => t('Leave node title'),
  95. '#description' => t('Advanced: if checked, do not touch the node title; this can cause the node title to appear twice unless your theme is aware of this.'),
  96. );
  97. $form['link_node_title'] = array(
  98. '#type' => 'checkbox',
  99. '#default_value' => !empty($conf['link_node_title']),
  100. '#title' => t('Link the node title to the node'),
  101. '#description' => t('Check this box if you would like your pane title to link to the node.'),
  102. );
  103. if ($form_state['op'] == 'add') {
  104. $form['nid'] = array(
  105. '#prefix' => '<div class="no-float">',
  106. '#title' => t('Enter the title or NID of a node'),
  107. '#description' => t('To use a NID from the URL, you may use %0, %1, ..., %N to get URL arguments. Or use @0, @1, @2, ..., @N to use arguments passed into the panel.'),
  108. '#type' => 'textfield',
  109. '#maxlength' => 512,
  110. '#autocomplete_path' => 'ctools/autocomplete/node',
  111. '#weight' => -10,
  112. '#suffix' => '</div>',
  113. );
  114. }
  115. else {
  116. $form['nid'] = array(
  117. '#type' => 'value',
  118. '#value' => $conf['nid'],
  119. );
  120. }
  121. $form['links'] = array(
  122. '#type' => 'checkbox',
  123. '#default_value' => $conf['links'],
  124. '#title' => t('Include node links for "add comment", "read more" etc.'),
  125. );
  126. $form['identifier'] = array(
  127. '#type' => 'textfield',
  128. '#default_value' => !empty($conf['identifier']) ? $conf['identifier'] : '',
  129. '#title' => t('Template identifier'),
  130. '#description' => t('This identifier will be added as a template suggestion to display this node: node--panel--IDENTIFIER.tpl.php. Please see the Drupal theming guide for information about template suggestions.'),
  131. );
  132. $entity = entity_get_info('node');
  133. $build_mode_options = array();
  134. foreach ($entity['view modes'] as $mode => $option) {
  135. $build_mode_options[$mode] = $option['label'];
  136. }
  137. // Handle existing configurations with the deprecated 'teaser' option.
  138. // Also remove the teaser key from the form_state.
  139. if (isset($conf['teaser']) || !isset($conf['build_mode'])) {
  140. unset($form_state['conf']['teaser']);
  141. $conf['build_mode'] = $conf['teaser'] ? 'teaser' : 'full';
  142. }
  143. $form['build_mode'] = array(
  144. '#title' => t('Build mode'),
  145. '#type' => 'select',
  146. '#description' => t('Select a build mode for this node.'),
  147. '#options' => $build_mode_options,
  148. '#default_value' => $conf['build_mode'],
  149. );
  150. return $form;
  151. }
  152. /**
  153. * Validate the node selection.
  154. */
  155. function ctools_node_content_type_edit_form_validate(&$form, &$form_state) {
  156. if ($form_state['op'] != 'add') {
  157. return;
  158. }
  159. $nid = $form_state['values']['nid'];
  160. $preg_matches = array();
  161. $match = preg_match('/\[id: (\d+)\]/', $nid, $preg_matches);
  162. if (!$match) {
  163. $match = preg_match('/^id: (\d+)/', $nid, $preg_matches);
  164. }
  165. if ($match) {
  166. $nid = $preg_matches[1];
  167. }
  168. if (is_numeric($nid)) {
  169. $node = db_query('SELECT nid, status FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
  170. }
  171. else {
  172. $node = db_query('SELECT nid, status FROM {node} WHERE LOWER(title) = LOWER(:title)', array(':title' => $nid))->fetchObject();
  173. }
  174. if ($node) {
  175. $form_state['values']['nid'] = $node->nid;
  176. }
  177. if (!($node || preg_match('/^[@%]\d+$/', $nid)) ||
  178. // Do not allow unpublished nodes to be selected by unprivileged users
  179. (empty($node->status) && !user_access('administer nodes'))) {
  180. form_error($form['nid'], t('Invalid node'));
  181. }
  182. }
  183. /**
  184. * Validate the node selection.
  185. */
  186. function ctools_node_content_type_edit_form_submit($form, &$form_state) {
  187. foreach (array('nid', 'links', 'leave_node_title', 'link_node_title', 'identifier', 'build_mode') as $key) {
  188. $form_state['conf'][$key] = $form_state['values'][$key];
  189. }
  190. }
  191. /**
  192. * Returns the administrative title for a node.
  193. */
  194. function ctools_node_content_type_admin_title($subtype, $conf) {
  195. if (!is_numeric($conf['nid'])) {
  196. return t('Node loaded from @var', array('@var' => $conf['nid']));
  197. }
  198. $node = node_load($conf['nid']);
  199. if ($node) {
  200. if (!empty($node->status) || user_access('administer nodes')) {
  201. return check_plain($node->title);
  202. }
  203. else {
  204. return t('Unpublished node @nid', array('@nid' => $conf['nid']));
  205. }
  206. }
  207. else {
  208. return t('Deleted/missing node @nid', array('@nid' => $conf['nid']));
  209. }
  210. }
  211. /**
  212. * Display the administrative information for a node pane.
  213. */
  214. function ctools_node_content_type_admin_info($subtype, $conf) {
  215. // Just render the node.
  216. return ctools_node_content_type_render($subtype, $conf, array());
  217. }