function SwitchShortcutSet::submitForm

Same name in other branches
  1. 9 core/modules/shortcut/src/Form/SwitchShortcutSet.php \Drupal\shortcut\Form\SwitchShortcutSet::submitForm()
  2. 8.9.x core/modules/shortcut/src/Form/SwitchShortcutSet.php \Drupal\shortcut\Form\SwitchShortcutSet::submitForm()
  3. 11.x core/modules/shortcut/src/Form/SwitchShortcutSet.php \Drupal\shortcut\Form\SwitchShortcutSet::submitForm()

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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.