View source
<?php
namespace Drupal\mongodb_shortcut;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\mongodb\MongoCollectionFactory;
use Drupal\shortcut\ShortcutSetInterface;
use Drupal\shortcut\ShortcutSetStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MongodbShortcutSetStorage extends ConfigEntityStorage implements ShortcutSetStorageInterface {
protected $moduleHandler;
protected $mongo;
public function __construct(EntityTypeInterface $entity_info, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, MongoCollectionFactory $mongo) {
parent::__construct($entity_info, $config_factory, $uuid_service, $language_manager);
$this->moduleHandler = $module_handler;
$this->mongo = $mongo;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
return new static($entity_info, $container
->get('config.factory'), $container
->get('uuid'), $container
->get('language_manager'), $container
->get('module_handler'), $container
->get('mongo'));
}
public function assignUser(ShortcutSetInterface $shortcut_set, $account) {
$newobj = [
'_id' => $account
->id(),
'set_name' => $shortcut_set
->id(),
];
$this->mongo
->get('shortcut_set_users')
->update([
'_id' => $account
->id(),
], $newobj, [
'upsert' => TRUE,
]);
drupal_static_reset('shortcut_current_displayed_set');
}
public function unassignUser($account) {
$this->mongo
->get('shortcut_set_users')
->remove([
'_id' => $account
->id(),
]);
}
public function deleteAssignedShortcutSets(ShortcutSetInterface $shortcut_set) {
$this->mongo
->get('shortcut_set_users')
->remove([
'set_name' => $shortcut_set
->id(),
]);
}
public function getAssignedToUser($account) {
$set = $this->mongo
->get('shortcut_set_users')
->findOne([
'_id' => $account
->id(),
], [
'set_name' => 1,
]);
return $set ? $set['set_name'] : FALSE;
}
public function countAssignedUsers(ShortcutSetInterface $shortcut_set) {
return $this->mongo
->get('shortcut_set_users')
->count([
'set_name' => $shortcut_set
->id(),
]);
}
public function getDefaultSet(AccountInterface $account) {
$suggestions = array_reverse($this->moduleHandler
->invokeAll('shortcut_default_set', array(
$account,
)));
$suggestions[] = 'default';
$shortcut_set = NULL;
foreach ($suggestions as $name) {
if ($shortcut_set = $this
->load($name)) {
break;
}
}
return $shortcut_set;
}
}