You are here

protected function HalEntityNormalizationTrait::applyHalFieldNormalization in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php \Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait::applyHalFieldNormalization()

Applies the HAL entity field normalization to an entity normalization.

The HAL normalization:

  • adds a 'lang' attribute to every translatable field
  • omits reference fields, since references are stored in _links & _embedded
  • omits empty fields (fields without value)

Parameters

array $normalization: An entity normalization.

Return value

array The updated entity normalization.

16 calls to HalEntityNormalizationTrait::applyHalFieldNormalization()
BlockContentHalJsonAnonTest::getExpectedNormalizedEntity in core/modules/block_content/tests/src/Functional/Hal/BlockContentHalJsonAnonTest.php
Returns the expected normalization of the entity.
CommentHalJsonTestBase::getExpectedNormalizedEntity in core/modules/comment/tests/src/Functional/Hal/CommentHalJsonTestBase.php
Returns the expected normalization of the entity.
EntityTestHalJsonAnonTest::getExpectedNormalizedEntity in core/modules/system/tests/modules/entity_test/tests/src/Functional/Hal/EntityTestHalJsonAnonTest.php
Returns the expected normalization of the entity.
EntityTestHalJsonInternalPropertyNormalizerTest::getExpectedNormalizedEntity in core/modules/system/tests/modules/entity_test/tests/src/Functional/Hal/EntityTestHalJsonInternalPropertyNormalizerTest.php
Returns the expected normalization of the entity.
EntityTestLabelHalJsonAnonTest::getExpectedNormalizedEntity in core/modules/system/tests/modules/entity_test/tests/src/Functional/Hal/EntityTestLabelHalJsonAnonTest.php
Returns the expected normalization of the entity.

... See full list

File

core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php, line 30

Class

HalEntityNormalizationTrait
Trait for EntityResourceTestBase subclasses testing formats using HAL.

Namespace

Drupal\Tests\hal\Functional\EntityResource

Code

protected function applyHalFieldNormalization(array $normalization) {
  if (!$this->entity instanceof FieldableEntityInterface) {
    throw new \LogicException('This trait should only be used for fieldable entity types.');
  }

  // In the HAL normalization, all translatable fields get a 'lang' attribute.
  $translatable_non_reference_fields = array_keys(array_filter($this->entity
    ->getTranslatableFields(), function (FieldItemListInterface $field) {
    return !$field instanceof EntityReferenceFieldItemListInterface;
  }));
  foreach ($translatable_non_reference_fields as $field_name) {
    if (isset($normalization[$field_name])) {
      $normalization[$field_name][0]['lang'] = 'en';
    }
  }

  // In the HAL normalization, reference fields are omitted, except for the
  // bundle field.
  $bundle_key = $this->entity
    ->getEntityType()
    ->getKey('bundle');
  $reference_fields = array_keys(array_filter($this->entity
    ->getFields(), function (FieldItemListInterface $field) use ($bundle_key) {
    return $field instanceof EntityReferenceFieldItemListInterface && $field
      ->getName() !== $bundle_key;
  }));
  foreach ($reference_fields as $field_name) {
    unset($normalization[$field_name]);
  }

  // In the HAL normalization, the bundle field  omits the 'target_type' and
  // 'target_uuid' properties, because it's encoded in the '_links' section.
  if ($bundle_key) {
    unset($normalization[$bundle_key][0]['target_type']);
    unset($normalization[$bundle_key][0]['target_uuid']);
  }

  // In the HAL normalization, empty fields are omitted.
  $empty_fields = array_keys(array_filter($this->entity
    ->getFields(), function (FieldItemListInterface $field) {
    return $field
      ->isEmpty();
  }));
  foreach ($empty_fields as $field_name) {
    unset($normalization[$field_name]);
  }
  return $normalization;
}