3 namespace Drupal\shortcut;
5 use Drupal\Component\Uuid\UuidInterface;
6 use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Config\Entity\ConfigEntityStorage;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Extension\ModuleHandlerInterface;
11 use Drupal\Core\Language\LanguageManagerInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Defines a storage for shortcut_set entities.
18 class ShortcutSetStorage extends ConfigEntityStorage implements ShortcutSetStorageInterface {
23 * @var \Drupal\Core\Extension\ModuleHandlerInterface
25 protected $moduleHandler;
28 * Constructs a ShortcutSetStorageController object.
30 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info
31 * The entity info for the entity type.
32 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
33 * The config factory service.
34 * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
36 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
38 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
39 * The language manager.
40 * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
43 public function __construct(EntityTypeInterface $entity_info, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache) {
44 parent::__construct($entity_info, $config_factory, $uuid_service, $language_manager, $memory_cache);
46 $this->moduleHandler = $module_handler;
52 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
55 $container->get('config.factory'),
56 $container->get('uuid'),
57 $container->get('module_handler'),
58 $container->get('language_manager'),
59 $container->get('entity.memory_cache')
66 public function deleteAssignedShortcutSets(ShortcutSetInterface $entity) {
67 // First, delete any user assignments for this set, so that each of these
68 // users will go back to using whatever default set applies.
69 db_delete('shortcut_set_users')
70 ->condition('set_name', $entity->id())
77 public function assignUser(ShortcutSetInterface $shortcut_set, $account) {
78 db_merge('shortcut_set_users')
79 ->key('uid', $account->id())
80 ->fields(['set_name' => $shortcut_set->id()])
82 drupal_static_reset('shortcut_current_displayed_set');
88 public function unassignUser($account) {
89 $deleted = db_delete('shortcut_set_users')
90 ->condition('uid', $account->id())
92 return (bool) $deleted;
98 public function getAssignedToUser($account) {
99 $query = db_select('shortcut_set_users', 'ssu');
100 $query->fields('ssu', ['set_name']);
101 $query->condition('ssu.uid', $account->id());
102 return $query->execute()->fetchField();
108 public function countAssignedUsers(ShortcutSetInterface $shortcut_set) {
109 return db_query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', [':name' => $shortcut_set->id()])->fetchField();
115 public function getDefaultSet(AccountInterface $account) {
116 // Allow modules to return a default shortcut set name. Since we can only
117 // have one, we allow the last module which returns a valid result to take
118 // precedence. If no module returns a valid set, fall back on the site-wide
119 // default, which is the lowest-numbered shortcut set.
120 $suggestions = array_reverse($this->moduleHandler->invokeAll('shortcut_default_set', [$account]));
121 $suggestions[] = 'default';
122 $shortcut_set = NULL;
123 foreach ($suggestions as $name) {
124 if ($shortcut_set = $this->load($name)) {
129 return $shortcut_set;