You are here

public function YamlFormEntitySettingsForm::save in YAML Form 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/YamlFormEntitySettingsForm.php, line 829

Class

YamlFormEntitySettingsForm
Provides a form to manage settings.

Namespace

Drupal\yamlform

Code

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

  /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
  $yamlform = $this
    ->getEntity();

  // Set custom properties, class, and style.
  $elements = $yamlform
    ->getElementsDecoded();
  $elements = YamlFormElementHelper::removeProperties($elements);
  $properties = [];
  if (!empty($values['method'])) {
    $properties['#method'] = $values['method'];
  }
  if (!empty($values['action'])) {
    $properties['#action'] = $values['action'];
  }
  if (!empty($values['custom'])) {
    $properties += YamlFormArrayHelper::addPrefix($values['custom']);
  }
  if (!empty($values['attributes'])) {
    $properties['#attributes'] = $values['attributes'];
  }
  $elements = $properties + $elements;
  $yamlform
    ->setElements($elements);

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

  /** @var \Drupal\yamlform\YamlFormSubmissionStorageInterface $submission_storage */
  $submission_storage = $this->entityTypeManager
    ->getStorage('yamlform_submission');

  // Set next serial number.
  $next_serial = (int) $values['next_serial'];
  $max_serial = $submission_storage
    ->getMaxSerial($yamlform);
  if ($next_serial < $max_serial) {
    drupal_set_message($this
      ->t('The next submission number was increased to @min to make it higher than existing submissions.', [
      '@min' => $max_serial,
    ]));
    $next_serial = $max_serial;
  }
  $yamlform
    ->setState('next_serial', $next_serial);

  // Remove main properties.
  unset($values['id'], $values['title'], $values['description'], $values['template'], $values['status'], $values['uid'], $values['next_serial']);

  // Remove disabled properties.
  unset($values['form_novalidate_disabled'], $values['form_unsaved_disabled'], $values['form_details_toggle_disabled']);

  // Set settings.
  $yamlform
    ->setSettings($values);

  // Save the form.
  $yamlform
    ->save();
  $this
    ->logger('yamlform')
    ->notice('Form settings @label saved.', [
    '@label' => $yamlform
      ->label(),
  ]);
  drupal_set_message($this
    ->t('Form settings %label saved.', [
    '%label' => $yamlform
      ->label(),
  ]));
}