You are here

public function WebformEntitySettingsFormForm::save in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/EntitySettings/WebformEntitySettingsFormForm.php \Drupal\webform\EntitySettings\WebformEntitySettingsFormForm::save()

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 WebformEntitySettingsBaseForm::save

File

src/EntitySettings/WebformEntitySettingsFormForm.php, line 704

Class

WebformEntitySettingsFormForm
Webform form settings.

Namespace

Drupal\webform\EntitySettings

Code

public function save(array $form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $this
    ->getEntity();

  // Set open and close date/time.
  $webform
    ->set('open', NULL);
  $webform
    ->set('close', NULL);
  if ($values['status'] === WebformInterface::STATUS_SCHEDULED) {

    // Massage open/close dates.
    // @see \Drupal\webform\Plugin\Field\FieldWidget\WebformEntityReferenceAutocompleteWidget::massageFormValues
    // @see \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase::massageFormValues
    $states = [
      'open',
      'close',
    ];
    foreach ($states as $state) {
      if (!empty($values[$state]) && $values[$state] instanceof DrupalDateTime) {
        $webform
          ->set($state, WebformDateHelper::formatStorage($values[$state]));
      }
    }
  }

  // Set custom properties, class, and style.
  $elements = $webform
    ->getElementsDecoded();
  $elements = WebformElementHelper::removeProperties($elements);
  $properties = [];

  // Unset custom method and action.
  unset($properties['#method'], $properties['#action']);

  // Set custom method and action.
  if (!empty($values['method'])) {
    $properties['#method'] = $values['method'];
    if (!empty($values['action'])) {
      $properties['#action'] = $values['action'];
    }
  }

  // Set custom properties.
  if (!empty($values['custom'])) {
    $properties += WebformArrayHelper::addPrefix($values['custom']);
  }

  // Set custom attributions.
  if (!empty($values['attributes'])) {
    $properties['#attributes'] = $values['attributes'];
  }

  // Prepend form properties to elements.
  $elements = $properties + $elements;

  // Save elements.
  $webform
    ->setElements($elements);

  // Remove custom properties and attributes.
  unset($values['method'], $values['action'], $values['attributes'], $values['custom']);

  // Remove main properties.
  unset($values['status'], $values['open'], $values['close']);

  // Remove *_disabled form behavior properties.
  $form_behaviors = $this
    ->getFormBehaviors();
  foreach ($form_behaviors as $form_behavior_key => $form_behavior_element) {
    if (isset($form_behavior_element['all_description'])) {
      unset($values[$form_behavior_key . '_disabled']);
    }
  }

  // Set settings.
  $webform
    ->setSettings($values);
  parent::save($form, $form_state);
}