-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompact_forms.module
More file actions
121 lines (110 loc) · 3.6 KB
/
compact_forms.module
File metadata and controls
121 lines (110 loc) · 3.6 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
<?php
/**
* @file
* Compact Forms Drupal module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_help().
*/
function compact_forms_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.compact_forms':
return '<p>' . t('Makes form fields compact by overlaying the field label on top the field itself using jQuery.') . '</p>';
}
}
/**
* Implements hook_form_alter().
*/
function compact_forms_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#pre_render'][] = 'compact_forms_pre_render';
}
/**
* The #pre_render callback for all forms.
*/
function compact_forms_pre_render($form) {
static $css_ids, $form_ids, $loaded, $field_size, $descriptions;
$config = \Drupal::config('compact_forms.settings');
// Prepare CSS form ids.
if (!isset($css_ids)) {
$css_ids = explode("\n", $config->get('compact_forms_ids'));
$css_ids = array_filter(array_map('trim', $css_ids));
}
// Prepare Form API form ids.
if (!isset($form_ids) && !empty($css_ids)) {
$form_ids = [];
foreach ($css_ids as $id) {
$form_ids[] = strtr($id, ['-' => '_']);
}
}
// Prepare form alteration settings.
if (!isset($field_size)) {
$field_size = $config->get('compact_forms_field_size');
$descriptions = $config->get('compact_forms_descriptions');
}
if (in_array($form['form_id']['#value'], $form_ids) || (isset($form['#id']) && in_array($form['#id'], $css_ids))) {
// If the custom #compact_forms property has been programmatically set to
// FALSE, do not process this form.
if (isset($form['#compact_forms']) && !$form['#compact_forms']) {
// Also remove it from the CSS IDs being added as JS settings.
foreach ($css_ids as $key => $value) {
if ($value == $form['#id']) {
unset($css_ids[$key]);
}
}
return;
}
// Load our page requisites and JavaScript settings.
if (!isset($loaded)) {
// Using #attached to add css, js, and js settings. JS and CSS libraries
// are defined in compact_forms.libraries.yml as introduced by
// https://drupal.org/node/2201089
$form['#attached']['library'][] = 'compact_forms/compact_forms';
$form['#attached']['drupalSettings']['compactForms'] = [
'forms' => $css_ids,
'stars' => (int) $config->get('compact_forms_stars'),
];
$loaded = TRUE;
}
// Only alter the form if a custom field size is configured or form element
// descriptions shall be hidden.
if (!empty($field_size) || !$descriptions) {
_compact_forms_resize_fields($form, $field_size, $descriptions);
}
}
return $form;
}
/**
* Helper function to recursively alter form elements.
*
* @todo Perform this in #after_build instead. - Or use hook_elements() to
* append a #process function to all supported elements.
*/
function _compact_forms_resize_fields(&$form, $field_size, $descriptions) {
if (empty($form) || !is_array($form)) {
return;
}
foreach (Element::children($form) as $key) {
if (!isset($form[$key]['#type'])) {
continue;
}
switch ($form[$key]['#type']) {
case 'fieldset':
_compact_forms_resize_fields($form[$key], $field_size, $descriptions);
break;
case 'textfield':
case 'textarea':
case 'password':
case 'password_confirm':
if (!empty($field_size)) {
$form[$key]['#size'] = $field_size;
}
if (!$descriptions) {
unset($form[$key]['#description']);
}
break;
}
}
}