Submit function to add/edit field states.

1 string reference to 'field_states_ui_submit'
field_states_ui_settings_form in ./field_states_ui.admin.inc
Implements hook_field_widget_third_party_settings_form().

File

./field_states_ui.admin.inc, line 189

Code

function field_states_ui_submit($form, FormStateInterface $form_state) {
    $trigger = $form_state->getTriggeringElement();
    $field_name = $trigger['#field_name'];
    $form_state->set('plugin_settings_edit', $field_name);
    // Show the add new field state form for the specified field state type.
    if ($trigger['#op'] == 'add') {
        $form_state->set('field_states_ui_edit', $field_name);
    }
    elseif ($trigger['#op'] == 'cancel') {
        $form_state->set('field_states_ui_edit', NULL);
        $form_state->set('field_states_ui_target', NULL);
    }
    elseif ($trigger['#op'] == 'edit') {
        $form_state->set('field_states_ui_edit', $field_name);
        $form_state->set('field_states_ui_target', $trigger['#key']);
    }
    elseif ($trigger['#op'] == 'delete') {
        $form_state->set('field_states_ui_edit', 'delete');
        $form_state->set('field_states_ui_target', $trigger['#key']);
    }
    elseif ($trigger['#op'] == 'process_delete') {
        $entity = $form_state->getFormObject()
            ->getEntity();
        $field = $entity->getComponent($field_name);
        unset($field['third_party_settings']['field_states_ui']['field_states'][$form_state->get('field_states_ui_target')]);
        $entity->setComponent($field_name, $field);
        $entity->save();
        $form_state->set('field_states_ui_edit', NULL);
        $form_state->set('field_states_ui_target', NULL);
    }
    elseif ($trigger['#op'] == 'new') {
        $entity = $form_state->getFormObject()
            ->getEntity();
        $field = $entity->getComponent($field_name);
        $field_state_manager = Drupal::service('plugin.manager.field_states_ui.fieldstate');
        $field_state = $field_state_manager->createInstance($trigger['#plugin']);
        $field_state_data = $form_state->getValue([
            'fields',
            $field_name,
            'settings_edit_form',
            'third_party_settings',
            'field_states_ui',
            'form',
            'edit',
        ]);
        $field_state->submitConfigurationForm($form, (new FormState())->setValues($field_state_data));
        $field['third_party_settings']['field_states_ui']['field_states'][$field_state->getUuid()] = $field_state->getConfiguration();
        $entity->setComponent($field_name, $field);
        $entity->save();
        $form_state->set('field_states_ui_edit', NULL);
    }
    elseif ($trigger['#op'] == 'process_update') {
        $entity = $form_state->getFormObject()
            ->getEntity();
        $field = $entity->getComponent($field_name);
        $field_state_manager = Drupal::service('plugin.manager.field_states_ui.fieldstate');
        $target = $form_state->get('field_states_ui_target');
        $field_state = $field_state_manager->createInstance($trigger['#plugin'], $field['third_party_settings']['field_states_ui']['field_states'][$target]);
        $field_state_data = $form_state->getValue([
            'fields',
            $field_name,
            'settings_edit_form',
            'third_party_settings',
            'field_states_ui',
            'form',
            'edit',
        ]);
        $field_state->submitConfigurationForm($form, (new FormState())->setValues($field_state_data));
        $field['third_party_settings']['field_states_ui']['field_states'][$field_state->getUuid()] = $field_state->getConfiguration();
        $entity->setComponent($field_name, $field);
        $entity->save();
        $form_state->set('field_states_ui_edit', NULL);
        $form_state->set('field_states_ui_target', NULL);
    }
    $form_state->setRebuild();
}