-
Notifications
You must be signed in to change notification settings - Fork 0
/
autofill.module
130 lines (112 loc) · 4.6 KB
/
autofill.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
<?php
use Drupal\Core\Routing\RouteMatchInterface;
use \Drupal\Core\Field\WidgetInterface;
use \Drupal\Core\Field\FieldDefinitionInterface;
use \Drupal\Core\Form\FormStateInterface;
use \Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* @file
* Contains autofill.module.
*/
/**
* Implements hook_help().
*/
function autofill_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the autofill module.
case 'help.page.autofill':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('A field can automatically be autofilled while typing into another one.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_field_widget_third_party_settings_form().
*/
function autofill_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, array $form, FormStateInterface $form_state) {
$element = [];
if ($field_definition->getType() === 'string') {
$field_name = $field_definition->getName();
$available_source_fields = _autofill_get_available_source_fields_as_options($form, $field_name);
$element['enabled'] = [
'#type' => 'checkbox',
'#title' => t('Enable Autofill from another field'),
'#disabled' => !empty($available_source_fields) ? FALSE : TRUE,
'#default_value' => !empty($available_source_fields) ? $plugin->getThirdPartySetting('autofill', 'enabled') : NULL,
];
// Check if there are available source fields.
if (!empty($available_source_fields)) {
$element['source_field'] = [
'#type' => 'select',
'#title' => t('Autofill source field'),
'#default_value' => $plugin->getThirdPartySetting('autofill', 'source_field'),
'#options' => $available_source_fields,
'#states' => [
'visible' => [
':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][autofill][enabled]"]' => ['checked' => TRUE],
],
],
];
}
else {
$element['source_field'] = ['#markup' => t('No source field available. Please create a text field from where this field should be autofilled.')];
}
}
return $element;
}
/**
* Build an option list of available autofill source fields.
*
* @param array $form
* The currently processed form.
* @param string $current_field_name
* The currently process field name.
*
* @return array
* The field name and field label as a key/value pair.
*/
function _autofill_get_available_source_fields_as_options(array $form, $current_field_name) {
$options = [];
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type);
// Filter the available fields by the support field type and make sure it is
// not the currently processed field.
$available_entity_fields = array_filter($field_storage_definitions, function(FieldStorageDefinitionInterface $field_storage) use ($current_field_name) {
return $field_storage->getName() !== $current_field_name && $field_storage->getType() === 'string';
});
$form_fields = array_intersect_key($available_entity_fields, array_flip($form['#fields']));
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field */
foreach ($form_fields as $field) {
$label = $field->getLabel();
// Load labels from field_config for regular fields.
$field_configs = \Drupal::entityTypeManager()
->getStorage('field_config')
->loadByProperties([
'field_name' => $field->getName(),
'entity_type' => $entity_type,
'bundle' => $bundle,
]);
/** @var \Drupal\field\FieldConfigInterface $field_config */
if ($field_config = array_pop($field_configs)) {
$label = $field_config->getLabel();
}
$options[$field->getName()] = $label;
}
return $options;
}
/**
* Implements hook_field_widget_form_alter().
*/
function autofill_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
/** @var \Drupal\Core\Field\WidgetInterface $widget */
$widget = $context['widget'];
/** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
$field_definition = $context['items']->getFieldDefinition();
if ($widget->getThirdPartySetting('autofill', 'enabled') && $source_field = $widget->getThirdPartySetting('autofill', 'source_field')) {
$element['#attached']['library'][] = 'autofill/autofill';
$element['#attached']['drupalSettings']['autofill']['field_mapping'][$field_definition->getName()] = $source_field;
}
}