You are here

function contact_storage_contact_form_form_submit in Contact Storage 8

Contact form's form submission handler.

Parameters

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

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

1 string reference to 'contact_storage_contact_form_form_submit'
contact_storage_form_contact_form_form_alter in ./contact_storage.module
Implements hook_form_FORM_ID_alter() for contact_form_form().

File

./contact_storage.module, line 164
Contains main module logic.

Code

function contact_storage_contact_form_form_submit(&$form, FormStateInterface &$formState) {
  $alias_storage = \Drupal::entityTypeManager()
    ->getStorage('path_alias');
  $entity = $formState
    ->getFormObject()
    ->getEntity();
  if ($old_alias = $formState
    ->get('path_alias_id')) {
    $old_alias = $alias_storage
      ->load($old_alias);
  }
  $new_alias = $formState
    ->getValue('contact_storage_url_alias');

  // If there isn't an alias, set a new one.
  if (!$old_alias) {
    if ($new_alias) {
      $alias_storage
        ->create([
        'path' => '/' . $entity
          ->toUrl()
          ->getInternalPath(),
        'alias' => $formState
          ->getValue('contact_storage_url_alias'),
      ])
        ->save();
    }
  }
  else {

    // Delete old alias if user erased it.
    if ($old_alias && !$new_alias) {
      $old_alias
        ->delete();
    }
    elseif ($new_alias) {
      $old_alias
        ->setAlias($formState
        ->getValue('contact_storage_url_alias'))
        ->save();
    }
  }
}