You are here

public function MetatagCustomCreateForm::submitForm in Metatag Routes 8

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/MetatagCustomCreateForm.php, line 154

Class

MetatagCustomCreateForm
Class MetatagCustomCreateForm.

Namespace

Drupal\metatag_routes\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Get values for form submission.
  $route_name = $form_state
    ->getValue('route_name');
  $url = $form_state
    ->getValue('metatag_url');
  if ($route_name && $url) {

    // Create the new metatag entity.
    $entity = $this->entityTypeManager
      ->getStorage('metatag_defaults')
      ->create([
      'id' => $route_name,
      'label' => $url,
    ]);
    $entity
      ->save();
    $this
      ->messenger()
      ->addStatus($this
      ->t('Created metatags for the path: @url. Internal route: @route.', [
      '@url' => $url,
      '@route' => $route_name,
    ]));

    // Redirect to metatag edit page.
    $form_state
      ->setRedirect('entity.metatag_defaults.edit_form', [
      'metatag_defaults' => $route_name,
    ]);
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('The metatags could not be created for the path: @url.', [
      '@url' => $url,
    ]));

    // Redirect to metatag edit page.
    $form_state
      ->setRedirect('entity.metatag_defaults.collection');
  }
}