You are here

public function SwitchShortcutSet::submitForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/shortcut/src/Form/SwitchShortcutSet.php \Drupal\shortcut\Form\SwitchShortcutSet::submitForm()
  2. 10 core/modules/shortcut/src/Form/SwitchShortcutSet.php \Drupal\shortcut\Form\SwitchShortcutSet::submitForm()

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

core/modules/shortcut/src/Form/SwitchShortcutSet.php, line 172

Class

SwitchShortcutSet
Builds the shortcut set switch form.

Namespace

Drupal\shortcut\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $account = $this
    ->currentUser();
  $account_is_user = $this->user
    ->id() == $account
    ->id();
  if ($form_state
    ->getValue('set') == 'new') {

    // Save a new shortcut set with links copied from the user's default set.

    /* @var \Drupal\shortcut\Entity\ShortcutSet $set */
    $set = $this->shortcutSetStorage
      ->create([
      'id' => $form_state
        ->getValue('id'),
      'label' => $form_state
        ->getValue('label'),
    ]);
    $set
      ->save();
    $replacements = [
      '%user' => $this->user
        ->label(),
      '%set_name' => $set
        ->label(),
      ':switch-url' => Url::fromRoute('<current>')
        ->toString(),
    ];
    if ($account_is_user) {

      // Only administrators can create new shortcut sets, so we know they have
      // access to switch back.
      $this
        ->messenger()
        ->addStatus($this
        ->t('You are now using the new %set_name shortcut set. You can edit it from this page or <a href=":switch-url">switch back to a different one.</a>', $replacements));
    }
    else {
      $this
        ->messenger()
        ->addStatus($this
        ->t('%user is now using a new shortcut set called %set_name. You can edit it from this page.', $replacements));
    }
    $form_state
      ->setRedirect('entity.shortcut_set.customize_form', [
      'shortcut_set' => $set
        ->id(),
    ]);
  }
  else {

    // Switch to a different shortcut set.

    /* @var \Drupal\shortcut\Entity\ShortcutSet $set */
    $set = $this->shortcutSetStorage
      ->load($form_state
      ->getValue('set'));
    $replacements = [
      '%user' => $this->user
        ->getDisplayName(),
      '%set_name' => $set
        ->label(),
    ];
    $this
      ->messenger()
      ->addStatus($account_is_user ? $this
      ->t('You are now using the %set_name shortcut set.', $replacements) : $this
      ->t('%user is now using the %set_name shortcut set.', $replacements));
  }

  // Assign the shortcut set to the provided user account.
  $this->shortcutSetStorage
    ->assignUser($set, $this->user);
}