You are here

class AvatarKitEntityField in Avatar Kit 8.2

Gets avatar from a field in the same entity.

Plugin annotation


@AvatarKitService(
  id = "avatars_entity_field",
  label = @Translation("Copy uploaded image"),
  description = @Translation("Copies an image uploaded to the entity."),
  files = TRUE,
)

Hierarchy

Expanded class hierarchy of AvatarKitEntityField

File

src/Plugin/Avatars/Service/AvatarKitEntityField.php, line 25

Namespace

Drupal\avatars\Plugin\Avatars\Service
View source
class AvatarKitEntityField extends AvatarKitServiceBase {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function getAvatar(AvatarIdentifierInterface $identifier) : ?string {
    if (!$identifier instanceof EntityAvatarIdentifierInterface) {

      // Identifier must have an entity.
      return NULL;
    }
    $entity = $identifier
      ->getEntity();
    $field = $this->configuration['field'];
    if ($field) {
      [
        $entity_type,
        $bundle,
        $fieldName,
      ] = explode('.', $field);
      if ($entity_type == $entity
        ->getEntityTypeId() && $bundle == $entity
        ->bundle()) {

        /** @var \Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field_list */
        $field_list = $entity->{$fieldName};

        /** @var \Drupal\file\Plugin\Field\FieldType\FileItem $item */
        $item = $field_list
          ->first();

        /** @var \Drupal\file\FileInterface $file */
        $file = $field_list->entity;
        if ($file) {
          $uri = $file
            ->getFileUri();
          return $uri;
        }
        return NULL;
      }
    }

    // Entity does not map to this plugin.
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() : array {
    $dependencies = [];
    $field = $this->configuration['field'] ?? NULL;
    if ($field) {
      $dependencies['config'][] = 'field.field.' . $field;
    }
    return $dependencies;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() : array {
    $configuration = parent::defaultConfiguration();
    $configuration['field'] = NULL;
    return $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) : array {
    $form = parent::buildConfigurationForm($form, $form_state);
    $avatar_field_types = [
      'file',
      'image',
    ];
    $fieldsOptions = [];
    foreach ($avatar_field_types as $fieldType) {
      $fieldMap = $this
        ->entityFieldManager()
        ->getFieldMapByFieldType($fieldType);
      unset($fieldMap['avatars_avatar_cache']);
      foreach ($fieldMap as $entity_type => $fields) {
        $entityTypeInfo = $this
          ->entityTypeManager()
          ->getDefinition($entity_type);
        $bundleInfo = $this
          ->entityTypeBundleInfo()
          ->getBundleInfo($entity_type);
        foreach ($fields as $fieldName => $fieldInfo) {
          [
            'bundles' => $bundles,
          ] = $fieldInfo;
          foreach ($bundles as $bundle) {
            $entity_type_label = $entityTypeInfo
              ->getLabel();
            $key = $entity_type . ':' . $bundle . ':' . $fieldName;
            $fieldsOptions[(string) $entity_type_label][$key] = $this
              ->t('@bundle: @field_name', [
              '@bundle' => $bundleInfo[$bundle]['label'],
              '@field_name' => $fieldName,
            ]);
          }
        }
      }
    }
    $form['help'] = [
      '#plain_text' => $this
        ->t('This service will run if the requested entity type and bundle is the same as specified by this field.'),
    ];
    $form['field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Field'),
      '#description' => $this
        ->t('Copy field values from this field.'),
      '#options' => $fieldsOptions,
      '#empty_option' => $this
        ->t('- Select -'),
      '#required' => TRUE,
    ];
    $field = $this->configuration['field'];
    if ($field) {
      [
        $entity_type,
        $bundle,
        $fieldName,
      ] = explode('.', $field);
      $form['field']['#default_value'] = $entity_type . ':' . $bundle . ':' . $fieldName;
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) : void {
    parent::submitConfigurationForm($form, $form_state);
    $field = $form_state
      ->getValue('field');
    [
      $entity_type,
      $bundle,
      $fieldName,
    ] = explode(':', $field);
    $field_config_id = $entity_type . '.' . $bundle . '.' . $fieldName;
    $fieldConfig = FieldConfig::load($field_config_id);
    if ($fieldConfig) {

      // @todo currently does not work with base fields.
      $this->configuration['field'] = $field_config_id;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function createIdentifier() : AvatarIdentifierInterface {
    return (new AvatarIdentifier())
      ->setHasher(function ($string) {
      return $string;
    });
  }

  /**
   * Get the entity field manager.
   *
   * @return \Drupal\Core\Entity\EntityFieldManagerInterface
   *   The entity field manager.
   */
  protected function entityFieldManager() : EntityFieldManagerInterface {
    return \Drupal::service('entity_field.manager');
  }

  /**
   * Get the entity type bundle info service.
   *
   * @return \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   *   The entity type bundle info service.
   */
  protected function entityTypeBundleInfo() : EntityTypeBundleInfoInterface {
    return \Drupal::service('entity_type.bundle.info');
  }

  /**
   * Get the entity type manager.
   *
   * @return \Drupal\Core\Entity\EntityTypeManagerInterface
   *   The entity type manager.
   */
  protected function entityTypeManager() : EntityTypeManagerInterface {
    return \Drupal::service('entity_type.manager');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvatarKitEntityField::buildConfigurationForm public function Form constructor. Overrides AvatarKitServiceBase::buildConfigurationForm
AvatarKitEntityField::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides AvatarKitServiceBase::calculateDependencies
AvatarKitEntityField::createIdentifier public function Overrides AvatarKitServiceBase::createIdentifier
AvatarKitEntityField::defaultConfiguration public function Gets default configuration for this plugin. Overrides AvatarKitServiceBase::defaultConfiguration
AvatarKitEntityField::entityFieldManager protected function Get the entity field manager.
AvatarKitEntityField::entityTypeBundleInfo protected function Get the entity type bundle info service.
AvatarKitEntityField::entityTypeManager protected function Get the entity type manager.
AvatarKitEntityField::getAvatar public function
AvatarKitEntityField::submitConfigurationForm public function Form submission handler. Overrides AvatarKitServiceBase::submitConfigurationForm
AvatarKitServiceBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
AvatarKitServiceBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
AvatarKitServiceBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.