LinkFieldSerializer.php in Acquia Content Hub 8.2
File
src/EventSubscriber/SerializeContentField/LinkFieldSerializer.php
View source
<?php
namespace Drupal\acquia_contenthub\EventSubscriber\SerializeContentField;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent;
use Drupal\Core\Entity\ContentEntityInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LinkFieldSerializer implements EventSubscriberInterface {
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
'onSerializeContentField',
15,
];
return $events;
}
public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
if ($event
->getField()
->getFieldDefinition()
->getType() !== 'link') {
return;
}
$entity = $event
->getEntity();
if (!$entity instanceof ContentEntityInterface) {
return;
}
$field_translations = $this
->getFieldTranslations($entity, $event);
if (!$field_translations) {
return;
}
$cdf = $event
->getCdf();
$metadata = $cdf
->getMetadata();
$data = [];
foreach ($field_translations as $field) {
$langcode = $field
->getLangcode();
$metadata['field'][$event
->getFieldName()] = [
'type' => $event
->getField()
->getFieldDefinition()
->getType(),
];
if (empty(count($field))) {
$data['value'][$langcode][] = NULL;
continue;
}
foreach ($field as $item) {
$values = $item
->getValue();
if (empty($values['uri'])) {
continue;
}
list($uri_type, $uri_reference) = explode(':', $values['uri'], 2);
$values['uri_type'] = $item
->isExternal() ? 'external' : $uri_type;
if ($uri_type === 'entity') {
list($entity_type, $entity_id) = explode('/', $uri_reference, 2);
$uri_entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($entity_id);
if (is_null($uri_entity)) {
continue;
}
$values['uri'] = $uri_entity
->uuid();
}
elseif ($uri_type === 'internal') {
$url = $item
->getUrl();
if ($url && $url
->isRouted()) {
$route_params = $url
->getRouteParameters();
if (!empty($route_params)) {
try {
$uri_storage = \Drupal::entityTypeManager()
->getStorage(key($route_params));
} catch (\Exception $e) {
$uri_storage = NULL;
}
$uri_entity = !is_null($uri_storage) ? $uri_storage
->load(current($route_params)) : NULL;
if (is_null($uri_entity)) {
continue;
}
$internal_path = $url
->getInternalPath();
if ($internal_path === ltrim($uri_reference, '/')) {
$values['internal_type'] = 'internal_entity';
$values['uri'] = $uri_entity
->uuid();
}
}
}
}
$data['value'][$langcode][] = $values;
}
}
$event
->setFieldData($data);
$cdf
->setMetadata($metadata);
$event
->stopPropagation();
}
protected function getFieldTranslations(ContentEntityInterface $entity, SerializeCdfEntityFieldEvent $event) {
$fields = [];
$languages = $entity
->getTranslationLanguages();
$field = $event
->getField();
if ($field
->getFieldDefinition()
->isTranslatable()) {
foreach ($languages as $language) {
$translated = $entity
->getTranslation($language
->getId());
$fields[] = $translated
->get($event
->getFieldName());
}
}
else {
$fields[] = $field;
}
return $fields;
}
}