class EntityReferenceImageFieldSerializer in Acquia Content Hub 8.2
Subscribes to entity field serialization to handle entity references.
Hierarchy
- class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\EntityReferenceFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\EntityReferenceImageFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of EntityReferenceImageFieldSerializer
1 string reference to 'EntityReferenceImageFieldSerializer'
1 service uses EntityReferenceImageFieldSerializer
File
- src/
EventSubscriber/ SerializeContentField/ EntityReferenceImageFieldSerializer.php, line 12
Namespace
Drupal\acquia_contenthub\EventSubscriber\SerializeContentFieldView source
class EntityReferenceImageFieldSerializer extends EntityReferenceFieldSerializer implements EventSubscriberInterface {
/**
* Use the image field type.
*
* @var array
*/
protected $fieldTypes = [
'image',
];
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
'onSerializeContentField',
98,
];
return $events;
}
/**
* Extract entity uuids as field values.
*
* @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
* The content entity field serialization event.
*
* @throws \Exception
*/
public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
if (in_array($event
->getField()
->getFieldDefinition()
->getType(), $this->fieldTypes)) {
parent::onSerializeContentField($event);
$values = $event
->getFieldData();
if (!empty($values['value'])) {
$field_name = $event
->getField()
->getName();
$entity = $event
->getEntity();
foreach ($values['value'] as $lang => $language_values) {
$translation = $entity
->getTranslation($lang);
$field_translation = $translation
->get($field_name);
foreach ($language_values as $delta => $value) {
$values['value'][$lang][$delta] = [
'target_id' => $value,
];
$values['value'][$lang][$delta] += $field_translation[$delta]
->getValue();
}
}
}
$event
->setFieldData($values);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityReferenceImageFieldSerializer:: |
protected | property |
Use the image field type. Overrides EntityReferenceFieldSerializer:: |
|
EntityReferenceImageFieldSerializer:: |
public static | function |
Returns an array of event names this subscriber wants to listen to. Overrides EntityReferenceFieldSerializer:: |
|
EntityReferenceImageFieldSerializer:: |
public | function |
Extract entity uuids as field values. Overrides EntityReferenceFieldSerializer:: |