6 * Plugin to provide a node_add_form context
10 * Plugins are described by creating a $plugin array which will be used
11 * by the system that includes this file.
14 'title' => t('Node add form'),
15 'description' => t('A node add form.'),
16 'context' => 'ctools_context_create_node_add_form',
17 'edit form' => 'ctools_context_node_add_form_settings_form',
18 'defaults' => array('type' => ''),
19 'keyword' => 'node_add',
20 'context name' => 'node_add_form',
21 'convert list' => array('type' => t('Node type')),
22 'convert' => 'ctools_context_node_add_form_convert',
23 'placeholder form' => array(
24 '#type' => 'textfield',
25 '#description' => t('Enter the node type this context.'),
30 * It's important to remember that $conf is optional here, because contexts
31 * are not always created from the UI.
33 function ctools_context_create_node_add_form($empty, $data = NULL, $conf = FALSE) {
34 static $creating = FALSE;
35 $context = new ctools_context(array('form', 'node_add', 'node_form', 'node', 'entity:node'));
36 $context->plugin = 'node_add_form';
38 if ($empty || ($creating)) {
43 if ($conf && (isset($data['types']) || isset($data['type']))) {
44 // Holdover from typo'd config.
45 $data = isset($data['types']) ? $data['types'] : $data['type'];
49 $types = node_type_get_types();
50 $type = str_replace('-', '_', $data);
52 // Validate the node type exists.
53 if (isset($types[$type]) && node_access('create', $type)) {
54 // Initialize settings:
56 $node = (object) array(
58 'name' => (isset($user->name) ? $user->name : ''),
60 'language' => LANGUAGE_NONE,
63 $form_id = $type . '_node_form';
67 'build_info' => array(
68 'args' => array($node)
72 // Use module_load_include so that caches and stuff can know to load this.
73 form_load_include($form_state, 'inc', 'node', 'node.pages');
75 $form = drupal_build_form($form_id, $form_state);
77 // In a form, $data is the object being edited.
78 $context->data = $node;
79 $context->title = $types[$type]->name;
80 $context->argument = $type;
82 // These are specific pieces of data to this form.
83 // All forms should place the form here.
84 $context->form = $form;
85 $context->form_id = $form_id;
86 $context->form_title = t('Submit @name', array('@name' => $types[$type]->name));
87 $context->node_type = $type;
88 $context->restrictions['type'] = array($type);
89 $context->restrictions['form'] = array('form');
98 function ctools_context_node_add_form_settings_form($form, &$form_state) {
99 $conf = $form_state['conf'];
101 $form['type'] = array(
102 '#title' => t('Node type'),
104 '#options' => node_type_get_names(),
105 '#default_value' => $conf['type'],
106 '#description' => t('Select the node type for this form.'),
112 function ctools_context_node_add_form_settings_form_submit($form, &$form_state) {
113 $form_state['conf']['type'] = $form_state['values']['type'];
117 * Convert a context into a string.
119 function ctools_context_node_add_form_convert($context, $type) {
122 return $context->data->type;