public function FieldStateManager::settingsForm in Field States UI 8.2
Implements hook_field_widget_third_party_settings_form().
File
- src/
FieldStateManager.php, line 316
Class
- FieldStateManager
- Manages field state plugins.
Namespace
Drupal\field_states_uiCode
public function settingsForm(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
$field_name = $field_definition
->getName();
$element = [
'form' => [
'#type' => 'fieldset',
'#title' => t('Manage Field States'),
'#description' => t('Configure field states - ie automatic hiding/showing of fields.'),
'list' => [
'#type' => 'table',
'#header' => [
t('Type'),
t('Comparison'),
t('Operations'),
],
'#empty' => t('There are no field states applied to this field currently. Add one by selecting an option below.'),
],
],
];
$cancel = [
'#type' => 'submit',
'#value' => t('Cancel'),
'#submit' => [
[
self::class,
'settingsSubmit',
],
],
'#field_name' => $field_name,
'#limit_validation_errors' => [],
'#op' => 'cancel',
];
// Display and maintain existing form states with edit options.
$field_states_settings = $plugin
->getThirdPartySettings('field_states_ui');
if (!empty($field_states_settings['field_states'])) {
$element['field_states'] = [];
foreach ($field_states_settings['field_states'] as $key => $state) {
if (!isset($state['id'])) {
continue;
}
$field_state = $this
->createInstance($state['id'], $state);
$element['form']['list'][$key] = [
'type' => [
'#type' => 'markup',
'#markup' => $field_state
->label(),
],
'comparison' => $field_state
->getSummary(),
'operations' => [
'edit' => [
'#type' => 'submit',
'#value' => t('Edit'),
'#op' => 'edit',
'#submit' => [
[
self::class,
'settingsSubmit',
],
],
'#field_name' => $field_name,
'#key' => $key,
'#name' => 'edit-' . $key,
],
'delete' => [
'#type' => 'submit',
'#value' => t('Delete'),
'#op' => 'delete',
'#submit' => [
[
self::class,
'settingsSubmit',
],
],
'#field_name' => $field_name,
'#key' => $key,
'#name' => 'delete-' . $key,
],
],
];
$element['field_states'][$field_state
->getUuid()] = [
'uuid' => [
'#type' => 'hidden',
'#value' => $field_state
->getUuid(),
],
'id' => [
'#type' => 'hidden',
'#value' => $field_state
->getPluginId(),
],
'data' => $field_state
->getConfigurationForForm(),
];
}
}
// Provide form elements to edit/add form states.
if ($form_state
->get('field_states_ui_edit') == $field_name) {
if ($form_state
->get('field_states_ui_target')) {
$target = $form_state
->get('field_states_ui_target');
$states = $plugin
->getThirdPartySettings('field_states_ui')['field_states'];
if (!isset($states[$target])) {
return $element;
}
$type = $states[$target]['id'];
$field_state = $this
->createInstance($type, $states[$target]);
$title = t('Edit field state: @type', [
'@type' => $field_state
->label(),
]);
$submit_label = t('Update State');
$op = 'process_update';
}
else {
$type = $form_state
->getValue([
'fields',
$field_name,
'settings_edit_form',
'third_party_settings',
'field_states_ui',
'form',
'type',
]);
$field_state = $this
->createInstance($type);
$title = t('Add new field state: @type', [
'@type' => $field_state
->label(),
]);
$submit_label = t('Add');
$op = 'new';
}
$element['form']['edit'] = $field_state
->buildConfigurationForm([], $form_state);
$element['form']['edit']['#type'] = 'fieldset';
$element['form']['edit']['#title'] = $title;
$element['form']['edit']['submit'] = [
'save' => [
'#type' => 'submit',
'#value' => $submit_label,
'#validate' => [
[
self::class,
'settingsValidate',
],
],
'#submit' => [
[
self::class,
'settingsSubmit',
],
],
'#field_name' => $field_name,
'#op' => $op,
'#plugin' => $type,
],
'cancel' => $cancel,
];
}
elseif ($form_state
->get('field_states_ui_edit') == 'delete') {
$element['form']['delete'] = [
'#type' => 'fieldset',
'#tree' => FALSE,
'#title' => t('Delete field state?'),
'delete_submit' => [
'#type' => 'submit',
'#value' => t('Confirm Delete'),
'#submit' => [
[
self::class,
'settingsSubmit',
],
],
'#target' => $form_state
->get('field_states_ui_target'),
'#op' => 'process_delete',
'#field_name' => $field_name,
],
'cancel' => $cancel,
];
}
else {
$field_state_options = [];
$field_states = $this
->getDefinitions();
foreach ($field_states as $field_state => $definition) {
$field_state_options[$field_state] = $definition['label'];
}
$element['form']['type'] = [
'#type' => 'select',
'#title' => t('Field States'),
'#title_display' => 'invisible',
'#options' => $field_state_options,
'#empty_option' => t('Select a new field state'),
];
$element['form']['add'] = [
'#type' => 'submit',
'#value' => t('Add'),
'#validate' => [
[
self::class,
'settingsValidate',
],
],
'#submit' => [
[
self::class,
'settingsSubmit',
],
],
'#field_name' => $field_name,
'#op' => 'add',
];
}
return $element;
}