You are here

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

Returns the value for a given property.

Parameters

\Drupal\Core\TypedData\TypedDataInterface $property: The property to be normalized.

\Drupal\Core\Field\FieldItemInterface $field_item: The field item parent of the property.

array|null $normalized_item: The normalized values of the field item, can be used to set a value other than the current property.

Return value

mixed|null The normalized value, a scalar, array or NULL to skip this property.

1 call to ContentEntityNormalizer::getValueFromProperty()
ContentEntityNormalizer::normalizeTranslation in src/Normalizer/ContentEntityNormalizer.php
Normalizes an entity (translation).

File

src/Normalizer/ContentEntityNormalizer.php, line 383

Class

ContentEntityNormalizer
Normalizes and denormalizes content entities.

Namespace

Drupal\default_content\Normalizer

Code

protected function getValueFromProperty(TypedDataInterface $property, FieldItemInterface $field_item, &$normalized_item = NULL) {
  $value = NULL;

  // @todo Is there case where it is not the entity property?
  if ($property
    ->getDataDefinition() instanceof DataReferenceTargetDefinition && $field_item->entity instanceof ContentEntityInterface) {

    // Ignore broken references.
    if (!$field_item->entity) {
      return NULL;
    }

    // Ignore data reference target properties for content entities,
    // except user 0 and 1, which can be referenced by ID unlike
    // their UUIDs, which are expected to changed.
    if (!$field_item->entity instanceof UserInterface || !in_array($field_item->entity
      ->id(), [
      0,
      1,
    ])) {
      return NULL;
    }
    $value = $property
      ->getCastedValue();
  }
  elseif ($property instanceof EntityReference && $property
    ->getValue() instanceof ContentEntityInterface) {

    /** @var \Drupal\Core\Entity\ContentEntityInterface $target */
    $target = $property
      ->getValue();

    // Ignore user 0 and 1, they are stored with their ID.
    if ($field_item->entity instanceof UserInterface && in_array($field_item->entity
      ->id(), [
      0,
      1,
    ])) {
      return NULL;
    }

    // Regular entity references are referenced by UUID, entity
    // types like paragraphs that are child entities are embedded
    // directly.
    if ($field_item
      ->getFieldDefinition()
      ->getType() == 'entity_reference_revisions' && $target
      ->getEntityType()
      ->get('entity_revision_parent_type_field')) {
      $value = $this
        ->normalize($target);
    }
    else {
      $this
        ->addDependency($target);
      $value = $target
        ->uuid();
    }
  }
  elseif ($property instanceof Uri) {
    $value = $property
      ->getValue();
    $scheme = parse_url($value, PHP_URL_SCHEME);
    if ($scheme === 'entity') {

      // Normalize entity URI's as UUID, do not set the URI property.
      $path = parse_url($value, PHP_URL_PATH);
      [
        $target_entity_type_id,
        $target_id,
      ] = explode('/', $path);
      $target = $this->entityTypeManager
        ->getStorage($target_entity_type_id)
        ->load($target_id);
      $this
        ->addDependency($target);
      $normalized_item['target_uuid'] = $target
        ->uuid();
      $value = NULL;
    }
  }
  elseif ($property
    ->getName() == 'pid' && $field_item instanceof PathItem) {

    // Ignore the pid attribute of path fields so that they are
    // correctly-created.
    return NULL;
  }
  elseif ($property instanceof PathautoState && $property
    ->getValue() !== NULL) {

    // Explicitly include the pathauto state.
    $value = (int) $property
      ->getValue();
  }
  elseif ($property instanceof PrimitiveInterface) {
    $value = $property
      ->getCastedValue();
  }
  elseif (!$property
    ->getDataDefinition()
    ->isComputed()) {
    $value = $property
      ->getValue();
  }
  return $value;
}