node.inc 5.2 KB

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