protected function ContentEntityNormalizer::setFieldValues in Default Content for D8 2.0.x
Set 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 ContentEntityNormalizer::setFieldValues()
- ContentEntityNormalizer::denormalize in src/
Normalizer/ ContentEntityNormalizer.php - Converts the normalized data back into a content entity.
File
- src/
Normalizer/ ContentEntityNormalizer.php, line 215
Class
- ContentEntityNormalizer
- Normalizes and denormalizes content entities.
Namespace
Drupal\default_content\NormalizerCode
protected function setFieldValues(ContentEntityInterface $entity, string $field_name, array $values) {
if (!$entity
->hasField($field_name)) {
return;
}
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']) && isset($item
->getProperties()['uri'])) {
$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 \LogicException("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)) {
$target_entity = $this
->denormalize($value);
}
else {
$target_entity = $this
->loadEntityDependency($value);
}
$property
->setValue($target_entity);
}
else {
$property
->setValue($value);
}
}
}
}