-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlist_predefined_options.module
More file actions
118 lines (104 loc) · 4.62 KB
/
list_predefined_options.module
File metadata and controls
118 lines (104 loc) · 4.62 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
<?php
/**
* @file
* Provides reusable, predefined options for list fields.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\FieldStorageConfigInterface;
/**
* Implements hook_form_FORM_ID_alter(): field_storage_config_edit_form.
*/
function list_predefined_options_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\field_ui\Form\FieldStorageConfigEditForm $form */
$formObject = $form_state->getFormObject();
/** @var \Drupal\field\FieldStorageConfigInterface $field */
$field = $formObject->getEntity();
$field_types = [
'list_float',
'list_integer',
'list_string',
];
if (in_array($field->getType(), $field_types)) {
$options = \Drupal::service('plugin.manager.list_options.processor')->listOptions();
$options = ['' => t('Custom')] + $options;
$allowed_values = $field->getSetting('allowed_values');
$allowed_values_function = $field->getSetting('allowed_values_function');
$settings = $field->getThirdPartySettings('list_predefined_options');
$plugin_id = isset($settings['plugin_id']) ? $settings['plugin_id'] : NULL;
$form['list_predefined_options_plugin_id'] = [
'#type' => 'select',
'#title' => t('Allowed values'),
'#options' => $options,
'#default_value' => $plugin_id,
'#ajax' => [
'callback' => 'list_predefined_options_form_field_storage_config_form_ajax_callback',
'wrapper' => 'edit-settings',
'event' => 'change',
],
'#weight' => -20,
];
$form['settings']['#type'] = 'container';
$form['settings']['allowed_values']['#access'] = TRUE;
$form['settings']['allowed_values_function']['#access'] = FALSE;
if (isset($plugin_id)) {
/** @var \Drupal\list_predefined_options\Plugin\ListOptionsInterface $plugin */
$plugin = \Drupal::service('plugin.manager.list_options.processor')->createInstance($plugin_id);
$form['settings']['allowed_values'] = [
'#type' => 'select',
'#title' => t('Allowed values List'),
'#description' => t('If left empty, the value of this field will be determined by the %function function.', ['%function' => $allowed_values_function]),
'#options' => $plugin->getListOptions($field),
'#multiple' => TRUE,
'#default_value' => array_keys($allowed_values),
];
$form['settings']['allowed_values_function']['#description'] = FALSE;
}
$form['#entity_builders'][] = 'list_predefined_options_form_field_storage_config_edit_form_builder';
}
}
/**
* AJAX callback for populating predefined option lists.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The associative array containing the new structure of the form.
*/
function list_predefined_options_form_field_storage_config_form_ajax_callback(array &$form, FormStateInterface $form_state) {
return $form['settings'];
}
/**
* Entity builder callback to save our settings into the field storage config.
*/
function list_predefined_options_form_field_storage_config_edit_form_builder($entity_type, $entity, &$form, FormStateInterface $form_state) {
$plugin_id = $form_state->getValue('list_predefined_options_plugin_id');
if (!empty($plugin_id)) {
$entity->setThirdPartySetting('list_predefined_options', 'plugin_id', $plugin_id);
$allowed_values = $form_state->getValue(['settings', 'allowed_values']);
if (empty($allowed_values)) {
$entity->setSetting('allowed_values_function', 'list_predefined_options_allowed_values');
}
}
else {
$entity->unsetThirdPartySetting('list_predefined_options', 'plugin_id');
// If one of our plugins is being removed, remove our allowed values
// function setting.
if ($entity->getSetting('allowed_values_function') == 'list_predefined_options_allowed_values') {
$entity->setSetting('allowed_values_function', '');
}
}
}
/**
* Implements callback_allowed_values_function().
*/
function list_predefined_options_allowed_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
$plugin_id = $definition->getThirdPartySetting('list_predefined_options', 'plugin_id');
/** @var \Drupal\list_predefined_options\Plugin\ListOptionsInterface $plugin */
$plugin = \Drupal::service('plugin.manager.list_options.processor')->createInstance($plugin_id);
return $plugin->getListOptions($definition, $entity, $cacheable);
}