protected function ContentEntityCdfNormalizer::addFieldsToDrupalEntity in Acquia Content Hub 8
Adds Content Hub Data to Drupal Entity Fields.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: The Drupal Entity.
\Acquia\ContentHubClient\Entity $contenthub_entity: The Content Hub Entity.
string $langcode: The language code.
array $context: Context.
Return value
\Drupal\Core\Entity\ContentEntityInterface The Drupal Entity after integrating data from Content Hub.
1 call to ContentEntityCdfNormalizer::addFieldsToDrupalEntity()
- ContentEntityCdfNormalizer::denormalize in src/
Normalizer/ ContentEntityCdfNormalizer.php - Denormalizes data back into an object of the given class.
File
- src/
Normalizer/ ContentEntityCdfNormalizer.php, line 875
Class
- ContentEntityCdfNormalizer
- Converts the Drupal entity object to a Acquia Content Hub CDF array.
Namespace
Drupal\acquia_contenthub\NormalizerCode
protected function addFieldsToDrupalEntity(ContentEntityInterface $entity, ContentHubEntity $contenthub_entity, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, array $context = []) {
/** @var \Drupal\Core\Field\FieldItemListInterface[] $fields */
$fields = $entity
->getFields();
// Ignore the entity ID and revision ID.
// Excluded comes here.
$excluded_fields = $this
->excludedProperties($entity);
// Add the bundle key to be ignored as it should have already been assigned.
$bundle_key = $this->entityTypeManager
->getDefinition($entity
->getEntityTypeId())
->getKey('bundle');
$excluded_fields[] = $bundle_key;
// We ignore `langcode` selectively because und i.e LANGCODE_NOT_SPECIFIED
// and zxx i.e LANGCODE_NOT_APPLICABLE content requires `langcode` field
// to *not* be excluded for such content to be importable.
if ($entity
->hasTranslation($langcode)) {
$excluded_fields[] = 'langcode';
}
// Iterate over all attributes.
foreach ($contenthub_entity
->getAttributes() as $name => $attribute) {
$attribute = (array) $attribute;
// If it is an excluded property, then skip it.
if (in_array($name, $excluded_fields)) {
continue;
}
// In the case of images/files, etc... we need to add the assets.
$file_types = [
'image',
'file',
'video',
];
$field = isset($fields[$name]) ? $fields[$name] : NULL;
if (isset($field)) {
// Try to map it to a known field type.
$field_type = $field
->getFieldDefinition()
->getType();
$settings = $field
->getFieldDefinition()
->getSettings();
$value = isset($attribute['value'][$langcode]) ? $attribute['value'][$langcode] : NULL;
$field
->setValue([]);
if ($value === NULL) {
continue;
}
$field
->setLangcode($langcode);
if ($field instanceof EntityReferenceFieldItemListInterface) {
$entity_type = $settings['target_type'];
$field_item = NULL;
foreach ($value as $item) {
$item = json_decode($item, TRUE) ?: $item;
if (in_array($field_type, $file_types)) {
if (is_array($item) && isset($item['target_uuid'])) {
$uuid = $this
->removeBracketsUuid($item['target_uuid']);
$referenced_entity = $this->entityRepository
->loadEntityByUuid($entity_type, $uuid);
}
else {
$uuid = $this
->removeBracketsUuid($item);
$referenced_entity = $this->entityRepository
->loadEntityByUuid($entity_type, $uuid);
}
$field_item = $referenced_entity ? [
'alt' => isset($item['alt']) ? $item['alt'] : ($settings['alt_field_required'] ? $referenced_entity
->label() : ''),
'title' => isset($item['title']) ? $item['title'] : ($settings['title_field_required'] ? $referenced_entity
->label() : ''),
'target_id' => $referenced_entity
->id(),
] : NULL;
}
else {
$uuid = $item;
$referenced_entity = $this->entityRepository
->loadEntityByUuid($entity_type, $uuid);
$field_item = $referenced_entity;
}
if ($field_item) {
$field
->appendItem($field_item);
}
}
}
else {
if ($field instanceof FieldItemListInterface && is_array($value)) {
foreach ($value as $item) {
// Only decode $item if it is a valid json string, otherwise just
// assign the value as it comes.
if (is_string($item) && isset($item[0]) && $item[0] === '{') {
$decoded = json_decode($item, TRUE);
if (json_last_error() === JSON_ERROR_NONE) {
$item = $decoded;
// Only do this if we are dealing with a link field type.
if ($link_field = ContentHubEntityLinkFieldHandler::load($field)
->validate()) {
$item = $link_field
->denormalizeItem($item);
}
}
}
$field
->appendItem($item);
}
}
else {
$field
->setValue($value);
}
}
}
}
return $entity;
}