You are here

public function AuthorizationProfileForm::submitForm in Authorization 8

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides EntityForm::submitForm

File

src/Form/AuthorizationProfileForm.php, line 604

Class

AuthorizationProfileForm
Authorization profile form.

Namespace

Drupal\authorization\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $authorization_profile = $this
    ->getEntity();

  // Check before loading the provider plugin so we don't throw an exception.
  if ($form['provider_config']['#type'] === 'details' && $authorization_profile
    ->hasValidProvider()) {
    $provider_form_state = new SubFormState($form_state, [
      'provider_config',
    ]);
    $authorization_profile
      ->getProvider()
      ->submitConfigurationForm($form['provider_config'], $provider_form_state);
  }

  // Check before loading the consumer plugin so we don't throw an exception.
  if ($form['consumer_config']['#type'] === 'details' && $authorization_profile
    ->hasValidConsumer()) {
    $consumer_form_state = new SubFormState($form_state, [
      'consumer_config',
    ]);
    $authorization_profile
      ->getConsumer()
      ->submitConfigurationForm($form['consumer_config'], $consumer_form_state);
  }
  if ($form['mappings']) {
    $mappings_form_state = new SubFormState($form_state, [
      'mappings',
    ]);
    $authorization_profile
      ->getConsumer()
      ->submitRowForm($form['mappings'], $mappings_form_state);
    $authorization_profile
      ->getProvider()
      ->submitRowForm($form['mappings'], $mappings_form_state);
    $values = $form_state
      ->getValues();
    $provider_mappings = $this
      ->extractArrayByName($values['mappings'], 'provider_mappings');
    $consumer_mappings = $this
      ->extractArrayByName($values['mappings'], 'consumer_mappings');
    foreach ($values['mappings'] as $key => $value) {
      if (empty($value) || $value['delete'] == 1) {
        unset($provider_mappings[$key]);
        unset($consumer_mappings[$key]);
      }
    }
    if ($provider_mappings && $consumer_mappings) {
      $authorization_profile
        ->setProviderMappings(array_values($provider_mappings));
      $authorization_profile
        ->setConsumerMappings(array_values($consumer_mappings));
    }
  }
  return $authorization_profile;
}