You are here

public function KeyFormBase::submitForm in Key 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/KeyFormBase.php, line 277

Class

KeyFormBase
Base form for key add and edit forms.

Namespace

Drupal\key\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $key_value_data = $form_state
    ->get('key_value');
  foreach ($this->entity
    ->getPlugins() as $type => $plugin) {
    if ($plugin instanceof KeyPluginFormInterface) {
      $plugin_form_state = $this
        ->createPluginFormState($type, $form_state);
      $plugin
        ->submitConfigurationForm($form, $plugin_form_state);
      $form_state
        ->setValue($type . '_settings', $plugin_form_state
        ->getValues());
    }
  }

  // If the key provider allows a key value to be set.
  if ($this->entity
    ->getKeyProvider() instanceof KeyProviderSettableValueInterface) {
    $set_key_value = FALSE;

    // If the key provider has changed, the key value should be set.
    if ($this->originalKey && $this->originalKey
      ->getKeyProvider()
      ->getPluginId() != $this->entity
      ->getKeyProvider()
      ->getPluginId()) {
      $set_key_value = TRUE;
    }

    // If the submitted value is not the same as the obscured value,
    // the key value should be set.
    if ($key_value_data['submitted'] != $key_value_data['obscured']) {
      $set_key_value = TRUE;
    }

    // If the processed value is not empty, but the submitted value is,
    // the key value should be set.
    if (!empty($key_value_data['processed_original']) && empty($key_value_data['submitted'])) {
      $set_key_value = TRUE;
    }

    // Set the key value in the entity, if necessary.
    if ($set_key_value) {
      $this->entity
        ->setKeyValue($key_value_data['processed_submitted']);
    }
  }
  parent::submitForm($form, $form_state);
}