function ListItemBase::storageSettingsForm
Same name in other branches
- 9 core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php \Drupal\options\Plugin\Field\FieldType\ListItemBase::storageSettingsForm()
- 8.9.x core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php \Drupal\options\Plugin\Field\FieldType\ListItemBase::storageSettingsForm()
- 10 core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php \Drupal\options\Plugin\Field\FieldType\ListItemBase::storageSettingsForm()
Overrides FieldItemBase::storageSettingsForm
3 calls to ListItemBase::storageSettingsForm()
- ListFloatItem::storageSettingsForm in core/
modules/ options/ src/ Plugin/ Field/ FieldType/ ListFloatItem.php - Returns a form for the storage-level settings.
- ListIntegerItem::storageSettingsForm in core/
modules/ options/ src/ Plugin/ Field/ FieldType/ ListIntegerItem.php - Returns a form for the storage-level settings.
- ListStringItem::storageSettingsForm in core/
modules/ options/ src/ Plugin/ Field/ FieldType/ ListStringItem.php - Returns a form for the storage-level settings.
3 methods override ListItemBase::storageSettingsForm()
- ListFloatItem::storageSettingsForm in core/
modules/ options/ src/ Plugin/ Field/ FieldType/ ListFloatItem.php - Returns a form for the storage-level settings.
- ListIntegerItem::storageSettingsForm in core/
modules/ options/ src/ Plugin/ Field/ FieldType/ ListIntegerItem.php - Returns a form for the storage-level settings.
- ListStringItem::storageSettingsForm in core/
modules/ options/ src/ Plugin/ Field/ FieldType/ ListStringItem.php - Returns a form for the storage-level settings.
File
-
core/
modules/ options/ src/ Plugin/ Field/ FieldType/ ListItemBase.php, line 92
Class
- ListItemBase
- Plugin base class inherited by the options field types.
Namespace
Drupal\options\Plugin\Field\FieldTypeCode
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
if (!array_key_exists('allowed_values', $form_state->getStorage())) {
$form_state->set('allowed_values', $this->getFieldDefinition()
->getSetting('allowed_values'));
}
$form['field_storage_submit']['#submit'][] = [
static::class,
'submitFieldStorageUpdate',
];
$form['field_storage_submit']['#limit_validation_errors'] = [];
$allowed_values = $form_state->getStorage()['allowed_values'];
$allowed_values_function = $this->getSetting('allowed_values_function');
if (!$form_state->get('items_count')) {
$form_state->set('items_count', max(count($allowed_values), 0));
}
$wrapper_id = Html::getUniqueId('allowed-values-wrapper');
$element['allowed_values'] = [
'#element_validate' => [
[
static::class,
'validateAllowedValues',
],
],
'#field_has_data' => $has_data,
'#allowed_values' => $allowed_values,
'#required' => TRUE,
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '</div>',
'#access' => empty($allowed_values_function),
'help_text' => [
'#markup' => $this->allowedValuesDescription(),
],
];
$element['allowed_values']['table'] = [
'#type' => 'table',
'#header' => [
$this->t('Allowed values'),
$this->t('Delete'),
$this->t('Weight'),
],
'#attributes' => [
'id' => 'allowed-values-order',
'data-field-list-table' => TRUE,
'class' => [
'allowed-values-table',
],
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'weight',
],
],
'#attached' => [
'library' => [
'core/drupal.fieldListKeyboardNavigation',
'field_ui/drupal.field_ui',
],
],
];
$max = $form_state->get('items_count');
$entity_type_id = $this->getFieldDefinition()
->getTargetEntityTypeId();
$field_name = $this->getFieldDefinition()
->getName();
$current_keys = array_keys($allowed_values);
for ($delta = 0; $delta <= $max; $delta++) {
$element['allowed_values']['table'][$delta] = [
'#attributes' => [
'class' => [
'draggable',
],
],
'#weight' => $delta,
];
$element['allowed_values']['table'][$delta]['item'] = [
'label' => [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#weight' => -30,
'#default_value' => isset($current_keys[$delta]) ? $allowed_values[$current_keys[$delta]] : '',
'#required' => $delta === 0,
],
'key' => [
'#type' => 'textfield',
'#maxlength' => 255,
'#title' => $this->t('Value'),
'#default_value' => $current_keys[$delta] ?? '',
'#weight' => -20,
'#required' => $delta === 0,
],
];
$element['allowed_values']['table'][$delta]['delete'] = [
'#type' => 'submit',
'#value' => $this->t('Remove'),
'#name' => "remove_row_button__{$delta}",
'#id' => "remove_row_button__{$delta}",
'#delta' => $delta,
'#submit' => [
[
static::class,
'deleteSubmit',
],
],
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => [
static::class,
'deleteAjax',
],
'wrapper' => $wrapper_id,
'effect' => 'fade',
],
];
$element['allowed_values']['table'][$delta]['weight'] = [
'#type' => 'weight',
'#title' => $this->t('Weight for row @number', [
'@number' => $delta + 1,
]),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => 0,
'#attributes' => [
'class' => [
'weight',
],
],
];
// Disable the remove button if there is only one row in the table.
if ($max === 0) {
$element['allowed_values']['table'][0]['delete']['#attributes']['disabled'] = 'disabled';
}
if ($delta < count($allowed_values)) {
$query = \Drupal::entityQuery($entity_type_id)->accessCheck(FALSE)
->condition($field_name, $current_keys[$delta]);
$entity_ids = $query->execute();
if (!empty($entity_ids)) {
$element['allowed_values']['table'][$delta]['item']['key']['#attributes']['disabled'] = 'disabled';
$element['allowed_values']['table'][$delta]['delete']['#attributes']['disabled'] = 'disabled';
$element['allowed_values']['table'][$delta]['delete'] += [
'message' => [
'#type' => 'item',
'#markup' => $this->t('Cannot be removed: option in use.'),
],
];
}
}
}
$element['allowed_values']['table']['#max_delta'] = $max;
$element['allowed_values']['add_more_allowed_values'] = [
'#type' => 'submit',
'#name' => 'add_more_allowed_values',
'#value' => $this->t('Add another item'),
'#attributes' => [
'class' => [
'field-add-more-submit',
],
'data-field-list-button' => TRUE,
],
// Allow users to add another row without requiring existing rows to have
// values.
'#limit_validation_errors' => [],
'#submit' => [
[
static::class,
'addMoreSubmit',
],
],
'#ajax' => [
'callback' => [
static::class,
'addMoreAjax',
],
'wrapper' => $wrapper_id,
'effect' => 'fade',
'progress' => [
'type' => 'throbber',
'message' => $this->t('Adding a new item...'),
],
],
];
$element['allowed_values_function'] = [
'#type' => 'item',
'#title' => $this->t('Allowed values list'),
'#markup' => $this->t('The value of this field is being determined by the %function function and may not be changed.', [
'%function' => $allowed_values_function,
]),
'#access' => !empty($allowed_values_function),
'#value' => $allowed_values_function,
];
return $element;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.