forked from publiclab/plots
/
nodeformcols.module
executable file
·236 lines (211 loc) · 7.13 KB
/
nodeformcols.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
// $Id: nodeformcols.module,v 1.7 2009/09/21 06:09:54 hugowetterberg Exp $
define('NODEFORMCOLS_DEFAULT_REGION', 'main');
/**
* Implementation of hook_theme().
*/
function nodeformcols_theme($aExisting) {
return array(
// This needs to run after node.module's hook_theme(), which we ensure
// by setting this module's weight to 1 during install.
'node_form' => array(
'template' => 'node-form',
),
'nodeformcols_configuration' => array(
'template' => 'nodeformcols-configuration',
'arguments' => array('element' => array()),
),
);
}
/**
* Implementation of hook_menu().
*/
function nodeformcols_menu() {
$items = array();
if (!defined('MAINTENANCE_MODE')) {
foreach (node_get_types() as $type) {
$type_name = $type->type;
$type_url_str = str_replace('_', '-', $type_name);
$items['admin/content/node-type/'. $type_url_str .'/form'] = array(
'title' => 'Manage form',
'page callback' => 'drupal_get_form',
'page arguments' => array('nodeformcols_configuration_form', $type_name, 5),
'access arguments' => array('administer content types'),
'file' => 'nodeformcols.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 3
);
}
}
return $items;
}
/**
* Implementation of hook_ctools_plugin_directory().
*/
function nodeformcols_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools') {
return 'plugins/' . $plugin;
}
}
function nodeformcols_form_regions() {
return array(
'main' => t('Main column'),
'right' => t('Right'),
'footer' => t('Footer'),
);
}
/**
* Gets default placements for standard fields
*
* @return array
*/
function _nodeformscols_default_field_placements() {
return array(
'title' => array('region' => 'main'),
'body_field' => array('region' => 'main'),
'menu' => array('region' => 'right'),
'revision_information' => array('region' => 'right'),
'comment_settings' => array('region' => 'right'),
'path' => array('region' => 'right'),
'options' => array('region' => 'right'),
'author' => array('region' => 'right'),
'buttons' => array('region' => NODEFORMCOLS_DEFAULT_REGION, 'weight' => 100),
);
}
function nodeformscols_field_placements($content_type, $variant) {
$default = _nodeformscols_default_field_placements();
if ($variant != 'default') {
$default = variable_get('nodeformscols_field_placements_' .
$content_type . '_default', $default);
}
$placements = variable_get('nodeformscols_field_placements_' . $content_type .
'_' . $variant, $default);
return $placements;
}
function nodeformscols_content_extra_fields($type_name, $variant) {
return array(
'buttons' => array(
'#title' => t('Buttons'),
'#description' => t('Save, preview and delete buttons.'),
'#weight' => 50,
),
'options' => array(
'#title' => t('Workflow options'),
'#description' => t('Options for publishing, sticky and publish on front page.'),
'#weight' => 0,
),
);
}
/**
* Implementation of hook_form_alter().
*/
function nodeformcols_form_alter(&$form, $form_state, $form_id) {
if ('node-form' == $form['#id']) {
drupal_alter('nodeformcols_pre_form', $form);
$variant = isset($form['#nodeformcols_variant']) ? $form['#nodeformcols_variant'] : 'default';
$placements = nodeformscols_field_placements($form['#node']->type, $variant);
foreach ($placements as $key => $p) {
if (isset($p['hidden']) && $p['hidden']) {
$form[$key]['#access'] = FALSE;
}
}
drupal_alter('nodeformcols_post_form', $form);
}
}
/**
* Preprocess function to run ahead of other modules.
*/
function template_preprocess_node_form(&$aVars) {
drupal_add_css(drupal_get_path('module', 'nodeformcols') . '/css/nodeformcols.css');
$default_region = variable_get('nodeformcols_default_region', NODEFORMCOLS_DEFAULT_REGION);
$form = &$aVars['form'];
$class = array('node-form');
$regions = array();
$has_elements = array();
$weight = 0;
foreach (nodeformcols_form_regions() as $name => $title) {
$regions[$name] = array(
'#prefix' => '<div class="form-region-' . $name . '">',
'#suffix' => '</div>',
'#weight' => $weight,
);
$weight++;
}
drupal_alter('nodeformcols_pre_placement', $form);
$variant = isset($form['#nodeformcols_variant']) ? $form['#nodeformcols_variant'] : 'default';
$placements = nodeformscols_field_placements($form['#node']->type, $variant);
// Track if new fields should be adjusted above the buttons.
// TODO: This should be generalized to a way to tell nodeformcols where to place new fields (above below field X).
$adjust_to_buttons = $placements['buttons']['region'] == $default_region;
foreach ($form as $key => $field) {
if (substr($key, 0, 1)=='#' ||
$field['#type']=='value' || $field['#type']=='hidden'|| $field['#type']=='token') {
continue;
}
if (isset($placements[$key])) {
$p = $placements[$key];
if (isset($p['weight'])) {
$field['#weight'] = $p['weight'];
}
if (isset($p['collapsed']) && isset($field['#collapsible']) && $field['#collapsible']) {
$field['#collapsed'] = $p['collapsed'];
}
$regions[$p['region']][$key] = $field;
$has_elements[$p['region']] = TRUE;
unset($form[$key]);
}
else { // Set the default placement for unknown fields
$regions[$default_region][$key] = $field;
if ($adjust_to_buttons && $regions[$default_region][$key]['#weight'] >= $placements['buttons']['weight']) {
$regions[$default_region][$key]['#weight'] = $placements['buttons']['weight'] - .1;
}
$has_elements[$default_region] = TRUE;
unset($form[$key]);
}
}
// Ensure that we have the footer wrapper so that
// we clear the floating columns
if (!$has_elements['footer']) {
$has_elements['footer'] = TRUE;
$regions['footer'][] = array(
'#type' => 'markup',
'#value' => ' ',
);
}
foreach ($has_elements as $name => $has) {
if ($has) {
$class[] = 'node-form-has-region-' . $name;
$form['nodeformcols_region_' . $name] = $regions[$name];
}
}
$aVars['class'] = join($class, ' ');
}
/**
* Implementation of hook_node_type().
*/
function nodeformcols_node_type($op, $info) {
switch ($op) {
case 'delete':
db_query("DELETE FROM {variable} WHERE name LIKE '%s'", array(
':name' => 'nodeformscols_field_placements_'. $info->type . '%',
));
break;
case 'update':
if (!empty($info->old_type) && $info->old_type != $info->type) {
$base = 'nodeformscols_field_placements_'. $info->old_type;
$new_base = 'nodeformscols_field_placements_'. $info->type;
$res = db_query("SELECT name FROM {variable} WHERE name LIKE '%s'", array(
':name' => $base . '%',
));
while ($old_name = db_result($res)) {
$new_name = str_replace($base, $new_base, $old_name);
db_query("UPDATE {variable} SET name='%s' WHERE name='%s'", array(
':new' => $new_name,
':old' => $old_name,
));
}
cache_clear_all('variables', 'cache');
}
break;
}
}