SwitchShortcutSet::submitForm

public SwitchShortcutSet::submitForm(array &$form, FormStateInterface $form_state)

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 169

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(array(
      'id' => $form_state->getValue('id'),
      'label' => $form_state->getValue('label'),
    ));
    $set->save();
    $replacements = array(
      '%user' => $this->user->label(),
      '%set_name' => $set->label(),
      ':switch-url' => $this->url('<current>'),
    );
    if ($account_is_user) {
      // Only administrators can create new shortcut sets, so we know they have
      // access to switch back.
      drupal_set_message($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 {
      drupal_set_message($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', 
    array('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 = array(
      '%user' => $this->user->getDisplayName(),
      '%set_name' => $set->label(),
    );
    drupal_set_message($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);
}
doc_Drupal
2016-10-29 09:45:32
Comments
Leave a Comment

Please login to continue.