public function EntityLanguage::onUnserializeContentField in Acquia Content Hub 8.2
Handles language fields and the default_langcode field values.
This is actually a tricky bit of code that throws away language values for the "langcode" entity type key when present because Drupal will do its own determination of things like "default_langcode" if values are present in that field. Rather than allow this, we throw it away and handle defaults manually.
Parameters
\Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event: The unserialize event.
File
- src/
EventSubscriber/ UnserializeContentField/ EntityLanguage.php, line 35
Class
- EntityLanguage
- Language and default_language handling code.
Namespace
Drupal\acquia_contenthub\EventSubscriber\UnserializeContentFieldCode
public function onUnserializeContentField(UnserializeCdfEntityFieldEvent $event) {
$entityType = $event
->getEntityType();
if ($entityType
->hasKey('langcode')) {
$fieldName = $event
->getFieldName();
if ($event
->getFieldMetadata()['type'] === 'boolean' && $fieldName === 'default_langcode') {
$field = $event
->getField();
$values = [];
foreach ($field['value'] as $langcode => $value) {
$values[$langcode][$entityType
->getKey('langcode')] = $langcode;
}
$event
->setValue($values);
$event
->stopPropagation();
}
}
}