You are here

public function EntityLegalDocumentForm::save in Entity Legal 8.2

Same name and namespace in other branches
  1. 8 src/Form/EntityLegalDocumentForm.php \Drupal\entity_legal\Form\EntityLegalDocumentForm::save()
  2. 4.0.x src/Form/EntityLegalDocumentForm.php \Drupal\entity_legal\Form\EntityLegalDocumentForm::save()
  3. 3.0.x src/Form/EntityLegalDocumentForm.php \Drupal\entity_legal\Form\EntityLegalDocumentForm::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 EntityForm::save

File

src/Form/EntityLegalDocumentForm.php, line 335

Class

EntityLegalDocumentForm
Base form for contact form edit forms.

Namespace

Drupal\entity_legal\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $this->entity
    ->set('require_signup', $this->entity
    ->get('settings')['new_users']['require']);
  $this->entity
    ->set('require_existing', $this->entity
    ->get('settings')['existing_users']['require']);
  $status = $this->entity
    ->save();
  if ($status == SAVED_NEW) {
    $form_state
      ->setRedirect('entity.entity_legal_document_version.add_form', [
      'entity_legal_document' => $this->entity
        ->id(),
    ]);
  }
  if (!empty($form_state
    ->getValues()['path'][0]) && (!empty($form_state
    ->getValues()['path'][0]['alias']) || !empty($form_state
    ->getValues()['path'][0]['pid']))) {
    $path = $form_state
      ->getValues()['path'][0];
    $path['alias'] = trim($path['alias']);
    if (!$path['source']) {
      $path['source'] = $this->entity
        ->toUrl()
        ->toString();
    }

    // Delete old alias if user erased it.
    if (!empty($path['pid']) && empty($path['alias'])) {
      $this->aliasStorage
        ->delete([
        'pid' => $path['pid'],
      ]);
    }
    else {
      $this->aliasStorage
        ->save($path['source'], $path['alias'], LanguageInterface::LANGCODE_NOT_SPECIFIED, $path['pid']);
    }
  }

  // Update the published version.
  if ($form_state
    ->get('published_version') && $form_state
    ->get('published_version') !== $form_state
    ->getValue('published_version')) {
    $storage = $this->entityTypeManager
      ->getStorage(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME);

    /** @var \Drupal\entity_legal\EntityLegalDocumentVersionInterface $published_version */
    $published_version = $storage
      ->load($form_state
      ->getValue('published_version'));
    $this->entity
      ->setPublishedVersion($published_version);
  }
}