You are here

public function ContentEntityNormalizer::normalize in Default Content for D8 2.0.x

Normalizes the entity into an array structure.

Parameters

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

Return value

array The normalized values, top level keys must include _meta with at least the entity_type and uuid keys, as well as the values for the default language in the default key and optionally translations.

Overrides ContentEntityNormalizerInterface::normalize

1 call to ContentEntityNormalizer::normalize()
ContentEntityNormalizer::getValueFromProperty in src/Normalizer/ContentEntityNormalizer.php
Returns the value for a given property.

File

src/Normalizer/ContentEntityNormalizer.php, line 92

Class

ContentEntityNormalizer
Normalizes and denormalizes content entities.

Namespace

Drupal\default_content\Normalizer

Code

public function normalize(ContentEntityInterface $entity) {

  // Define the generic metadata, define a version to allow to change the
  // format later.
  $normalized = [
    '_meta' => [
      'version' => '1.0',
      'entity_type' => $entity
        ->getEntityTypeId(),
      'uuid' => $entity
        ->uuid(),
    ],
  ];
  $entity_type = $entity
    ->getEntityType();
  if ($bundle_key = $entity_type
    ->getKey('bundle')) {
    $normalized['_meta']['bundle'] = $entity
      ->bundle();
  }
  if ($langcode_key = $entity_type
    ->getKey('langcode')) {
    $normalized['_meta']['default_langcode'] = $entity
      ->language()
      ->getId();
  }
  $is_root = FALSE;
  if ($this->dependencies === NULL) {
    $is_root = TRUE;
    $this->dependencies = [];
  }
  $field_names = $this
    ->getFieldsToNormalize($entity);

  // For menu links, add dependency information for the parent.
  if ($entity instanceof MenuLinkContentInterface) {
    if (strpos($entity
      ->getParentId(), PluginBase::DERIVATIVE_SEPARATOR) !== FALSE) {
      [
        $plugin_id,
        $parent_uuid,
      ] = explode(PluginBase::DERIVATIVE_SEPARATOR, $entity
        ->getParentId());
      if ($plugin_id === 'menu_link_content' && ($parent_entity = $this->entityRepository
        ->loadEntityByUuid('menu_link_content', $parent_uuid))) {
        $this
          ->addDependency($parent_entity);
      }
    }
  }
  foreach ($entity
    ->getTranslationLanguages() as $langcode => $language) {
    $translation = $entity
      ->getTranslation($langcode);
    $normalized_translation = $this
      ->normalizeTranslation($translation, $field_names);
    if ($translation
      ->isDefaultTranslation()) {
      $normalized['default'] = $normalized_translation;
    }
    else {
      $normalized['translations'][$langcode] = $normalized_translation;
    }
  }
  if ($is_root) {
    if ($this->dependencies) {
      $normalized['_meta']['depends'] = $this->dependencies;
    }
    $this->dependencies = NULL;
  }
  return $normalized;
}