3 namespace Drupal\shortcut;
5 use Drupal\Component\Uuid\UuidInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Config\Entity\ConfigEntityStorage;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Defines a storage for shortcut_set entities.
17 class ShortcutSetStorage extends ConfigEntityStorage implements ShortcutSetStorageInterface {
22 * @var \Drupal\Core\Extension\ModuleHandlerInterface
24 protected $moduleHandler;
27 * Constructs a ShortcutSetStorageController object.
29 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info
30 * The entity info for the entity type.
31 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
32 * The config factory service.
33 * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
35 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
37 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
38 * The language manager.
40 public function __construct(EntityTypeInterface $entity_info, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager) {
41 parent::__construct($entity_info, $config_factory, $uuid_service, $language_manager);
43 $this->moduleHandler = $module_handler;
49 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
52 $container->get('config.factory'),
53 $container->get('uuid'),
54 $container->get('module_handler'),
55 $container->get('language_manager')
62 public function deleteAssignedShortcutSets(ShortcutSetInterface $entity) {
63 // First, delete any user assignments for this set, so that each of these
64 // users will go back to using whatever default set applies.
65 db_delete('shortcut_set_users')
66 ->condition('set_name', $entity->id())
73 public function assignUser(ShortcutSetInterface $shortcut_set, $account) {
74 db_merge('shortcut_set_users')
75 ->key('uid', $account->id())
76 ->fields(['set_name' => $shortcut_set->id()])
78 drupal_static_reset('shortcut_current_displayed_set');
84 public function unassignUser($account) {
85 $deleted = db_delete('shortcut_set_users')
86 ->condition('uid', $account->id())
88 return (bool) $deleted;
94 public function getAssignedToUser($account) {
95 $query = db_select('shortcut_set_users', 'ssu');
96 $query->fields('ssu', ['set_name']);
97 $query->condition('ssu.uid', $account->id());
98 return $query->execute()->fetchField();
104 public function countAssignedUsers(ShortcutSetInterface $shortcut_set) {
105 return db_query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', [':name' => $shortcut_set->id()])->fetchField();
111 public function getDefaultSet(AccountInterface $account) {
112 // Allow modules to return a default shortcut set name. Since we can only
113 // have one, we allow the last module which returns a valid result to take
114 // precedence. If no module returns a valid set, fall back on the site-wide
115 // default, which is the lowest-numbered shortcut set.
116 $suggestions = array_reverse($this->moduleHandler->invokeAll('shortcut_default_set', [$account]));
117 $suggestions[] = 'default';
118 $shortcut_set = NULL;
119 foreach ($suggestions as $name) {
120 if ($shortcut_set = $this->load($name)) {
125 return $shortcut_set;