View source
<?php
namespace Drupal\shortcut;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ShortcutSetStorage extends ConfigEntityStorage implements ShortcutSetStorageInterface {
protected $moduleHandler;
protected $connection;
public function __construct(EntityTypeInterface $entity_info, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, Connection $connection) {
parent::__construct($entity_info, $config_factory, $uuid_service, $language_manager, $memory_cache);
$this->moduleHandler = $module_handler;
$this->connection = $connection;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
return new static($entity_info, $container
->get('config.factory'), $container
->get('uuid'), $container
->get('module_handler'), $container
->get('language_manager'), $container
->get('entity.memory_cache'), $container
->get('database'));
}
public function deleteAssignedShortcutSets(ShortcutSetInterface $entity) {
$this->connection
->delete('shortcut_set_users')
->condition('set_name', $entity
->id())
->execute();
}
public function assignUser(ShortcutSetInterface $shortcut_set, $account) {
$this->connection
->merge('shortcut_set_users')
->key('uid', $account
->id())
->fields([
'set_name' => $shortcut_set
->id(),
])
->execute();
drupal_static_reset('shortcut_current_displayed_set');
}
public function unassignUser($account) {
$deleted = $this->connection
->delete('shortcut_set_users')
->condition('uid', $account
->id())
->execute();
return (bool) $deleted;
}
public function getAssignedToUser($account) {
$query = $this->connection
->select('shortcut_set_users', 'ssu');
$query
->fields('ssu', [
'set_name',
]);
$query
->condition('ssu.uid', $account
->id());
return $query
->execute()
->fetchField();
}
public function countAssignedUsers(ShortcutSetInterface $shortcut_set) {
return Database::getConnection()
->query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', [
':name' => $shortcut_set
->id(),
])
->fetchField();
}
public function getDefaultSet(AccountInterface $account) {
$suggestions = array_reverse($this->moduleHandler
->invokeAll('shortcut_default_set', [
$account,
]));
$suggestions[] = 'default';
$shortcut_set = NULL;
foreach ($suggestions as $name) {
if ($shortcut_set = $this
->load($name)) {
break;
}
}
return $shortcut_set;
}
}