You are here

protected function ContentEntityNormalizer::getFieldsToNormalize in Default Content for D8 2.0.x

Returns a list of fields to be normalized.

Ignores identifiers, fields that are already defined in the metadata, fields that are known to be overwritten like revision creation time and media thumbnail.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The content entity.

Return value

string[] TThe list of fields to normalize.

1 call to ContentEntityNormalizer::getFieldsToNormalize()
ContentEntityNormalizer::normalize in src/Normalizer/ContentEntityNormalizer.php
Normalizes the entity into an array structure.

File

src/Normalizer/ContentEntityNormalizer.php, line 276

Class

ContentEntityNormalizer
Normalizes and denormalizes content entities.

Namespace

Drupal\default_content\Normalizer

Code

protected function getFieldsToNormalize(ContentEntityInterface $entity) : array {
  $fields = TypedDataInternalPropertiesHelper::getNonInternalProperties($entity
    ->getTypedData());

  // Unset identifiers.

  /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
  $entity_type = $entity
    ->getEntityType();
  unset($fields[$entity_type
    ->getKey('id')]);
  unset($fields[$entity_type
    ->getKey('uuid')]);
  if ($revision_key = $entity_type
    ->getKey('revision')) {
    unset($fields[$revision_key]);
  }

  // Unset the bundle ang language code.
  if ($bundle_key = $entity_type
    ->getKey('bundle')) {
    unset($fields[$bundle_key]);
  }
  if ($langcode_key = $entity_type
    ->getKey('langcode')) {
    unset($fields[$langcode_key]);
    unset($fields[$entity_type
      ->getKey('default_langcode')]);
  }

  // Ignore the revision created timestamp, it is set on save.
  if ($revision_created_key = $entity_type
    ->getRevisionMetadataKey('revision_created')) {
    unset($fields[$revision_created_key]);
  }

  // Ignore the media thumbnail field, it is force regenerated for new
  // media entities. See \Drupal\media\Entity\Media::shouldUpdateThumbnail().
  if ($entity_type
    ->id() == 'media') {
    unset($fields['thumbnail']);
  }

  // Ignore parent reference fields of composite entities.
  $parent_reference_keys = [
    'entity_revision_parent_type_field',
    'entity_revision_parent_id_field',
    'entity_revision_parent_field_name_field',
  ];
  foreach ($parent_reference_keys as $parent_reference_key) {
    if ($key_field_name = $entity_type
      ->get($parent_reference_key)) {
      unset($fields[$key_field_name]);
    }
  }
  return array_keys($fields);
}