class AvatarKitEntityFieldPreferenceSubscriber in Avatar Kit 8.2
Subscriber for preference events.
Hierarchy
- class \Drupal\avatars\EventSubscriber\AvatarKitEntityFieldPreferenceSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of AvatarKitEntityFieldPreferenceSubscriber
1 string reference to 'AvatarKitEntityFieldPreferenceSubscriber'
1 service uses AvatarKitEntityFieldPreferenceSubscriber
File
- src/
EventSubscriber/ AvatarKitEntityFieldPreferenceSubscriber.php, line 14
Namespace
Drupal\avatars\EventSubscriberView source
class AvatarKitEntityFieldPreferenceSubscriber implements EventSubscriberInterface {
/**
* The avatar entity field handler.
*
* @var \Drupal\avatars\AvatarKitEntityFieldHandlerInterface
*/
private $entityFieldHandler;
/**
* Constructs a new AvatarKitEntityFieldPreferenceSubscriber.
*
* @param \Drupal\avatars\AvatarKitEntityFieldHandlerInterface $entityFieldHandler
* The avatar entity field handler.
*/
public function __construct(AvatarKitEntityFieldHandlerInterface $entityFieldHandler) {
$this->entityFieldHandler = $entityFieldHandler;
}
/**
* Sorts avatar services by services as they appear in field configuration.
*
* @param \Drupal\avatars\Event\EntityServicePreferenceEvent $event
* Entity service preference event.
*/
public function avatarServiceFieldWeights(EntityServicePreferenceEvent $event) {
$entity = $event
->getEntity();
$field_config = $this->entityFieldHandler
->getAvatarFieldConfig($entity);
if ($field_config) {
$existingServices = $event
->getServices();
$existingServices = array_flip($existingServices);
$configServices = $field_config
->getThirdPartySetting('avatars', 'services', []);
// Existing services is considered the pool of allowed/enabled services.
// Filter these services down to what is also present in configuration,
// omitting any service ID's that no longer exist.
$service_ids = array_intersect($configServices, $existingServices);
// Reset the ID's, then flip.
$service_ids = array_flip(array_values($service_ids));
$event
->setServices($service_ids);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AvatarKitEvents::ENTITY_SERVICE_PREFERENCE][] = [
'avatarServiceFieldWeights',
-512,
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AvatarKitEntityFieldPreferenceSubscriber:: |
private | property | The avatar entity field handler. | |
AvatarKitEntityFieldPreferenceSubscriber:: |
public | function | Sorts avatar services by services as they appear in field configuration. | |
AvatarKitEntityFieldPreferenceSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AvatarKitEntityFieldPreferenceSubscriber:: |
public | function | Constructs a new AvatarKitEntityFieldPreferenceSubscriber. |