function ContentEntityNormalizer::normalize

Same name in this branch
  1. 8.9.x core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php \Drupal\serialization\Normalizer\ContentEntityNormalizer::normalize()
Same name in other branches
  1. 9 core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php \Drupal\serialization\Normalizer\ContentEntityNormalizer::normalize()
  2. 9 core/modules/hal/src/Normalizer/ContentEntityNormalizer.php \Drupal\hal\Normalizer\ContentEntityNormalizer::normalize()
  3. 10 core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php \Drupal\serialization\Normalizer\ContentEntityNormalizer::normalize()
  4. 11.x core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php \Drupal\serialization\Normalizer\ContentEntityNormalizer::normalize()
1 call to ContentEntityNormalizer::normalize()
FileEntityNormalizer::normalize in core/modules/hal/src/Normalizer/FileEntityNormalizer.php
1 method overrides ContentEntityNormalizer::normalize()
FileEntityNormalizer::normalize in core/modules/hal/src/Normalizer/FileEntityNormalizer.php

File

core/modules/hal/src/Normalizer/ContentEntityNormalizer.php, line 84

Class

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

Namespace

Drupal\hal\Normalizer

Code

public function normalize($entity, $format = NULL, array $context = []) {
    $context += [
        'account' => NULL,
        'included_fields' => NULL,
    ];
    // Create the array of normalized fields, starting with the URI.
    
    /** @var $entity \Drupal\Core\Entity\ContentEntityInterface */
    $normalized = [
        '_links' => [
            'self' => [
                'href' => $this->getEntityUri($entity),
            ],
            'type' => [
                'href' => $this->linkManager
                    ->getTypeUri($entity->getEntityTypeId(), $entity->bundle(), $context),
            ],
        ],
    ];
    $field_items = TypedDataInternalPropertiesHelper::getNonInternalProperties($entity->getTypedData());
    // If the fields to use were specified, only output those field values.
    if (isset($context['included_fields'])) {
        $field_items = array_intersect_key($field_items, array_flip($context['included_fields']));
    }
    foreach ($field_items as $field) {
        // Continue if the current user does not have access to view this field.
        if (!$field->access('view', $context['account'])) {
            continue;
        }
        $normalized_property = $this->serializer
            ->normalize($field, $format, $context);
        $normalized = NestedArray::mergeDeep($normalized, $normalized_property);
    }
    return $normalized;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.