You are here

public function WebformCompositeForm::save in Webform Composite Tools 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

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

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/WebformCompositeForm.php, line 151

Class

WebformCompositeForm
Form handler for the Composite add and edit forms.

Namespace

Drupal\webform_composite\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $composite = $this->entity;

  // Rekey and prefix elements into an render array.
  $elements = [];
  foreach ($form_state
    ->getValue('elements') as $key => $element) {
    $element = array_filter($element);
    $elements[$key] = WebformArrayHelper::addPrefix($element);
  }

  // Update the values stored on the composite.
  $composite
    ->set('elements', Yaml::encode($elements));
  $status = $composite
    ->save();
  if ($status) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Composite %label has been saved.', [
      '%label' => $composite
        ->label(),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Composite %label was not saved.', [
      '%label' => $composite
        ->label(),
    ]), 'error');
  }
  $form_state
    ->setRedirect('entity.webform_composite.list');
}