Pull merge.
[yaffs-website] / web / core / modules / shortcut / src / ShortcutSetStorage.php
1 <?php
2
3 namespace Drupal\shortcut;
4
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;
14
15 /**
16  * Defines a storage for shortcut_set entities.
17  */
18 class ShortcutSetStorage extends ConfigEntityStorage implements ShortcutSetStorageInterface {
19
20   /**
21    * The module handler.
22    *
23    * @var \Drupal\Core\Extension\ModuleHandlerInterface
24    */
25   protected $moduleHandler;
26
27   /**
28    * Constructs a ShortcutSetStorageController object.
29    *
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
35    *   The UUID service.
36    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
37    *   The module handler.
38    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
39    *   The language manager.
40    * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
41    *   The memory cache.
42    */
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);
45
46     $this->moduleHandler = $module_handler;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
53     return new static(
54       $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')
60     );
61   }
62
63   /**
64    * {@inheritdoc}
65    */
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())
71       ->execute();
72   }
73
74   /**
75    * {@inheritdoc}
76    */
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()])
81       ->execute();
82     drupal_static_reset('shortcut_current_displayed_set');
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function unassignUser($account) {
89     $deleted = db_delete('shortcut_set_users')
90       ->condition('uid', $account->id())
91       ->execute();
92     return (bool) $deleted;
93   }
94
95   /**
96    * {@inheritdoc}
97    */
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();
103   }
104
105   /**
106    * {@inheritdoc}
107    */
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();
110   }
111
112   /**
113    * {@inheritdoc}
114    */
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)) {
125         break;
126       }
127     }
128
129     return $shortcut_set;
130   }
131
132 }