You are here

class FileEntityNormalizer in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/hal/src/Normalizer/FileEntityNormalizer.php \Drupal\hal\Normalizer\FileEntityNormalizer

Converts the Drupal entity object structure to a HAL array structure.

@internal

Hierarchy

Expanded class hierarchy of FileEntityNormalizer

1 string reference to 'FileEntityNormalizer'
hal.services.yml in core/modules/hal/hal.services.yml
core/modules/hal/hal.services.yml
1 service uses FileEntityNormalizer
serializer.normalizer.file_entity.hal in core/modules/hal/hal.services.yml
Drupal\hal\Normalizer\FileEntityNormalizer

File

core/modules/hal/src/Normalizer/FileEntityNormalizer.php, line 19

Namespace

Drupal\hal\Normalizer
View source
class FileEntityNormalizer extends ContentEntityNormalizer {

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = FileInterface::class;

  /**
   * The HTTP client.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * The HAL settings config.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $halSettings;

  /**
   * Constructs a FileEntityNormalizer object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
   *   The hypermedia link manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository
   *   The entity type repository.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, EntityTypeRepositoryInterface $entity_type_repository = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) {
    parent::__construct($link_manager, $entity_type_manager, $module_handler, $entity_type_repository, $entity_field_manager);
    $this->halSettings = $config_factory
      ->get('hal.settings');
  }

  /**
   * {@inheritdoc}
   *
   * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0.
   */
  public function normalize($entity, $format = NULL, array $context = []) {
    $data = parent::normalize($entity, $format, $context);
    $this
      ->addCacheableDependency($context, $this->halSettings);
    if ($this->halSettings
      ->get('bc_file_uri_as_url_normalizer')) {

      // Replace the file url with a full url for the file.
      $data['uri'][0]['value'] = $this
        ->getEntityUri($entity);
      @trigger_error("Replacing the file uri with the URL is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use the provided url property instead and disable hal.settings:bc_file_uri_as_url_normalizer. See https://www.drupal.org/node/2925783", E_USER_DEPRECATED);
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntityUri(EntityInterface $entity, array $context = []) {
    assert($entity instanceof FileInterface);

    // https://www.drupal.org/project/drupal/issues/2277705 introduced a hack
    // in \Drupal\file\Entity\File::url(), but EntityInterface::url() was
    // deprecated in favor of ::toUrl(). The parent implementation now calls
    // ::toUrl(), but this normalizer (for File entities) needs to override that
    // back to the old behavior because it relies on said hack, not just to
    // generate the value for the 'uri' field of a file (see ::normalize()), but
    // also for the HAL normalization's '_links' value.
    return $entity
      ->createFileUrl(FALSE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
ContentEntityNormalizer::$deprecatedProperties protected property
ContentEntityNormalizer::$linkManager protected property The hypermedia link manager.
ContentEntityNormalizer::$moduleHandler protected property The module handler.
ContentEntityNormalizer::denormalize public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize().
ContentEntityNormalizer::getTypedDataIds protected function Gets the typed data IDs for a type URI.
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
FieldableEntityNormalizerTrait::$entityFieldManager protected property The entity field manager.
FieldableEntityNormalizerTrait::$entityTypeManager protected property The entity type manager. 1
FieldableEntityNormalizerTrait::$entityTypeRepository protected property The entity type repository.
FieldableEntityNormalizerTrait::constructValue protected function Build the field item value using the incoming data. 7
FieldableEntityNormalizerTrait::denormalizeFieldData protected function Denormalizes entity data by denormalizing each field individually.
FieldableEntityNormalizerTrait::determineEntityTypeId protected function Determines the entity type ID to denormalize as.
FieldableEntityNormalizerTrait::extractBundleData protected function Denormalizes the bundle property so entity creation can use it.
FieldableEntityNormalizerTrait::getEntityFieldManager protected function Returns the entity field manager.
FieldableEntityNormalizerTrait::getEntityTypeDefinition protected function Gets the entity type definition.
FieldableEntityNormalizerTrait::getEntityTypeManager protected function Returns the entity type manager.
FieldableEntityNormalizerTrait::getEntityTypeRepository protected function Returns the entity type repository.
FileEntityNormalizer::$halSettings protected property The HAL settings config.
FileEntityNormalizer::$httpClient protected property The HTTP client.
FileEntityNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides ContentEntityNormalizer::$supportedInterfaceOrClass
FileEntityNormalizer::getEntityUri protected function Constructs the entity URI. Overrides ContentEntityNormalizer::getEntityUri
FileEntityNormalizer::normalize Deprecated public function Overrides ContentEntityNormalizer::normalize
FileEntityNormalizer::__construct public function Constructs a FileEntityNormalizer object. Overrides ContentEntityNormalizer::__construct
NormalizerBase::$format protected property List of formats which supports (de-)normalization. Overrides NormalizerBase::$format
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. Overrides NormalizerBase::checkFormat
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1