node_add_form.inc 3.5 KB

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