-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrf.module
More file actions
executable file
·358 lines (317 loc) · 13.4 KB
/
srf.module
File metadata and controls
executable file
·358 lines (317 loc) · 13.4 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/**
* .module file : required for semi-required field module.
*/
/**
* Include field validate file
*/
module_load_include('inc', 'srf', 'srf.validate');
/**
* Implements hook_form_FORM_ID_alter().
*/
function srf_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
// Transform status field, only for node field instance.
if ($form['#instance']['entity_type'] === 'node') {
// Get default value from conf.
$default_value = 0; // optional
if (isset($form['#instance']['semi_required']) && $form['#instance']['semi_required'] == 1) {
$default_value = 1; // semi required
}
elseif ($form['#instance']['required'] == 1) {
$default_value = 2; // fully required
}
// Rewrite status field (to display radio buttons instead of checkbox).
$form['instance']['required'] = array(
'#type' => 'select',
'#title' => t('Field status'),
'#required' => TRUE,
'#default_value' => $default_value,
'#options' => array(
t('Optional'),
t('Semi-Required'),
t('Required'),
),
'#weight' => -10,
);
// Add a form if the workbench_moderation module is enabled to allow integrations
// with the status transitions
if (module_exists('workbench_moderation')) {
module_load_include('module', 'workbench_moderation');
// Get default value from conf.
$default_value = 0; // optional
if (isset($form['#instance']['workbench_moderation']) &&
workbench_moderation_node_type_moderated($form['#instance']['bundle'])) {
$default_value = $form['#instance']['workbench_moderation'];
}
$form['instance']['workbench_moderation'] = array(
'#type' => 'checkbox',
'#title' => t('Enable integration with the module workbench_moderation.'),
'#default_value' => $default_value,
'#states' => array(
'visible' => array(
'select[name="instance[required]"]' => array('value' => 1)
)
),
'#weight' => -9,
);
if (workbench_moderation_node_type_moderated($form['#instance']['bundle'])) {
// If the moderation is active for this content type
// Get default value from conf.
$default_value = array(); // optional
if (isset($form['#instance']['workbench_moderation_status'])) {
// The default_value for checkboxes shoul be an array always.
$default_value = is_array($form['#instance']['workbench_moderation_status']) ?
$form['#instance']['workbench_moderation_status'] :
array($form['#instance']['workbench_moderation_status']);
}
// Get the status availabes
$options = array();
foreach (workbench_moderation_states() as $machine_name => $state) {
$options[$machine_name] = check_plain(workbench_moderation_state_label($machine_name));
}
// Create the form
$form['instance']['workbench_moderation_status'] = array(
'#type' => 'checkboxes',
'#title' => t('Status'),
'#default_value' => $default_value,
'#options' => $options,
'#states' => array(
'visible' => array(
'select[name="instance[required]"]' => array('value' => 1),
':input[name="instance[workbench_moderation]"]' => array('checked' => TRUE)
)
),
'#weight' => -8,
);
}
//Else show a message to recommend the user to enabled the moderation and the revisions
else {
$form['instance']['workbench_moderation']['#description'] = t('Revisions and moderations must be enabled in order to use this functionality.');
$form['instance']['workbench_moderation']['#disabled'] = TRUE;
}
}
// Add a new form validate function.
$form['#validate'][] = 'srf_field_ui_field_edit_form_validate';
}
}
/**
* Form validate function to re-create normal structure
* for field instance configuration.
*/
function srf_field_ui_field_edit_form_validate(&$form, &$form_state) {
$required_state = $form_state['values']['instance']['required'];
switch ($required_state) {
// Optional field.
case '0':
$form_state['values']['instance']['required'] = 0;
$form_state['values']['instance']['semi_required'] = 0;
$form_state['values']['instance']['workbench_moderation'] = 0;
$form_state['values']['instance']['workbench_moderation_status'] = array();
break;
// Semi Required field (only for publication or transitions if workbench_moderation is enabled).
case '1':
$form_state['values']['instance']['required'] = 0;
$form_state['values']['instance']['semi_required'] = 1;
if (!workbench_moderation_node_type_moderated($form['#instance']['bundle'])) {
$form_state['values']['instance']['workbench_moderation'] = 0;
$form_state['values']['instance']['workbench_moderation_status'] = array();
}
break;
// Fully required field.
case '2':
$form_state['values']['instance']['required'] = 1;
$form_state['values']['instance']['semi_required'] = 0;
$form_state['values']['instance']['workbench_moderation'] = 0;
$form_state['values']['instance']['workbench_moderation_status'] = array();
break;
}
}
/**
* Implements hook_field_widget_form_alter().
*/
function srf_field_widget_form_alter(&$element, &$form_state, $context) {
// Alter field label if instance is required only for publication.
if (isset($context['instance']['semi_required']) && $context['instance']['semi_required'] == 1) {
// Include callbacks file.
module_load_include('inc', 'srf', 'srf.callbacks');
// If $element is directly the field
if (isset($element['#field_name'])) {
_srf_add_validate_function($element);
}
// It may be an array of fields, so we have to iterate
else {
foreach ($element as $delta => &$elem) {
_srf_add_validate_function($element[$delta]);
}
}
// As $element structure depends on the type of field,
// let's pass it through a function to alter correctly field label.
$alter_widget = srf_get_alter_widget();
$field_type = $context['field']['type'];
if (isset($alter_widget[$field_type]['callback_alter']) && function_exists($alter_widget[$field_type]['callback_alter'])) {
$alter_widget[$field_type]['callback_alter']($element);
}
else {
drupal_set_message(t('To alter the widget title and add the semi required validation to <strong>@field_label</strong> field (<em>@field_type</em>), you need to implement hook_srf_alter_widget and configure it according to the !readme_link or the <strong>srf.api.php</strong>.', array(
'@field_label' => $context['instance']['label'],
'@field_type' => $field_type,
'!readme_link' => l('README.md', drupal_get_path('module', 'srf') . '/README.md'),
)), 'warning');
}
}
}
/**
* Helper function to add validate function to a field.
*/
function _srf_add_validate_function(&$element) {
if (isset($element['#element_validate']) && is_array($element['#element_validate'])) {
$element['#element_validate'][] = 'srf_field_widget_form_validate';
}
else {
$element['#element_validate'] = array('srf_field_widget_form_validate');
}
}
/**
* Implements hook_field_widget_form_validate().
*/
function srf_field_widget_form_validate($element, &$form_state, $form) {
$validate = FALSE;
$field_name = $element['#field_name'];
// If we are saving the node the submit handlers should be like
// node_form_submit, i18n_node_form_submit, workbench_moderation_node_form_submit etc.
$node_submit = FALSE;
if (isset($form_state['submit_handlers'])) {
foreach ($form_state['submit_handlers'] as $submit_handler){
if (strstr($submit_handler, 'node_form_submit') !== FALSE){
$node_submit = TRUE;
break;
}
}
}
// If we are saving the node we will check the need for validation
if ($node_submit) {
// If the integration with the module workbench_moderation is enabled use the workbench status
if (isset($form_state['field'][$field_name][LANGUAGE_NONE]['instance']['workbench_moderation']) &&
$form_state['field'][$field_name][LANGUAGE_NONE]['instance']['workbench_moderation']) {
// If all the values are set and the workbench_moderation_status == workbench_moderation_state_new
// We will validate the field.
if (isset($form_state['field'][$field_name][LANGUAGE_NONE]['instance']['workbench_moderation_status']) &&
isset($form_state['values']['workbench_moderation_state_new']) &&
in_array($form_state['values']['workbench_moderation_state_new'], array_filter($form_state['field'][$field_name][LANGUAGE_NONE]['instance']['workbench_moderation_status']))
) {
$validate = TRUE;
$new_status = $form_state['values']['workbench_moderation_state_new'];
}
}
else {
if (isset($form_state['values']['status']) && $form_state['values']['status']) {
// If workbench_moderation is not enabled, validate the field if the node change the status to published
$validate = TRUE;
$new_status = 'published';
}
}
// Validate the field if needed
if ($validate) {
$alter_widget = srf_get_alter_widget();
$field_type = $form_state['field'][$field_name][LANGUAGE_NONE]['field']['type'];
// As $element structure depends on the type of field,
// let call the right validate function for the field
if (isset($alter_widget[$field_type]['callback_alter']) && function_exists($alter_widget[$field_type]['callback_validate'])) {
$alter_widget[$field_type]['callback_validate']($element, $form_state, $form, $new_status);
}
else {
drupal_set_message(t('To add the semi required validation to <strong>@field_label</strong> field (<em>@field_type</em>), you need to implement hook_srf_alter_widget and configure it according to the !readme_link or the <strong>srf.api.php</strong>.', array(
'@field_label' => $form_state['field'][$field_name][LANGUAGE_NONE]['instance']['label'],
'@field_type' => $field_type,
'!readme_link' => l('README.txt', drupal_get_path('module', 'srf') . '/README.txt', array('attributes' => array('target' => '_blank'))),
)), 'warning');
}
}
}
}
/**
* Get alter label list by calling srf_alter_widget hook,
* and store result in a static variable.
* @return array List of widget alteration
*/
function srf_get_alter_widget() {
$alter_widget = &drupal_static(__FUNCTION__);
if (!isset($alter_widget)) {
$alter_widget = module_invoke_all('srf_alter_widget');
drupal_alter('srf_alter_widget', $alter_widget);
}
return $alter_widget;
}
/**
* Implements hook_srf_alter_widget().
*
* @see srf.callbacks.inc for callback_alter functions
* @see srf.validate.inc for callback_validate functions
*/
function srf_srf_alter_widget() {
// Same callback functions are use in differents types of fields
// because some fields has similar data structures.
$alter_widget = array();
// Use same callback_validate functions for all text field types
// but different callback_alter for text_with_summary field type.
$alter_widget['text'] = array(
'callback_alter' => 'srf_field_alter_widget',
'callback_validate' => 'srf_field_validate',
);
$alter_widget['text_long'] = $alter_widget['text'];
$alter_widget['text_with_summary'] = $alter_widget['text'];
$alter_widget['text_with_summary']['callback_alter'] = 'srf_text_with_summary_alter_widget';
// Use same callback_alter function for all date field types
// same callbacks as text field.
$alter_widget['date'] = $alter_widget['text'];
$alter_widget['datetime'] = $alter_widget['date'];
$alter_widget['datestamp'] = $alter_widget['date'];
// Use same callback_alter functions that is used for the text field types
// but differents callback_validate.
$alter_widget['taxonomy_term_reference'] = $alter_widget['text'];
$alter_widget['taxonomy_term_reference']['callback_validate'] = 'srf_taxonomy_term_reference_validate';
$alter_widget['link_field'] = $alter_widget['text'];
$alter_widget['link_field']['callback_validate'] = 'srf_link_field_validate';
// Use same callback functions for file and image field types
$alter_widget['file'] = array(
'callback_alter' => 'srf_file_alter_widget',
'callback_validate' => 'srf_file_validate',
);
$alter_widget['image'] = $alter_widget['file'];
// Entityreference field type callbacks
$alter_widget['entityreference'] = array(
'callback_alter' => 'srf_entityreference_alter_widget',
'callback_validate' => 'srf_entityreference_validate',
);
// Email field type callbacks
$alter_widget['email'] = array(
'callback_alter' => 'srf_email_alter_widget',
'callback_validate' => 'srf_email_validate',
);
return $alter_widget;
}
/**
* Implements hook_theme().
*/
function srf_theme($existing, $type, $theme, $path) {
$themes = array();
$themes['srf_field_label'] = array(
'variables' => array(
'vars' => array(),
),
);
return $themes;
}
/**
* Theme function for srf_field_label.
*/
function theme_srf_field_label($vars) {
// Add a litle CSS to design semi-required span.
drupal_add_css(drupal_get_path('module', 'srf') . '/css/styles.css');
// Add the semi-required span to field label.
$altered_label = $vars['title'] . ' '
. '<span class="form-semi-required" title="'
. t('This field is semi-required.')
. '">*</span>';
return $altered_label;
}