node_edit_form.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Plugin to provide a node_edit_form context
  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 edit form"),
  13. 'description' => t('A node edit form.'),
  14. 'context' => 'ctools_context_create_node_edit_form',
  15. 'edit form' => 'ctools_context_node_edit_form_settings_form',
  16. 'defaults' => array('nid' => ''),
  17. 'keyword' => 'node_edit',
  18. 'context name' => 'node_edit_form',
  19. 'convert list' => 'ctools_context_node_edit_convert_list',
  20. 'convert' => 'ctools_context_node_edit_convert',
  21. 'placeholder form' => array(
  22. '#type' => 'textfield',
  23. '#description' => t('Enter the node ID of a node for this argument:'),
  24. ),
  25. );
  26. /**
  27. * It's important to remember that $conf is optional here, because contexts
  28. * are not always created from the UI.
  29. */
  30. function ctools_context_create_node_edit_form($empty, $node = NULL, $conf = FALSE) {
  31. static $creating = FALSE;
  32. $context = new ctools_context(array('form', 'node_edit', 'node_form', 'node_edit_form', 'node', 'entity:node'));
  33. $context->plugin = 'node_edit_form';
  34. if ($empty || ($creating)) {
  35. return $context;
  36. }
  37. $creating = TRUE;
  38. if ($conf) {
  39. // In this case, $node is actually our $conf array.
  40. $nid = is_array($node) && isset($node['nid']) ? $node['nid'] : (is_object($node) ? $node->nid : 0);
  41. if (module_exists('translation')) {
  42. if ($translation = module_invoke('translation', 'node_nid', $nid, $GLOBALS['language']->language)) {
  43. $nid = $translation;
  44. $reload = TRUE;
  45. }
  46. }
  47. if (is_array($node) || !empty($reload)) {
  48. $node = node_load($nid);
  49. }
  50. }
  51. if (!empty($node)) {
  52. $form_id = $node->type . '_node_form';
  53. $form_state = array('want form' => TRUE, 'build_info' => array('args' => array($node)));
  54. $file = drupal_get_path('module', 'node') . '/node.pages.inc';
  55. require_once DRUPAL_ROOT . '/' . $file;
  56. // This piece of information can let other modules know that more files
  57. // need to be included if this form is loaded from cache:
  58. $form_state['build_info']['files'] = array($file);
  59. $form = drupal_build_form($form_id, $form_state);
  60. // Fill in the 'node' portion of the context
  61. $context->data = $node;
  62. $context->title = isset($node->title) ? $node->title : '';
  63. $context->argument = isset($node->nid) ? $node->nid : $node->type;
  64. $context->form = $form;
  65. $context->form_state = &$form_state;
  66. $context->form_id = $form_id;
  67. $context->form_title = isset($node->title) ? $node->title : '';
  68. $context->node_type = $node->type;
  69. $context->restrictions['type'] = array($node->type);
  70. $context->restrictions['form'] = array('form');
  71. $creating = FALSE;
  72. return $context;
  73. }
  74. $creating = FALSE;
  75. }
  76. function ctools_context_node_edit_form_settings_form($form, &$form_state) {
  77. $conf = &$form_state['conf'];
  78. $form['node'] = array(
  79. '#title' => t('Enter the title or NID of a node'),
  80. '#type' => 'textfield',
  81. '#maxlength' => 512,
  82. '#autocomplete_path' => 'ctools/autocomplete/node',
  83. '#weight' => -10,
  84. );
  85. if (!empty($conf['nid'])) {
  86. $info = db_query('SELECT * FROM {node} WHERE nid = :nid', array(':nid' => $conf['nid']))->fetchObject();
  87. if ($info) {
  88. $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));
  89. $form['node']['#description'] = t('Currently set to !link', array('!link' => $link));
  90. }
  91. }
  92. $form['nid'] = array(
  93. '#type' => 'value',
  94. '#value' => $conf['nid'],
  95. );
  96. $form['set_identifier'] = array(
  97. '#type' => 'checkbox',
  98. '#default_value' => FALSE,
  99. '#title' => t('Reset identifier to node title'),
  100. '#description' => t('If checked, the identifier will be reset to the node title of the selected node.'),
  101. );
  102. return $form;
  103. }
  104. /**
  105. * Validate a node.
  106. */
  107. function ctools_context_node_edit_form_settings_form_validate($form, &$form_state) {
  108. // Validate the autocomplete
  109. if (empty($form_state['values']['nid']) && empty($form_state['values']['node'])) {
  110. form_error($form['node'], t('You must select a node.'));
  111. return;
  112. }
  113. if (empty($form_state['values']['node'])) {
  114. return;
  115. }
  116. $nid = $form_state['values']['node'];
  117. $preg_matches = array();
  118. $match = preg_match('/\[id: (\d+)\]/', $nid, $preg_matches);
  119. if (!$match) {
  120. $match = preg_match('/^id: (\d+)/', $nid, $preg_matches);
  121. }
  122. if ($match) {
  123. $nid = $preg_matches[1];
  124. }
  125. if (is_numeric($nid)) {
  126. $node = db_query('SELECT nid, status FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
  127. }
  128. else {
  129. $node = db_query('SELECT nid, status FROM {node} WHERE LOWER(title) = LOWER(:title)', array(':title' => $nid))->fetchObject();
  130. }
  131. // Do not allow unpublished nodes to be selected by unprivileged users
  132. if (!$node || (empty($node->status) && !(user_access('administer nodes')))) {
  133. form_error($form['node'], t('Invalid node selected.'));
  134. }
  135. else {
  136. form_set_value($form['nid'], $node->nid, $form_state);
  137. }
  138. }
  139. function ctools_context_node_edit_form_settings_form_submit($form, &$form_state) {
  140. if ($form_state['values']['set_identifier']) {
  141. $node = node_load($form_state['values']['nid']);
  142. $form_state['values']['identifier'] = $node->title;
  143. }
  144. // This will either be the value set previously or a value set by the
  145. // validator.
  146. $form_state['conf']['nid'] = $form_state['values']['nid'];
  147. }
  148. /**
  149. * Provide a list of ways that this context can be converted to a string.
  150. */
  151. function ctools_context_node_edit_convert_list() {
  152. // Pass through to the "node" context convert list.
  153. $plugin = ctools_get_context('node');
  154. return ctools_context_node_convert_list();
  155. }
  156. /**
  157. * Convert a context into a string.
  158. */
  159. function ctools_context_node_edit_convert($context, $type) {
  160. // Pass through to the "node" context convert list.
  161. $plugin = ctools_get_context('node');
  162. return ctools_context_node_convert($context, $type);
  163. }