-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules_forms.rules.inc
More file actions
536 lines (517 loc) · 17.4 KB
/
rules_forms.rules.inc
File metadata and controls
536 lines (517 loc) · 17.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<?php
/**
* @file
* Rules events, conditions, and actions hooks for Rules Forms module.
*/
/**
* Implements hook_rules_file_info().
*/
function rules_forms_rules_file_info() {
return array('includes/rules_forms.eval');
}
/**
* Implements hook_rules_event_info().
*/
function rules_forms_rules_event_info() {
$form_info = rules_forms_get_form_info();
$defaults = array(
'group' => 'Rules Forms',
'access callback' => 'rules_forms_integration_access',
);
$events = array();
foreach ($form_info as $form_id => $info) {
$events['rules_forms_' . $form_id . '_form_built'] = $defaults + array(
'label' => t('@form is being built', array('@form' => $info['label'])),
'variables' => rules_forms_event_variables() + rules_forms_element_variables($form_id),
);
$events['rules_forms_' . $form_id . '_form_submit'] = $defaults + array(
'label' => t('@form is submitted', array('@form' => $info['label'])),
'variables' => rules_forms_event_variables() + rules_forms_element_variables($form_id),
);
$events['rules_forms_' . $form_id . '_form_validate'] = $defaults + array(
'label' => t('@form is being validated', array('@form' => $info['label'])),
'variables' => rules_forms_event_variables() + rules_forms_element_variables($form_id),
);
if (isset($info['buttons'], $info['submit'])) {
foreach ($info['submit'] as $element_id => $label) {
// Add button validate events.
$events['rules_forms_' . $form_id . '_button_' . str_replace(':', '_', $element_id) . '_validate'] = $defaults + array(
'label' => t('@form @button button is being validated', array(
'@form' => $info['label'],
'@button' => strtolower($label)
)),
'variables' => rules_forms_event_variables() + rules_forms_element_variables($form_id),
);
// Add button submit events.
$events['rules_forms_' . $form_id . '_button_' . str_replace(':', '_', $element_id) . '_submit'] = $defaults + array(
'label' => t('@form @button button is being submitted', array(
'@form' => $info['label'],
'@button' => strtolower($label)
)),
'variables' => rules_forms_event_variables() + rules_forms_element_variables($form_id),
);
}
}
}
return $events;
}
/**
* Returns some arguments suitable for hook form alter.
*
* @see rules_forms_rules_event_info()
*/
function rules_forms_event_variables() {
return array(
'form' => array(
'type' => 'form',
'label' => t('Form'),
),
'form_state' => array(
'type' => 'form_state',
'label' => t('Form state'),
),
'form_id' => array(
'type' => 'text',
'label' => t('Form ID'),
),
'entity' => array(
'type' => 'entity',
'label' => t('Entity if entity form.'),
),
'user' => array(
'type' => 'user',
'label' => t('Logged in user'),
),
);
}
/**
* Adds element variables for the form. Each form element ID is passed as
* a variable to the rule for access in conditions and actions. This allows
* us to provide a select list of elements rather than having to cut-and-paste
* element IDs.
*/
function rules_forms_element_variables($form_id) {
$variables = array();
$form_info = variable_get('rules_forms_form_info', array());
if (isset($form_info[$form_id]['elements']) && is_array($form_info[$form_id]['elements'])) {
foreach ($form_info[$form_id]['elements'] as $key => $info) {
$variables[$key] = array(
'type' => 'form_element',
'label' => $info['label'],
);
}
}
return $variables;
}
/**
* Implements hook_rules_condition_info().
*/
function rules_forms_rules_condition_info() {
$conditions = array();
$conditions['rules_forms_element_value'] = array(
'label' => t('Form element has value'),
'group' => 'Rules Forms',
'parameter' => array(
'form' => array(
'type' => 'form',
'label' => t('Form'),
),
'form_state' => array(
'type' => 'form_state',
'label' => t('Form state'),
),
'element' => array(
'type' => 'form_element',
'label' => t('Form element'),
'description' => t('The form element to be targeted.'),
),
'value' => array(
'type' => 'form_value',
'label' => t('Value(s)'),
'optional' => TRUE,
'description' => t('Value(s) assigned to the form element. If the form element allows multiple values, enter one value per line.'),
),
'regex' => array(
'type' => 'boolean',
'label' => t('Evaluate as regular expression'),
'optional' => TRUE,
'description' => t('Perform the data comparison using the provided value as a regular expression. ' .
'To execute multiple regular expressions, enter one expression per line.'),
),
),
'base' => 'rules_forms_condition_element_value',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$conditions['rules_forms_element_changed'] = array(
'label' => t('Form element value has changed'),
'group' => 'Rules Forms',
'parameter' => array(
'form' => array(
'type' => 'form',
'label' => t('Form'),
),
'form_state' => array(
'type' => 'form_state',
'label' => t('Form state'),
),
'element' => array(
'type' => 'form_element',
'label' => t('Form element'),
'description' => t('The form element to be targeted.'),
),
),
'base' => 'rules_forms_condition_element_changed',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$conditions['rules_forms_button_clicked'] = array(
'label' => t('Form button was clicked'),
'group' => 'Rules Forms',
'parameter' => array(
'form' => array(
'type' => 'form',
'label' => t('Form'),
),
'form_state' => array(
'type' => 'form_state',
'label' => t('Form state'),
),
'element' => array(
'type' => 'form_element',
'label' => t('Form element'),
'description' => t('The form button that was clicked.'),
),
),
'base' => 'rules_forms_condition_button_clicked',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
return $conditions;
}
/**
* Implements hook_rules_action_info().
*/
function rules_forms_rules_action_info() {
$element_params = array(
'form' => array('type' => 'form', 'label' => t('Form')),
'element' => array(
'type' => 'form_element',
'label' => t('Form element'),
'restriction' => 'input',
'description' => t('The form element to be targeted.'),
),
);
$actions = array();
$actions['rules_forms_set_title'] = array(
'label' => t('Set the title of an element in the form'),
'group' => 'Rules Forms',
'parameter' => $element_params + array(
'title' => array(
'type' => 'text',
'label' => t('Title'),
'optional' => TRUE,
),
),
'base' => 'rules_forms_action_set_title',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$actions['rules_forms_set_description'] = array(
'label' => t('Set the description of an element in the form'),
'group' => 'Rules Forms',
'parameter' => $element_params + array(
'description' => array(
'type' => 'text',
'label' => t('Description'),
'optional' => TRUE,
),
),
'base' => 'rules_forms_action_set_description',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$actions['rules_forms_set_access'] = array(
'label' => t('Hide an element in the form'),
'group' => 'Rules Forms',
'parameter' => $element_params + array(
'access' => array(
'type' => 'boolean',
'label' => t('Hide'),
'optional' => TRUE,
'description' => t('Hides a form element.'),
),
),
'base' => 'rules_forms_action_set_access',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$actions['rules_forms_set_disabled'] = array(
'label' => t('Disable an element in the form'),
'group' => 'Rules Forms',
'parameter' => $element_params + array(
'disabled' => array(
'type' => 'boolean',
'label' => t('Disable'),
'optional' => TRUE,
'description' => t('Disables a form element.'),
),
),
'base' => 'rules_forms_action_set_disabled',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$actions['rules_forms_set_required'] = array(
'label' => t('Require an element in the form'),
'group' => 'Rules Forms',
'parameter' => $element_params + array(
'require' => array(
'type' => 'boolean',
'label' => t('Required'),
'optional' => TRUE,
'description' => t('Requires a form element.'),
),
),
'base' => 'rules_forms_action_set_required',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$actions['rules_forms_set_options'] = array(
'label' => t('Set multiple value options of an element in the form'),
'group' => 'Rules Forms',
'parameter' => $element_params + array(
'options' => array(
'type' => 'text',
'label' => t('Options'),
'description' => t('<strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>. Use of only alphanumeric characters and underscores is recommended in keys. One option per line.'),
),
),
'base' => 'rules_forms_action_set_options',
'validate' => 'rules_forms_action_set_options_validate',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$actions['rules_forms_set_default'] = array(
'label' => t('Set the default value of an element in the form'),
'group' => 'Rules Forms',
'parameter' => $element_params + array(
'value' => array(
'type' => 'form_value',
'label' => t('Value(s)'),
'optional' => TRUE,
'description' => t('Value(s) to assign to the form element. If the form element allows multiple values, enter one value per line.'),
),
),
'base' => 'rules_forms_action_set_default',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$actions['rules_forms_set_weight'] = array(
'label' => t('Adjust weight of an element in the form'),
'group' => 'Rules Forms',
'parameter' => $element_params + array(
'weight' => array(
'type' => 'integer',
'label' => t('Element weight'),
'options list' => '_rules_forms_weight_options',
'description' => t('Low numbers make the element bubble up, high numbers sink it down.'),
),
),
'base' => 'rules_forms_action_set_weight',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$actions['rules_forms_set_prefix_suffix'] = array(
'label' => t('Insert HTML into the prefix/suffix of an element in the form'),
'group' => 'Rules Forms',
'parameter' => $element_params + array(
'prefix' => array(
'type' => 'text',
'label' => t('Prefixed HTML'),
'optional' => TRUE,
'description' => t('HTML inserted before the element.'),
),
'suffix' => array(
'type' => 'text',
'label' => t('Suffixed HTML'),
'optional' => TRUE,
'description' => t('HTML inserted after the element.'),
),
),
'base' => 'rules_forms_action_set_prefix_suffix_html',
'callbacks' => array(
'access' => 'rules_forms_integration_access',
'form_alter' => 'rules_forms_element_form_alter',
),
);
$actions['rules_forms_set_error'] = array(
'label' => t('Set an error on the form'),
'group' => 'Rules Forms',
'parameter' => array(
'form' => array('type' => 'form', 'label' => t('Form')),
'element' => array(
'type' => 'form_element',
'label' => t('Form element'),
'restriction' => 'input',
'optional' => TRUE,
'description' => t('The form element to be targeted.'),
),
'message' => array(
'type' => 'text',
'label' => t('Message'),
'optional' => TRUE,
'description' => t('The message that should be displayed to the user.'),
),
),
'base' => 'rules_forms_action_set_error',
'access callback' => 'rules_forms_integration_access',
);
$actions['rules_forms_redirect'] = array(
'label' => t('Set the redirect target of the form'),
'group' => 'Rules Forms',
'parameter' => array(
'form_state' => array('type' => 'form_state', 'label' => t('Form state')),
'path' => array('type' => 'text', 'label' => t('Path')),
'query' => array(
'type' => 'text',
'label' => t('Query'),
'optional' => TRUE
),
'fragment' => array(
'type' => 'text',
'label' => t('Fragment'),
'optional' => TRUE
),
),
'base' => 'rules_forms_action_redirect',
'access callback' => 'rules_forms_integration_access',
'help' => t('Enter a Drupal path, path alias, or external URL to redirect to. Enter (optional) queries after "?" and (optional) anchor after "#".'),
);
$actions['rules_forms_submit'] = array(
'label' => t('Submit the form'),
'group' => 'Rules Forms',
'parameter' => array(
'form' => array('type' => 'form', 'label' => t('Form')),
'form_state' => array('type' => 'form_state', 'label' => t('Form state')),
),
'base' => 'rules_forms_action_submit',
'access callback' => 'rules_forms_integration_access',
);
return $actions;
}
/**
* Provides numeric options for setting the weight of a form element.
*/
function _rules_forms_weight_options() {
$options = array();
for ($i = -20; $i < 21; $i++) {
$options[(string) $i] = $i;
}
return $options;
}
/**
* Provides a standard explanaition of how to use element IDs with
* conditions and actions.
*/
function _rules_forms_element_description() {
return t('Examples on the "Create Article" form: "title" for the title field or "body[und][0][value]" for the body field.');
}
/**
* Implements hook_rules_data_type_info().
*/
function rules_forms_rules_data_info() {
return array(
'form' => array(
'label' => t('form'),
'group' => 'Rules Forms',
),
'form_state' => array(
'label' => t('form state'),
'group' => 'Rules Forms',
),
'form_element' => array(
'label' => t('form element'),
'group' => 'Rules Forms',
'ui class' => 'RulesFormsDataElement',
),
'form_value' => array(
'label' => t('form value'),
'group' => 'Rules Forms',
'ui class' => 'RulesFormsDataValue',
),
);
}
/**
* Alters the form for action: Set element property. Taken from Rules module.
*/
function rules_forms_element_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) {
$first_step = empty($element->settings['element']);
$form['reload'] = array(
'#weight' => 10,
'#type' => 'submit',
'#name' => 'reload',
'#value' => $first_step ? t('Continue') : t('Reload form'),
'#limit_validation_errors' => array(array('parameter', 'element')),
'#submit' => array('rules_action_type_form_submit_rebuild'),
'#ajax' => rules_ui_form_default_ajax(),
);
// Use ajax and trigger as the reload button.
$form['parameter']['element']['settings']['element']['#ajax'] = $form['reload']['#ajax'] + array(
'event' => 'change',
'trigger_as' => array('name' => 'reload'),
);
// Hide all form elements other than the element selector.
if ($first_step) {
// In the first step show only the type select.
foreach (element_children($form['parameter']) as $key) {
if ($key != 'element') {
unset($form['parameter'][$key]);
}
}
unset($form['submit']);
unset($form['provides']);
unset($form['negate']);
}
else {
// For the Condition: Element has value form, unset the regex option
// if it doesn't make sense for the type of field selected.
if ($element->getElementName() == 'rules_forms_element_value') {
$type = substr($element->settings['element'], 0, strpos($element->settings['element'], ':'));
if ($type != 'textfield' && $type != 'textarea' && $type != 'hidden') {
$form['parameter']['regex']['#access'] = FALSE;
$form['parameter']['regex']['settings']['regex']['#type'] = 'value';
$form['parameter']['regex']['settings']['regex']['#value'] = 0;
}
}
// Hide the reload button in case js is enabled and it's not the first step.
$form['reload']['#attributes'] = array('class' => array('rules-hide-js'));
}
}
/**
* Rules Forms integration access callback.
*/
function rules_forms_integration_access($type, $name) {
return user_access('administer rules forms rules');
}