class EntityReferenceImageField in Acquia Content Hub 8.2
Entity/image/file field reference handling.
Hierarchy
- class \Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField\EntityReferenceImageField implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses FieldEntityDependencyTrait
Expanded class hierarchy of EntityReferenceImageField
1 string reference to 'EntityReferenceImageField'
1 service uses EntityReferenceImageField
File
- src/
EventSubscriber/ UnserializeContentField/ EntityReferenceImageField.php, line 12
Namespace
Drupal\acquia_contenthub\EventSubscriber\UnserializeContentFieldView source
class EntityReferenceImageField implements EventSubscriberInterface {
use FieldEntityDependencyTrait;
/**
* Image field type declaration.
*
* @var array
* Array of field types,
*/
protected $fieldTypes = [
'image',
];
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::UNSERIALIZE_CONTENT_ENTITY_FIELD] = [
'onUnserializeContentField',
8,
];
return $events;
}
/**
* Extracts the target storage and retrieves the referenced entity.
*
* @param \Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event
* The unserialize event.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function onUnserializeContentField(UnserializeCdfEntityFieldEvent $event) {
$field = $event
->getField();
if (!in_array($event
->getFieldMetadata()['type'], $this->fieldTypes)) {
return;
}
$values = [];
if (!empty($field['value'])) {
foreach ($field['value'] as $langcode => $value) {
if (empty($value)) {
continue;
}
if (!is_array(reset($value))) {
$entity = $this
->getEntity($value['target_id'], $event);
$value['target_id'] = $entity
->id();
$values[$langcode][$event
->getFieldName()] = $value;
}
else {
foreach ($value as $delta => $item) {
$entity = $this
->getEntity($item['target_id'], $event);
$item['target_id'] = $entity
->id();
$values[$langcode][$event
->getFieldName()][] = $item;
}
}
}
}
$event
->setValue($values);
$event
->stopPropagation();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityReferenceImageField:: |
protected | property | Image field type declaration. | |
EntityReferenceImageField:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
EntityReferenceImageField:: |
public | function | Extracts the target storage and retrieves the referenced entity. | |
FieldEntityDependencyTrait:: |
protected | function | Get an entity from the dependency stack. |