88use Drupal\Core\Form\FormStateInterface;
99use Drupal\Core\Entity\FieldableEntityInterface;
1010use Drupal\Core\Field\FieldStorageDefinitionInterface;
11+ use Drupal\field\FieldStorageConfigInterface;
1112
1213/**
1314 * Implements hook_form_FORM_ID_alter(): field_storage_config_edit_form.
1415 */
1516function list_predefined_options_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
16- /** @var \Drupal\Core\Field\FieldConfigInterface $field */
17- $field = $form_state->getFormObject()->getEntity();
17+ /** @var \Drupal\field_ui\Form\FieldStorageConfigEditForm $form */
18+ $formObject = $form_state->getFormObject();
19+ /** @var \Drupal\field\FieldStorageConfigInterface $field */
20+ $field = $formObject->getEntity();
1821
1922 $field_types = [
2023 'list_float',
@@ -26,31 +29,73 @@ function list_predefined_options_form_field_storage_config_edit_form_alter(&$for
2629 $options = \Drupal::service('plugin.manager.list_options.processor')->listOptions();
2730 $options = ['' => t('Custom')] + $options;
2831
32+ $allowed_values = $field->getSetting('allowed_values');
33+ $allowed_values_function = $field->getSetting('allowed_values_function');
2934 $settings = $field->getThirdPartySettings('list_predefined_options');
35+ $plugin_id = isset($settings['plugin_id']) ? $settings['plugin_id'] : NULL;
36+
3037 $form['list_predefined_options_plugin_id'] = [
3138 '#type' => 'select',
3239 '#title' => t('Allowed values'),
3340 '#options' => $options,
34- '#default_value' => isset($settings['plugin_id']) ? $settings['plugin_id'] : NULL,
41+ '#default_value' => $plugin_id,
42+ '#ajax' => [
43+ 'callback' => 'list_predefined_options_form_field_storage_config_form_ajax_callback',
44+ 'wrapper' => 'edit-settings',
45+ 'event' => 'change',
46+ ],
3547 '#weight' => -20,
3648 ];
37- // TODO: currently produces an error.
38- //$form['allowed_values']['#states']['visible'][':input[name="field[settings][allowed_values_function]"]'] = array('value' => '');
39- $form['allowed_values']['#access'] = TRUE;
40- $form['allowed_values_function_display']['#access'] = FALSE;
49+
50+ $form['settings']['#type'] = 'container';
51+ $form['settings']['allowed_values']['#access'] = TRUE;
52+ $form['settings']['allowed_values_function']['#access'] = FALSE;
53+ if (isset($plugin_id)) {
54+ /** @var \Drupal\list_predefined_options\Plugin\ListOptionsInterface $plugin */
55+ $plugin = \Drupal::service('plugin.manager.list_options.processor')->createInstance($plugin_id);
56+
57+ $form['settings']['allowed_values'] = [
58+ '#type' => 'select',
59+ '#title' => t('Allowed values List'),
60+ '#description' => t('If left empty, the value of this field will be determined by the %function function.', ['%function' => $allowed_values_function]),
61+ '#options' => $plugin->getListOptions($field),
62+ '#multiple' => TRUE,
63+ '#default_value' => $allowed_values,
64+ ];
65+ $form['settings']['allowed_values_function']['#description'] = FALSE;
66+ }
4167
4268 $form['#entity_builders'][] = 'list_predefined_options_form_field_storage_config_edit_form_builder';
4369 }
4470}
4571
72+ /**
73+ * AJAX callback for populating predefined option lists.
74+ *
75+ * @param array $form
76+ * An associative array containing the structure of the form.
77+ * @param \Drupal\Core\Form\FormStateInterface $form_state
78+ * The current state of the form.
79+ *
80+ * @return array
81+ * The associative array containing the new structure of the form.
82+ */
83+ function list_predefined_options_form_field_storage_config_form_ajax_callback(array &$form, FormStateInterface $form_state) {
84+ return $form['settings'];
85+ }
86+
4687/**
4788 * Entity builder callback to save our settings into the field storage config.
4889 */
4990function list_predefined_options_form_field_storage_config_edit_form_builder($entity_type, $entity, &$form, FormStateInterface $form_state) {
5091 $plugin_id = $form_state->getValue('list_predefined_options_plugin_id');
5192 if (!empty($plugin_id)) {
5293 $entity->setThirdPartySetting('list_predefined_options', 'plugin_id', $plugin_id);
53- $entity->setSetting('allowed_values_function', 'list_predefined_options_allowed_values');
94+
95+ $allowed_values = $form_state->getValue(['settings', 'allowed_values']);
96+ if (empty($allowed_values)) {
97+ $entity->setSetting('allowed_values_function', 'list_predefined_options_allowed_values');
98+ }
5499 }
55100 else {
56101 $entity->unsetThirdPartySetting('list_predefined_options', 'plugin_id');
@@ -67,6 +112,7 @@ function list_predefined_options_form_field_storage_config_edit_form_builder($en
67112 */
68113function list_predefined_options_allowed_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
69114 $plugin_id = $definition->getThirdPartySetting('list_predefined_options', 'plugin_id');
115+ /** @var \Drupal\list_predefined_options\Plugin\ListOptionsInterface $plugin */
70116 $plugin = \Drupal::service('plugin.manager.list_options.processor')->createInstance($plugin_id);
71117 return $plugin->getListOptions($definition, $entity, $cacheable);
72118}
0 commit comments