You are here

public function XmlSitemapForm::save in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 src/Form/XmlSitemapForm.php \Drupal\xmlsitemap\Form\XmlSitemapForm::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/XmlSitemapForm.php, line 57

Class

XmlSitemapForm
Provides a form for creating and editing xmlsitemap entities.

Namespace

Drupal\xmlsitemap\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  if (!$form_state
    ->hasValue('context')) {
    $form_state
      ->setValue('context', xmlsitemap_get_current_context());
  }
  if ($form_state
    ->hasValue([
    'context',
    'language',
  ])) {
    $language = $form_state
      ->getValue([
      'context',
      'language',
    ]);
    if ($language == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
      $form_state
        ->unsetValue([
        'context',
        'language',
      ]);
    }
  }
  $context = $form_state
    ->getValue('context');
  $this->entity->context = $context;
  $this->entity->label = $form_state
    ->getValue('label');
  $this->entity->id = xmlsitemap_sitemap_get_context_hash($context);
  try {
    $status = $this->entity
      ->save();
    if ($status == SAVED_NEW) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Saved the %label sitemap.', [
        '%label' => $this->entity
          ->label(),
      ]));
    }
    elseif ($status == SAVED_UPDATED) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Updated the %label sitemap.', [
        '%label' => $this->entity
          ->label(),
      ]));
    }
  } catch (EntityStorageException $ex) {
    $this
      ->messenger()
      ->addError($this
      ->t('There is another sitemap saved with the same context.'));
  }
  $form_state
    ->setRedirect('xmlsitemap.admin_search');
}