1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
*
|
6 |
|
|
* Plugin to provide a node_add_form context
|
7 |
|
|
*/
|
8 |
|
|
|
9 |
|
|
/**
|
10 |
|
|
* Plugins are described by creating a $plugin array which will be used
|
11 |
|
|
* by the system that includes this file.
|
12 |
|
|
*/
|
13 |
|
|
$plugin = array(
|
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.'),
|
26 |
|
|
),
|
27 |
|
|
);
|
28 |
|
|
|
29 |
|
|
/**
|
30 |
|
|
* It's important to remember that $conf is optional here, because contexts
|
31 |
|
|
* are not always created from the UI.
|
32 |
|
|
*/
|
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';
|
37 |
|
|
|
38 |
|
|
if ($empty || ($creating)) {
|
39 |
|
|
return $context;
|
40 |
|
|
}
|
41 |
|
|
$creating = TRUE;
|
42 |
|
|
|
43 |
|
|
if ($conf && (isset($data['types']) || isset($data['type']))) {
|
44 |
|
|
// Holdover from typo'd config.
|
45 |
|
|
$data = isset($data['types']) ? $data['types'] : $data['type'];
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
if (!empty($data)) {
|
49 |
|
|
$types = node_type_get_types();
|
50 |
|
|
$type = str_replace('-', '_', $data);
|
51 |
|
|
|
52 |
|
|
// Validate the node type exists.
|
53 |
|
|
if (isset($types[$type]) && node_access('create', $type)) {
|
54 |
|
|
// Initialize settings:
|
55 |
|
|
global $user;
|
56 |
|
|
$node = (object) array(
|
57 |
|
|
'uid' => $user->uid,
|
58 |
|
|
'name' => (isset($user->name) ? $user->name : ''),
|
59 |
|
|
'type' => $type,
|
60 |
|
|
'language' => LANGUAGE_NONE,
|
61 |
|
|
);
|
62 |
|
|
|
63 |
|
|
$form_id = $type . '_node_form';
|
64 |
|
|
|
65 |
|
|
$form_state = array(
|
66 |
|
|
'want form' => TRUE,
|
67 |
|
|
'build_info' => array(
|
68 |
|
|
'args' => array($node)
|
69 |
|
|
)
|
70 |
|
|
);
|
71 |
|
|
|
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');
|
74 |
|
|
|
75 |
|
|
$form = drupal_build_form($form_id, $form_state);
|
76 |
|
|
|
77 |
|
|
// In a form, $data is the object being edited.
|
78 |
|
|
$context->data = $node;
|
79 |
|
|
$context->title = $types[$type]->name;
|
80 |
|
|
$context->argument = $type;
|
81 |
|
|
|
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');
|
90 |
|
|
|
91 |
|
|
$creating = FALSE;
|
92 |
|
|
return $context;
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
$creating = FALSE;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
function ctools_context_node_add_form_settings_form($form, &$form_state) {
|
99 |
|
|
$conf = $form_state['conf'];
|
100 |
|
|
|
101 |
|
|
$form['type'] = array(
|
102 |
|
|
'#title' => t('Node type'),
|
103 |
|
|
'#type' => 'select',
|
104 |
|
|
'#options' => node_type_get_names(),
|
105 |
|
|
'#default_value' => $conf['type'],
|
106 |
|
|
'#description' => t('Select the node type for this form.'),
|
107 |
|
|
);
|
108 |
|
|
|
109 |
|
|
return $form;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
function ctools_context_node_add_form_settings_form_submit($form, &$form_state) {
|
113 |
|
|
$form_state['conf']['type'] = $form_state['values']['type'];
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
/**
|
117 |
|
|
* Convert a context into a string.
|
118 |
|
|
*/
|
119 |
|
|
function ctools_context_node_add_form_convert($context, $type) {
|
120 |
|
|
switch ($type) {
|
121 |
|
|
case 'type':
|
122 |
|
|
return $context->data->type;
|
123 |
|
|
}
|
124 |
|
|
} |