class SwitchShortcutSet in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/shortcut/src/Form/SwitchShortcutSet.php \Drupal\shortcut\Form\SwitchShortcutSet
 
Builds the shortcut set switch form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\shortcut\Form\SwitchShortcutSet
 
 
Expanded class hierarchy of SwitchShortcutSet
1 string reference to 'SwitchShortcutSet'
- shortcut.routing.yml in core/
modules/ shortcut/ shortcut.routing.yml  - core/modules/shortcut/shortcut.routing.yml
 
File
- core/
modules/ shortcut/ src/ Form/ SwitchShortcutSet.php, line 20  - Contains \Drupal\shortcut\Form\SwitchShortcutSet.
 
Namespace
Drupal\shortcut\FormView source
class SwitchShortcutSet extends FormBase {
  /**
   * The account the shortcut set is for.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $user;
  /**
   * The shortcut set storage.
   *
   * @var \Drupal\shortcut\ShortcutSetStorageInterface
   */
  protected $shortcutSetStorage;
  /**
   * Constructs a SwitchShortcutSet object.
   *
   * @param \Drupal\shortcut\ShortcutSetStorageInterface $shortcut_set_storage
   *   The shortcut set storage.
   */
  public function __construct(ShortcutSetStorageInterface $shortcut_set_storage) {
    $this->shortcutSetStorage = $shortcut_set_storage;
  }
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.manager')
      ->getStorage('shortcut_set'));
  }
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'shortcut_set_switch';
  }
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
    $account = $this
      ->currentUser();
    $this->user = $user;
    // Prepare the list of shortcut sets.
    $options = array_map(function (ShortcutSet $set) {
      return $set
        ->label();
    }, $this->shortcutSetStorage
      ->loadMultiple());
    $current_set = shortcut_current_displayed_set($this->user);
    // Only administrators can add shortcut sets.
    $add_access = $account
      ->hasPermission('administer shortcuts');
    if ($add_access) {
      $options['new'] = $this
        ->t('New set');
    }
    $account_is_user = $this->user
      ->id() == $account
      ->id();
    if (count($options) > 1) {
      $form['set'] = array(
        '#type' => 'radios',
        '#title' => $account_is_user ? $this
          ->t('Choose a set of shortcuts to use') : $this
          ->t('Choose a set of shortcuts for this user'),
        '#options' => $options,
        '#default_value' => $current_set
          ->id(),
      );
      $form['label'] = array(
        '#type' => 'textfield',
        '#title' => $this
          ->t('Label'),
        '#description' => $this
          ->t('The new set is created by copying items from your default shortcut set.'),
        '#access' => $add_access,
        '#states' => array(
          'visible' => array(
            ':input[name="set"]' => array(
              'value' => 'new',
            ),
          ),
          'required' => array(
            ':input[name="set"]' => array(
              'value' => 'new',
            ),
          ),
        ),
      );
      $form['id'] = array(
        '#type' => 'machine_name',
        '#machine_name' => array(
          'exists' => array(
            $this,
            'exists',
          ),
          'replace_pattern' => '[^a-z0-9-]+',
          'replace' => '-',
        ),
        // This ID could be used for menu name.
        '#maxlength' => 23,
        '#states' => array(
          'required' => array(
            ':input[name="set"]' => array(
              'value' => 'new',
            ),
          ),
        ),
        '#required' => FALSE,
      );
      if (!$account_is_user) {
        $default_set = $this->shortcutSetStorage
          ->getDefaultSet($this->user);
        $form['new']['#description'] = $this
          ->t('The new set is created by copying items from the %default set.', array(
          '%default' => $default_set
            ->label(),
        ));
      }
      $form['actions'] = array(
        '#type' => 'actions',
      );
      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => $this
          ->t('Change set'),
      );
    }
    else {
      // There is only 1 option, so output a message in the $form array.
      $form['info'] = array(
        '#markup' => '<p>' . $this
          ->t('You are currently using the %set-name shortcut set.', array(
          '%set-name' => $current_set
            ->label(),
        )) . '</p>',
      );
    }
    return $form;
  }
  /**
   * Determines if a shortcut set exists already.
   *
   * @param string $id
   *   The set ID to check.
   *
   * @return bool
   *   TRUE if the shortcut set exists, FALSE otherwise.
   */
  public function exists($id) {
    return (bool) $this->shortcutSetStorage
      ->getQuery()
      ->condition('id', $id)
      ->execute();
  }
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('set') == 'new') {
      // Check to prevent creating a shortcut set with an empty title.
      if (trim($form_state
        ->getValue('label')) == '') {
        $form_state
          ->setErrorByName('label', $this
          ->t('The new set label is required.'));
      }
    }
  }
  /**
   * {@inheritdoc}
   */
  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);
  }
  /**
   * Checks access for the shortcut set switch form.
   *
   * @param \Drupal\user\UserInterface $user
   *   (optional) The owner of the shortcut set.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function checkAccess(UserInterface $user = NULL) {
    return shortcut_set_switch_access($user);
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            DependencySerializationTrait:: | 
                  protected | property | An array of service IDs keyed by property name used for serialization. | |
| 
            DependencySerializationTrait:: | 
                  public | function | 1 | |
| 
            DependencySerializationTrait:: | 
                  public | function | 2 | |
| 
            FormBase:: | 
                  protected | property | The config factory. | 3 | 
| 
            FormBase:: | 
                  protected | property | The logger factory. | |
| 
            FormBase:: | 
                  protected | property | The request stack. | 1 | 
| 
            FormBase:: | 
                  protected | property | The route match. | |
| 
            FormBase:: | 
                  protected | function | Retrieves a configuration object. | |
| 
            FormBase:: | 
                  protected | function | Gets the config factory for this form. | 3 | 
| 
            FormBase:: | 
                  private | function | Returns the service container. | |
| 
            FormBase:: | 
                  protected | function | Gets the current user. | |
| 
            FormBase:: | 
                  protected | function | Gets the request object. | |
| 
            FormBase:: | 
                  protected | function | Gets the route match. | |
| 
            FormBase:: | 
                  protected | function | Gets the logger for a specific channel. | |
| 
            FormBase:: | 
                  public | function | Resets the configuration factory. | |
| 
            FormBase:: | 
                  public | function | Sets the config factory for this form. | |
| 
            FormBase:: | 
                  public | function | Sets the request stack object to use. | |
| 
            LinkGeneratorTrait:: | 
                  protected | property | The link generator. | 1 | 
| 
            LinkGeneratorTrait:: | 
                  protected | function | Returns the link generator. | |
| 
            LinkGeneratorTrait:: | 
                  protected | function | Renders a link to a route given a route name and its parameters. | |
| 
            LinkGeneratorTrait:: | 
                  public | function | Sets the link generator service. | |
| 
            RedirectDestinationTrait:: | 
                  protected | property | The redirect destination service. | |
| 
            RedirectDestinationTrait:: | 
                  protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| 
            RedirectDestinationTrait:: | 
                  protected | function | Returns the redirect destination service. | |
| 
            RedirectDestinationTrait:: | 
                  public | function | Sets the redirect destination service. | |
| 
            StringTranslationTrait:: | 
                  protected | property | The string translation service. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Formats a string containing a count of items. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Returns the number of plurals supported by a given language. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Gets the string translation service. | |
| 
            StringTranslationTrait:: | 
                  public | function | Sets the string translation service to use. | 2 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Translates a string to the current language or to a given language. | |
| 
            SwitchShortcutSet:: | 
                  protected | property | The shortcut set storage. | |
| 
            SwitchShortcutSet:: | 
                  protected | property | The account the shortcut set is for. | |
| 
            SwitchShortcutSet:: | 
                  public | function | 
            Form constructor. Overrides FormInterface:: | 
                  |
| 
            SwitchShortcutSet:: | 
                  public | function | Checks access for the shortcut set switch form. | |
| 
            SwitchShortcutSet:: | 
                  public static | function | 
            Instantiates a new instance of this class. Overrides FormBase:: | 
                  |
| 
            SwitchShortcutSet:: | 
                  public | function | Determines if a shortcut set exists already. | |
| 
            SwitchShortcutSet:: | 
                  public | function | 
            Returns a unique string identifying the form. Overrides FormInterface:: | 
                  |
| 
            SwitchShortcutSet:: | 
                  public | function | 
            Form submission handler. Overrides FormInterface:: | 
                  |
| 
            SwitchShortcutSet:: | 
                  public | function | 
            Form validation handler. Overrides FormBase:: | 
                  |
| 
            SwitchShortcutSet:: | 
                  public | function | Constructs a SwitchShortcutSet object. | |
| 
            UrlGeneratorTrait:: | 
                  protected | property | The url generator. | |
| 
            UrlGeneratorTrait:: | 
                  protected | function | Returns the URL generator service. | |
| 
            UrlGeneratorTrait:: | 
                  protected | function | Returns a redirect response object for the specified route. | |
| 
            UrlGeneratorTrait:: | 
                  public | function | Sets the URL generator service. | |
| 
            UrlGeneratorTrait:: | 
                  protected | function | Generates a URL or path for a specific route based on the given parameters. |