node_content.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * @file
  4. * Plugins are described by creating a $plugin array which will be used
  5. * by the system that includes this file.
  6. */
  7. $plugin = array(
  8. 'single' => TRUE,
  9. 'title' => t('Node content'),
  10. 'icon' => 'icon_node.png',
  11. 'description' => t('The content of the referenced node.'),
  12. 'required context' => new ctools_context_required(t('Node'), 'node'),
  13. 'category' => t('Node'),
  14. 'defaults' => array(
  15. 'links' => TRUE,
  16. 'no_extras' => TRUE,
  17. 'override_title' => FALSE,
  18. 'override_title_text' => '',
  19. 'identifier' => '',
  20. 'link' => TRUE,
  21. 'leave_node_title' => FALSE,
  22. 'build_mode' => 'teaser',
  23. ),
  24. );
  25. /**
  26. * Render the node content.
  27. */
  28. function ctools_node_content_content_type_render($subtype, $conf, $panel_args, $context) {
  29. if (!empty($context) && (empty($context->data) || empty($context->data->nid))) {
  30. return;
  31. }
  32. $node = isset($context->data) ? clone $context->data : NULL;
  33. $block = new stdClass();
  34. $block->module = 'node';
  35. $block->delta = $node->nid;
  36. if (empty($node)) {
  37. $block->delta = 'placeholder';
  38. $block->title = t('Node title.');
  39. $block->content = t('Node content goes here.');
  40. }
  41. else {
  42. if (!empty($conf['identifier'])) {
  43. $node->ctools_template_identifier = $conf['identifier'];
  44. }
  45. $block->title = $node->title;
  46. if (empty($conf['leave_node_title'])) {
  47. $node->title = NULL;
  48. }
  49. $block->content = ctools_node_content_render_node($node, $conf);
  50. }
  51. if (!empty($conf['link']) && $node) {
  52. $block->title_link = "node/$node->nid";
  53. }
  54. return $block;
  55. }
  56. function ctools_node_content_render_node($node, $conf) {
  57. if (empty($node->content)) {
  58. // Copied from node_build_content() so we can fiddle with it as we render.
  59. $node->content = array();
  60. // The 'view' hook can be implemented to overwrite the default function
  61. // to display nodes.
  62. if (node_hook($node, 'view')) {
  63. $node = node_invoke($node, 'view', $conf['build_mode']);
  64. }
  65. // Build fields content.
  66. // In case of a multiple view, node_view_multiple() already ran the
  67. // 'prepare_view' step. An internal flag prevents the operation from running
  68. // twice.
  69. field_attach_prepare_view('node', array($node->nid => $node), $conf['build_mode']);
  70. entity_prepare_view('node', array($node->nid => $node));
  71. $node->content += field_attach_view('node', $node, $conf['build_mode']);
  72. // Always display a read more link on teasers because we have no way
  73. // to know when a teaser view is different than a full view.
  74. $links = array();
  75. if ($conf['build_mode'] == 'teaser') {
  76. $links['node-readmore'] = array(
  77. 'title' => t('Read more'),
  78. 'href' => 'node/' . $node->nid,
  79. 'attributes' => array('rel' => 'tag', 'title' => strip_tags($node->title)),
  80. );
  81. }
  82. $node->content['links'] = array(
  83. '#theme' => 'links__node',
  84. '#links' => $links,
  85. '#attributes' => array('class' => array('links', 'inline')),
  86. );
  87. if (empty($conf['no_extras'])) {
  88. // Allow modules to make their own additions to the node.
  89. $langcode = $GLOBALS['language_content']->language;
  90. module_invoke_all('node_view', $node, $conf['build_mode'], $langcode);
  91. module_invoke_all('entity_view', $node, 'node', $conf['build_mode'], $langcode);
  92. }
  93. }
  94. // Set the proper node part, then unset unused $node part so that a bad
  95. // theme can not open a security hole.
  96. $content = $node->content;
  97. $content += array(
  98. '#theme' => 'node',
  99. '#node' => $node,
  100. '#view_mode' => $conf['build_mode'],
  101. '#language' => NULL,
  102. );
  103. // Add contextual links for this node, except when the node is already being
  104. // displayed on its own page. Modules may alter this behavior (for example,
  105. // to restrict contextual links to certain view modes) by implementing
  106. // hook_node_view_alter().
  107. if (!empty($node->nid) && !($conf['build_mode'] == 'full' && node_is_page($node))) {
  108. $content['#contextual_links']['node'] = array('node', array($node->nid));
  109. }
  110. // Allow modules to modify the structured node.
  111. $type = 'node';
  112. drupal_alter(array('node_view', 'entity_view'), $content, $type);
  113. // Kill the links if not requested.
  114. if (!$conf['links']) {
  115. $content['links']['#access'] = FALSE;
  116. }
  117. return $content;
  118. }
  119. /**
  120. * Returns an edit form for the custom type.
  121. */
  122. function ctools_node_content_content_type_edit_form($form, &$form_state) {
  123. $conf = $form_state['conf'];
  124. $form['leave_node_title'] = array(
  125. '#type' => 'checkbox',
  126. '#default_value' => !empty($conf['leave_node_title']),
  127. '#title' => t('Leave node title'),
  128. '#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.'),
  129. );
  130. $form['link'] = array(
  131. '#title' => t('Link title to node'),
  132. '#type' => 'checkbox',
  133. '#default_value' => $conf['link'],
  134. '#description' => t('Check here to make the title link to the node.'),
  135. );
  136. $form['links'] = array(
  137. '#type' => 'checkbox',
  138. '#default_value' => $conf['links'],
  139. '#title' => t('Include node links for "add comment", "read more" etc.'),
  140. );
  141. $form['no_extras'] = array(
  142. '#type' => 'checkbox',
  143. '#default_value' => $conf['no_extras'],
  144. '#title' => t('No extras'),
  145. '#description' => t('Check here to disable additions that modules might make to the node, such as file attachments and CCK fields; this should just display the basic teaser or body.'),
  146. );
  147. $form['identifier'] = array(
  148. '#type' => 'textfield',
  149. '#default_value' => $conf['identifier'],
  150. '#title' => t('Template identifier'),
  151. '#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.'),
  152. );
  153. $entity = entity_get_info('node');
  154. $build_mode_options = array();
  155. foreach ($entity['view modes'] as $mode => $option) {
  156. $build_mode_options[$mode] = $option['label'];
  157. }
  158. $form['build_mode'] = array(
  159. '#title' => t('Build mode'),
  160. '#type' => 'select',
  161. '#description' => t('Select a build mode for this node.'),
  162. '#options' => $build_mode_options,
  163. '#default_value' => $conf['build_mode'],
  164. );
  165. return $form;
  166. }
  167. function ctools_node_content_content_type_edit_form_submit($form, &$form_state) {
  168. // Copy everything from our defaults.
  169. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  170. $form_state['conf'][$key] = $form_state['values'][$key];
  171. }
  172. }
  173. function ctools_node_content_content_type_admin_title($subtype, $conf, $context) {
  174. return t('"@s" content', array('@s' => $context->identifier));
  175. }