You are here

class AvatarKitEntityFieldPreferenceSubscriber in Avatar Kit 8.2

Subscriber for preference events.

Hierarchy

Expanded class hierarchy of AvatarKitEntityFieldPreferenceSubscriber

1 string reference to 'AvatarKitEntityFieldPreferenceSubscriber'
avatars.services.yml in ./avatars.services.yml
avatars.services.yml
1 service uses AvatarKitEntityFieldPreferenceSubscriber
avatars.service_preference.entity_field in ./avatars.services.yml
Drupal\avatars\EventSubscriber\AvatarKitEntityFieldPreferenceSubscriber

File

src/EventSubscriber/AvatarKitEntityFieldPreferenceSubscriber.php, line 14

Namespace

Drupal\avatars\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
AvatarKitEntityFieldPreferenceSubscriber::$entityFieldHandler private property The avatar entity field handler.
AvatarKitEntityFieldPreferenceSubscriber::avatarServiceFieldWeights public function Sorts avatar services by services as they appear in field configuration.
AvatarKitEntityFieldPreferenceSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AvatarKitEntityFieldPreferenceSubscriber::__construct public function Constructs a new AvatarKitEntityFieldPreferenceSubscriber.