You are here

public function AvatarKitEntityPreferenceManager::getPreferences in Avatar Kit 8.2

Get the avatar service preferences for an entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to get preferences for.

Return value

string[] An array of avatar service plugin ID's sorted in order of priority. Keys have no importance.

Overrides AvatarKitEntityPreferenceManagerInterface::getPreferences

File

src/AvatarKitEntityPreferenceManager.php, line 57

Class

AvatarKitEntityPreferenceManager
Manages order in which services should be fetched for entities.

Namespace

Drupal\avatars

Code

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