node.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide a node context. A node context is a node wrapped in a
  5. * context object that can be utilized by anything that accepts contexts.
  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("Node"),
  13. 'description' => t('A node object.'),
  14. 'context' => 'ctools_context_create_node',
  15. 'edit form' => 'ctools_context_node_settings_form',
  16. 'defaults' => array('nid' => ''),
  17. 'keyword' => 'node',
  18. 'context name' => 'node',
  19. 'convert list' => 'ctools_context_node_convert_list',
  20. 'convert' => 'ctools_context_node_convert',
  21. 'placeholder form' => array(
  22. '#type' => 'textfield',
  23. '#description' => t('Enter the node ID of a node for this context.'),
  24. ),
  25. // This context is deprecated and should not be usable in the UI.
  26. 'no ui' => TRUE,
  27. 'no required context ui' => TRUE,
  28. 'superceded by' => 'entity:node',
  29. );
  30. /**
  31. * It's important to remember that $conf is optional here, because contexts
  32. * are not always created from the UI.
  33. */
  34. function ctools_context_create_node($empty, $data = NULL, $conf = FALSE) {
  35. $context = new ctools_context('node');
  36. $context->plugin = 'node';
  37. if ($empty) {
  38. return $context;
  39. }
  40. if ($conf) {
  41. $nid = is_array($data) && isset($data['nid']) ? $data['nid'] : (is_object($data) ? $data->nid : 0);
  42. if (module_exists('translation')) {
  43. if ($translation = module_invoke('translation', 'node_nid', $nid, $GLOBALS['language']->language)) {
  44. $nid = $translation;
  45. $reload = TRUE;
  46. }
  47. }
  48. if (is_array($data) || !empty($reload)) {
  49. $data = node_load($nid);
  50. }
  51. }
  52. if (!empty($data)) {
  53. $context->data = $data;
  54. $context->title = $data->title;
  55. $context->argument = $data->nid;
  56. $context->restrictions['type'] = array($data->type);
  57. return $context;
  58. }
  59. }
  60. function ctools_context_node_settings_form($form, &$form_state) {
  61. $conf = &$form_state['conf'];
  62. $form['node'] = array(
  63. '#title' => t('Enter the title or NID of a node'),
  64. '#type' => 'textfield',
  65. '#maxlength' => 512,
  66. '#autocomplete_path' => 'ctools/autocomplete/node',
  67. '#weight' => -10,
  68. );
  69. if (!empty($conf['nid'])) {
  70. $info = db_query('SELECT * FROM {node} WHERE nid = :nid', array(':nid' => $conf['nid']))->fetchObject();
  71. if ($info) {
  72. $link = l(t("'%title' [node id %nid]", array('%title' => $info->title, '%nid' => $info->nid)), "node/$info->nid", array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
  73. $form['node']['#description'] = t('Currently set to !link', array('!link' => $link));
  74. }
  75. }
  76. $form['nid'] = array(
  77. '#type' => 'value',
  78. '#value' => $conf['nid'],
  79. );
  80. $form['set_identifier'] = array(
  81. '#type' => 'checkbox',
  82. '#default_value' => FALSE,
  83. '#title' => t('Reset identifier to node title'),
  84. '#description' => t('If checked, the identifier will be reset to the node title of the selected node.'),
  85. );
  86. return $form;
  87. }
  88. /**
  89. * Validate a node.
  90. */
  91. function ctools_context_node_settings_form_validate($form, &$form_state) {
  92. // Validate the autocomplete.
  93. if (empty($form_state['values']['nid']) && empty($form_state['values']['node'])) {
  94. form_error($form['node'], t('You must select a node.'));
  95. return;
  96. }
  97. if (empty($form_state['values']['node'])) {
  98. return;
  99. }
  100. $nid = $form_state['values']['node'];
  101. $preg_matches = array();
  102. $match = preg_match('/\[id: (\d+)\]/', $nid, $preg_matches);
  103. if (!$match) {
  104. $match = preg_match('/^id: (\d+)/', $nid, $preg_matches);
  105. }
  106. if ($match) {
  107. $nid = $preg_matches[1];
  108. }
  109. if (is_numeric($nid)) {
  110. $node = db_query('SELECT nid, status FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
  111. }
  112. else {
  113. $node = db_query('SELECT nid, status FROM {node} WHERE LOWER(title) = LOWER(:title)', array(':title' => $nid))->fetchObject();
  114. }
  115. // Do not allow unpublished nodes to be selected by unprivileged users.
  116. if (!$node || (empty($node->status) && !(user_access('administer nodes')))) {
  117. form_error($form['node'], t('Invalid node selected.'));
  118. }
  119. else {
  120. form_set_value($form['nid'], $node->nid, $form_state);
  121. }
  122. }
  123. function ctools_context_node_settings_form_submit($form, &$form_state) {
  124. if ($form_state['values']['set_identifier']) {
  125. $node = node_load($form_state['values']['nid']);
  126. $form_state['values']['identifier'] = $node->title;
  127. }
  128. // This will either be the value set previously or a value set by the
  129. // validator.
  130. $form_state['conf']['nid'] = $form_state['values']['nid'];
  131. }
  132. /**
  133. * Provide a list of ways that this context can be converted to a string.
  134. */
  135. function ctools_context_node_convert_list() {
  136. $tokens = token_info();
  137. foreach ($tokens['tokens']['node'] as $id => $info) {
  138. if (!isset($list[$id])) {
  139. $list[$id] = $info['name'];
  140. }
  141. }
  142. return $list;
  143. }
  144. /**
  145. * Convert a context into a string.
  146. */
  147. function ctools_context_node_convert($context, $type) {
  148. $tokens = token_info();
  149. if (isset($tokens['tokens']['node'][$type])) {
  150. $values = token_generate('node', array($type => $type), array('node' => $context->data));
  151. if (isset($values[$type])) {
  152. return $values[$type];
  153. }
  154. }
  155. }