Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

ContentEntityNormalizer.php

Namespace

Drupal\serialization\Normalizer

File

core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php

View source
<?php

namespace Drupal\serialization\Normalizer;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper;

/**
 * Normalizes/denormalizes Drupal content entities into an array structure.
 */
class ContentEntityNormalizer extends EntityNormalizer {
    
    /**
     * {@inheritdoc}
     */
    public function normalize($entity, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
        $context += [
            'account' => NULL,
        ];
        $attributes = [];
        
        /** @var \Drupal\Core\Entity\Entity $entity */
        foreach (TypedDataInternalPropertiesHelper::getNonInternalProperties($entity->getTypedData()) as $name => $field_items) {
            if ($field_items->access('view', $context['account'])) {
                $attributes[$name] = $this->serializer
                    ->normalize($field_items, $format, $context);
            }
        }
        return $attributes;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSupportedTypes(?string $format) : array {
        return [
            ContentEntityInterface::class => TRUE,
        ];
    }

}

Classes

Title Deprecated Summary
ContentEntityNormalizer Normalizes/denormalizes Drupal content entities into an array structure.
RSS feed
Powered by Drupal