function ListStringItem::storageSettingsForm

Same name and namespace in other branches
  1. 10 core/modules/options/src/Plugin/Field/FieldType/ListStringItem.php \Drupal\options\Plugin\Field\FieldType\ListStringItem::storageSettingsForm()

Overrides ListItemBase::storageSettingsForm

File

core/modules/options/src/Plugin/Field/FieldType/ListStringItem.php, line 90

Class

ListStringItem
Plugin implementation of the 'list_string' field type.

Namespace

Drupal\options\Plugin\Field\FieldType

Code

public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
  $element = parent::storageSettingsForm($form, $form_state, $has_data);
  // Improve user experience by using an automatically generated machine name.
  foreach (Element::children($element['allowed_values']['table']) as $delta => $row) {
    $element['allowed_values']['table'][$delta]['item']['key']['#type'] = 'machine_name';
    // ListItemBase::storageSettingsForm() will set the default value to an
    // integer if the key is a decimal integer string, so cast it back here.
    $element['allowed_values']['table'][$delta]['item']['key']['#default_value'] = (string) $element['allowed_values']['table'][$delta]['item']['key']['#default_value'];
    $element['allowed_values']['table'][$delta]['item']['key']['#machine_name'] = [
      'exists' => [
        static::class,
        'exists',
      ],
    ];
    $element['allowed_values']['table'][$delta]['item']['key']['#process'] = array_merge([
      [
        static::class,
        'processAllowedValuesKey',
      ],
    ], \Drupal::service('plugin.manager.element_info')->getInfoProperty('machine_name', '#process', []));
    // Remove #element_validate from the machine name so that any value can be
    // used as a key, while keeping the widget's behavior for generating
    // defaults the same.
    $element['allowed_values']['table'][$delta]['item']['key']['#element_validate'] = [];
  }
  return $element;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.