You are here

public function AvatarKitEntityFieldPreferenceSubscriber::avatarServiceFieldWeights in Avatar Kit 8.2

Sorts avatar services by services as they appear in field configuration.

Parameters

\Drupal\avatars\Event\EntityServicePreferenceEvent $event: Entity service preference event.

File

src/EventSubscriber/AvatarKitEntityFieldPreferenceSubscriber.php, line 39

Class

AvatarKitEntityFieldPreferenceSubscriber
Subscriber for preference events.

Namespace

Drupal\avatars\EventSubscriber

Code

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);
  }
}