You are here

class AvatarKitEntityFieldHandler in Avatar Kit 8.2

Handles pushing avatar caches into entities.

Hierarchy

Expanded class hierarchy of AvatarKitEntityFieldHandler

1 string reference to 'AvatarKitEntityFieldHandler'
avatars.services.yml in ./avatars.services.yml
avatars.services.yml
1 service uses AvatarKitEntityFieldHandler
avatars.entity.field_handler in ./avatars.services.yml
Drupal\avatars\AvatarKitEntityFieldHandler

File

src/AvatarKitEntityFieldHandler.php, line 16

Namespace

Drupal\avatars
View source
class AvatarKitEntityFieldHandler implements AvatarKitEntityFieldHandlerInterface {

  /**
   * The avatar entity handler.
   *
   * @var \Drupal\avatars\AvatarKitEntityHandlerInterface
   */
  protected $entityHandler;

  /**
   * Constructs a new AvatarKitEntityFieldHandler instance.
   *
   * @param \Drupal\avatars\AvatarKitEntityHandlerInterface $entityHandler
   *   The avatar entity handler.
   */
  public function __construct(AvatarKitEntityHandlerInterface $entityHandler) {
    $this->entityHandler = $entityHandler;
  }

  /**
   * {@inheritdoc}
   */
  public function copyCacheToEntity(FieldableEntityInterface $entity, AvatarCacheInterface $avatar_cache) : void {
    $field_name = $this
      ->getAvatarFieldName($entity);
    if (!$field_name) {
      throw new \Exception('Entity does not accept avatars.');
    }
    $file = $avatar_cache
      ->getAvatar();
    $this
      ->pushFileIntoEntity($entity, $field_name, $file);
  }

  /**
   * {@inheritdoc}
   */
  public function checkUpdates(FieldableEntityInterface $entity) : void {

    // Don't try to find the first avatar when it doesnt have a target field.
    $field_name = $this
      ->getAvatarFieldName($entity);
    if (!$field_name) {
      return;
    }
    $first = $this->entityHandler
      ->findFirst($entity);
    if ($first) {
      $this
        ->copyCacheToEntity($entity, $first);
    }
  }

  /**
   * Places a reference to a file in the field of a given entity.
   *
   * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
   *   The destination entity.
   * @param string $field_name
   *   Name of a file field.
   * @param \Drupal\file\FileInterface $file
   *   The new file.
   */
  protected function pushFileIntoEntity(FieldableEntityInterface $entity, string $field_name, FileInterface $file) {

    /** @var \Drupal\Core\Field\EntityReferenceFieldItemList $field_item_list */
    $field_item_list = $entity
      ->get($field_name);

    // Determine if file is already in the entity.
    $files = $field_item_list
      ->referencedEntities();
    $current_file = reset($files);
    if ($current_file instanceof FileInterface && $current_file
      ->id() == $file
      ->id()) {
      return;
    }

    // Replace existing values, if any, with new file.
    $field_item_list
      ->setValue([
      $file,
    ]);
    $entity
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function getAvatarFieldName(EntityInterface $entity) : ?string {
    $entity_type = $entity
      ->getEntityTypeId();
    $bundle = $entity
      ->bundle();
    $entity_map = AvatarKitEntityMap::load($entity_type . '.' . $bundle . '.' . 'default');
    return $entity_map ? $entity_map
      ->getFieldName() : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvatarFieldConfig(EntityInterface $entity) : ?FieldConfigInterface {
    $field_name = $this
      ->getAvatarFieldName($entity);
    if (!$field_name) {
      return NULL;
    }
    $field_config_id = $entity
      ->getEntityTypeId() . '.' . $entity
      ->bundle() . '.' . $field_name;
    $field_config = FieldConfig::load($field_config_id);
    return $field_config ? $field_config : NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvatarKitEntityFieldHandler::$entityHandler protected property The avatar entity handler.
AvatarKitEntityFieldHandler::checkUpdates public function Checks if there are updates to the first avatar for an entity. Overrides AvatarKitEntityFieldHandlerInterface::checkUpdates
AvatarKitEntityFieldHandler::copyCacheToEntity public function Copies the avatar in a cache entity to an entity. Overrides AvatarKitEntityFieldHandlerInterface::copyCacheToEntity
AvatarKitEntityFieldHandler::getAvatarFieldConfig public function Get target field configuration for an entity. Overrides AvatarKitEntityFieldHandlerInterface::getAvatarFieldConfig
AvatarKitEntityFieldHandler::getAvatarFieldName public function Get field name for avatars. Overrides AvatarKitEntityFieldHandlerInterface::getAvatarFieldName
AvatarKitEntityFieldHandler::pushFileIntoEntity protected function Places a reference to a file in the field of a given entity.
AvatarKitEntityFieldHandler::__construct public function Constructs a new AvatarKitEntityFieldHandler instance.