/** * {@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' => '-'), '#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; }
/** * Test the shortcut set migration. */ public function testShortcutSetUsersMigration() { // Check if migrated user has correct migrated shortcut set assigned. $account = User::load(2); $shortcut_set = shortcut_current_displayed_set($account); /** @var \Drupal\shortcut\ShortcutSetInterface $shortcut_set */ $this->assertIdentical('shortcut_set_2', $shortcut_set->id()); }
/** * {@inheritdoc} */ protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { switch ($operation) { case 'update': if ($account->hasPermission('administer shortcuts')) { return AccessResult::allowed()->cachePerPermissions(); } if (!$account->hasPermission('access shortcuts')) { return AccessResult::neutral()->cachePerPermissions(); } return AccessResult::allowedIf($account->hasPermission('customize shortcut links') && $entity == shortcut_current_displayed_set($account))->cachePerPermissions()->addCacheableDependency($entity); case 'delete': return AccessResult::allowedIf($account->hasPermission('administer shortcuts') && $entity->id() != 'default')->cachePerPermissions(); default: // No opinion. return AccessResult::neutral(); } }
/** * {@inheritdoc} */ protected function checkAccess(EntityInterface $entity, $operation, $langcode, AccountInterface $account) { switch ($operation) { case 'update': if ($account->hasPermission('administer shortcuts')) { return TRUE; } if (!$account->hasPermission('access shortcuts')) { return FALSE; } if ($account->hasPermission('customize shortcut links')) { return $entity == shortcut_current_displayed_set($account); } return FALSE; break; case 'delete': if (!$account->hasPermission('administer shortcuts')) { return FALSE; } return $entity->id() != 'default'; break; } }
/** * Tests unassigning a shortcut set. */ function testShortcutSetUnassign() { $new_set = $this->generateShortcutSet($this->randomMachineName()); $shortcut_set_storage = \Drupal::entityManager()->getStorage('shortcut_set'); $shortcut_set_storage->assignUser($new_set, $this->shortcutUser); $shortcut_set_storage->unassignUser($this->shortcutUser); $current_set = shortcut_current_displayed_set($this->shortcutUser); $default_set = shortcut_default_set($this->shortcutUser); $this->assertTrue($current_set->id() == $default_set->id(), "Successfully unassigned another user's shortcut set."); }
/** * {@inheritdoc} */ public function build() { return array(shortcut_renderable_links(shortcut_current_displayed_set())); }
/** * Tests unassigning a shortcut set. */ function testShortcutSetUnassign() { $new_set = $this->generateShortcutSet($this->randomMachineName()); shortcut_set_assign_user($new_set, $this->shortcut_user); shortcut_set_unassign_user($this->shortcut_user); $current_set = shortcut_current_displayed_set($this->shortcut_user); $default_set = shortcut_default_set($this->shortcut_user); $this->assertTrue($current_set->id() == $default_set->id(), "Successfully unassigned another user's shortcut set."); }