You are here

public function AgreementForm::submitForm in Agreement 3.0.x

Same name in this branch
  1. 3.0.x src/Form/AgreementForm.php \Drupal\agreement\Form\AgreementForm::submitForm()
  2. 3.0.x src/Entity/AgreementForm.php \Drupal\agreement\Entity\AgreementForm::submitForm()
Same name and namespace in other branches
  1. 8.2 src/Entity/AgreementForm.php \Drupal\agreement\Entity\AgreementForm::submitForm()

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/Entity/AgreementForm.php, line 288

Class

AgreementForm
Add or edit agreements.

Namespace

Drupal\agreement\Entity

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $pages_array = [];
  $agreement_text = $form_state
    ->getValue('agreement');
  $visibility = $form_state
    ->getValue([
    'settings',
    'visibility',
    'settings',
  ]);
  $visibility_pages = $form_state
    ->getValue([
    'settings',
    'visibility',
    'pages',
  ]);
  $roles = array_values($form_state
    ->getValue([
    'settings',
    'roles',
  ]));

  // Normalizes the visibility pages form state into an array.
  if ($visibility_pages) {
    $list = explode("\n", $visibility_pages);
    $list = array_map('trim', $list);
    $pages_array = array_filter($list, 'strlen');
  }
  $form_state
    ->setValue('agreement', $agreement_text['value']);
  $form_state
    ->setValue([
    'settings',
    'format',
  ], $agreement_text['format']);
  $form_state
    ->setValue([
    'settings',
    'visibility',
    'pages',
  ], $pages_array);
  $form_state
    ->setValue([
    'settings',
    'visibility',
    'settings',
  ], (int) $visibility);
  $form_state
    ->setValue([
    'settings',
    'frequency',
  ], (double) $form_state
    ->getValue([
    'settings',
    'frequency',
  ]));
  $form_state
    ->setValue([
    'settings',
    'roles',
  ], $roles);
  $form_state
    ->setRedirect('entity.agreement.collection');
  parent::submitForm($form, $form_state);
}