Implements hook_field_widget_third_party_settings_form().

1 call to field_states_ui_settings_form()
field_states_ui_field_widget_third_party_settings_form in ./field_states_ui.module
Implements hook_field_widget_third_party_settings_form().

File

./field_states_ui.admin.inc, line 16

Code

function field_states_ui_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
    $current = $plugin->getThirdPartySetting('field_states_ui', 'states', []);
    $field_state_manager = Drupal::service('plugin.manager.field_states_ui.fieldstate');
    $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' => [
            'field_states_ui_submit',
        ],
        '#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 = $field_state_manager->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' => [
                            'field_states_ui_submit',
                        ],
                        '#field_name' => $field_name,
                        '#key' => $key,
                        '#name' => 'edit-' . $key,
                    ],
                    'delete' => [
                        '#type' => 'submit',
                        '#value' => t('Delete'),
                        '#op' => 'delete',
                        '#submit' => [
                            'field_states_ui_submit',
                        ],
                        '#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 = $field_state_manager->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 = $field_state_manager->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' => [
                    'field_states_ui_validate',
                ],
                '#submit' => [
                    'field_states_ui_submit',
                ],
                '#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' => [
                    'field_states_ui_submit',
                ],
                '#target' => $form_state->get('field_states_ui_target'),
                '#op' => 'process_delete',
                '#field_name' => $field_name,
            ],
            'cancel' => $cancel,
        ];
    }
    else {
        $field_state_options = [];
        $field_states = $field_state_manager->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' => [
                'field_states_ui_validate',
            ],
            '#submit' => [
                'field_states_ui_submit',
            ],
            '#field_name' => $field_name,
            '#op' => 'add',
        ];
    }
    return $element;
}