class AvatarKitEntityPreferenceManager in Avatar Kit 8.2
Manages order in which services should be fetched for entities.
Hierarchy
- class \Drupal\avatars\AvatarKitEntityPreferenceManager implements AvatarKitEntityPreferenceManagerInterface
Expanded class hierarchy of AvatarKitEntityPreferenceManager
1 string reference to 'AvatarKitEntityPreferenceManager'
1 service uses AvatarKitEntityPreferenceManager
File
- src/
AvatarKitEntityPreferenceManager.php, line 15
Namespace
Drupal\avatarsView source
class AvatarKitEntityPreferenceManager implements AvatarKitEntityPreferenceManagerInterface {
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Storage for avatar service entities.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $serviceStorage;
/**
* The avatar service preference cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $preferenceCacheBackend;
/**
* Creates a new AvatarKitEntityPreferenceManager object.
*
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* The event dispatcher.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $preferenceCacheBackend
* The avatar service preference cache backend.
*/
public function __construct(EventDispatcherInterface $eventDispatcher, EntityTypeManagerInterface $entityTypeManager, CacheBackendInterface $preferenceCacheBackend) {
$this->eventDispatcher = $eventDispatcher;
$this->serviceStorage = $entityTypeManager
->getStorage('avatars_service');
$this->preferenceCacheBackend = $preferenceCacheBackend;
}
/**
* {@inheritdoc}
*/
public function getPreferences(EntityInterface $entity) : array {
$preference_cache_id = $entity
->getEntityTypeId() . ':' . $entity
->id();
$cache_item = $this->preferenceCacheBackend
->get($preference_cache_id);
if ($cache_item !== FALSE) {
return $cache_item->data;
}
$services = $this->serviceStorage
->loadMultiple();
$services_weights = array_flip(array_keys($services));
$event = (new EntityServicePreferenceEvent())
->setServices($services_weights)
->setEntity($entity);
$this->eventDispatcher
->dispatch(AvatarKitEvents::ENTITY_SERVICE_PREFERENCE, $event);
$services = $event
->getServices();
// Sort the array by weight (values), maintaining keys.
asort($services);
// Strip out the weights, leaving only service ID's in sorted order.
$service_ids = array_keys($services);
$this->preferenceCacheBackend
->set($preference_cache_id, $service_ids);
return $service_ids;
}
/**
* {@inheritdoc}
*/
public function invalidatePreferences(string $entityType, string $bundle) : void {
// There is currently no way to differentiate entity type / bundles. So
// reset everything.
$this->preferenceCacheBackend
->invalidateAll();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AvatarKitEntityPreferenceManager:: |
protected | property | The event dispatcher. | |
AvatarKitEntityPreferenceManager:: |
protected | property | The avatar service preference cache backend. | |
AvatarKitEntityPreferenceManager:: |
protected | property | Storage for avatar service entities. | |
AvatarKitEntityPreferenceManager:: |
public | function |
Get the avatar service preferences for an entity. Overrides AvatarKitEntityPreferenceManagerInterface:: |
|
AvatarKitEntityPreferenceManager:: |
public | function |
Reset preferences for a entity bundle. Overrides AvatarKitEntityPreferenceManagerInterface:: |
|
AvatarKitEntityPreferenceManager:: |
public | function | Creates a new AvatarKitEntityPreferenceManager object. |