function Importer::setFieldValues

Same name in other branches
  1. 11.x core/lib/Drupal/Core/DefaultContent/Importer.php \Drupal\Core\DefaultContent\Importer::setFieldValues()

Sets field values based on the normalized data.

Parameters

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

string $field_name: The name of the field.

array $values: The normalized data for the field.

1 call to Importer::setFieldValues()
Importer::toEntity in core/lib/Drupal/Core/DefaultContent/Importer.php
Converts an array of content entity data to a content entity object.

File

core/lib/Drupal/Core/DefaultContent/Importer.php, line 247

Class

Importer
A service for handling import of content.

Namespace

Drupal\Core\DefaultContent

Code

private function setFieldValues(ContentEntityInterface $entity, string $field_name, array $values) : void {
    foreach ($values as $delta => $item_value) {
        if (!$entity->get($field_name)
            ->get($delta)) {
            $entity->get($field_name)
                ->appendItem();
        }
        
        /** @var \Drupal\Core\Field\FieldItemInterface $item */
        $item = $entity->get($field_name)
            ->get($delta);
        // Update the URI based on the target UUID for link fields.
        if (isset($item_value['target_uuid']) && $item instanceof LinkItem) {
            $target_entity = $this->loadEntityDependency($item_value['target_uuid']);
            if ($target_entity) {
                $item_value['uri'] = 'entity:' . $target_entity->getEntityTypeId() . '/' . $target_entity->id();
            }
            unset($item_value['target_uuid']);
        }
        $serialized_property_names = $this->getCustomSerializedPropertyNames($item);
        foreach ($item_value as $property_name => $value) {
            if (\in_array($property_name, $serialized_property_names)) {
                if (\is_string($value)) {
                    throw new ImportException("Received string for serialized property {$field_name}.{$delta}.{$property_name}");
                }
                $value = serialize($value);
            }
            $property = $item->get($property_name);
            if ($property instanceof EntityReference) {
                if (is_array($value)) {
                    $value = $this->toEntity($value);
                }
                else {
                    $value = $this->loadEntityDependency($value);
                }
            }
            $property->setValue($value);
        }
    }
}

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