node_add_form.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Plugin to provide a node_add_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 add form'),
  13. 'description' => t('A node add form.'),
  14. 'context' => 'ctools_context_create_node_add_form',
  15. 'edit form' => 'ctools_context_node_add_form_settings_form',
  16. 'defaults' => array('type' => ''),
  17. 'keyword' => 'node_add',
  18. 'context name' => 'node_add_form',
  19. 'convert list' => array('type' => t('Node type')),
  20. 'convert' => 'ctools_context_node_add_form_convert',
  21. 'placeholder form' => array(
  22. '#type' => 'textfield',
  23. '#description' => t('Enter the node type this context.'),
  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_add_form($empty, $data = NULL, $conf = FALSE) {
  31. static $creating = FALSE;
  32. $context = new ctools_context(array('form', 'node_add', 'node_form', 'node', 'entity:node'));
  33. $context->plugin = 'node_add_form';
  34. if ($empty || ($creating)) {
  35. return $context;
  36. }
  37. $creating = TRUE;
  38. if ($conf && (isset($data['types']) || isset($data['type']))) {
  39. // Holdover from typo'd config.
  40. $data = isset($data['types']) ? $data['types'] : $data['type'];
  41. }
  42. if (!empty($data)) {
  43. $types = node_type_get_types();
  44. $type = str_replace('-', '_', $data);
  45. // Validate the node type exists.
  46. if (isset($types[$type]) && node_access('create', $type)) {
  47. // Initialize settings:
  48. global $user;
  49. $node = (object) array(
  50. 'uid' => $user->uid,
  51. 'name' => (isset($user->name) ? $user->name : ''),
  52. 'type' => $type,
  53. 'language' => LANGUAGE_NONE,
  54. );
  55. $form_id = $type . '_node_form';
  56. $form_state = array(
  57. 'want form' => TRUE,
  58. 'build_info' => array(
  59. 'args' => array($node)
  60. )
  61. );
  62. // Use module_load_include so that caches and stuff can know to load this.
  63. form_load_include($form_state, 'inc', 'node', 'node.pages');
  64. $form = drupal_build_form($form_id, $form_state);
  65. // In a form, $data is the object being edited.
  66. $context->data = $node;
  67. $context->title = $types[$type]->name;
  68. $context->argument = $type;
  69. // These are specific pieces of data to this form.
  70. // All forms should place the form here.
  71. $context->form = $form;
  72. $context->form_id = $form_id;
  73. $context->form_title = t('Submit @name', array('@name' => $types[$type]->name));
  74. $context->node_type = $type;
  75. $context->restrictions['type'] = array($type);
  76. $context->restrictions['form'] = array('form');
  77. $creating = FALSE;
  78. return $context;
  79. }
  80. }
  81. $creating = FALSE;
  82. }
  83. function ctools_context_node_add_form_settings_form($form, &$form_state) {
  84. $conf = $form_state['conf'];
  85. $form['type'] = array(
  86. '#title' => t('Node type'),
  87. '#type' => 'select',
  88. '#options' => node_type_get_names(),
  89. '#default_value' => $conf['type'],
  90. '#description' => t('Select the node type for this form.'),
  91. );
  92. return $form;
  93. }
  94. function ctools_context_node_add_form_settings_form_submit($form, &$form_state) {
  95. $form_state['conf']['type'] = $form_state['values']['type'];
  96. }
  97. /**
  98. * Convert a context into a string.
  99. */
  100. function ctools_context_node_add_form_convert($context, $type) {
  101. switch ($type) {
  102. case 'type':
  103. return $context->data->type;
  104. }
  105. }