node_edit_form.inc 6.0 KB

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