J'ai un objet nœud avec un champ d'image nommé field_image. Quand je fais
$node
->get('field_image')
->first()
->getValue()
Je reçois un tableau avec des propriétés au lieu d'objet entité. Le tableau ressemble à ceci:
'target_id' => string '8' (length=1)
'alt' => string '' (length=0)
'title' => string '' (length=0)
'width' => string '587' (length=3)
'height' => string '458' (length=3)
'_loaded' => boolean true
'_accessCacheability' =>
object(Drupal\Core\Cache\CacheableMetadata)[1092]
protected 'cacheContexts' =>
array (size=0)
...
protected 'cacheTags' =>
array (size=0)
...
protected 'cacheMaxAge' => int -1
Dois-je toujours charger l'entité de fichier en utilisant target_idou existe-t-il un moyen d'obtenir automatiquement un objet d'entité référencé à partir d'une instance de Drupal\image\Plugin\Field\FieldType\ImageItem?
Pourquoi l'entité référencée n'est pas accessible via getter? Il pourrait être chargé paresseux s'il n'est pas disponible immédiatement.
PHP Fatal error: Call to undefined method Drupal\image\Plugin\Field\FieldType\ImageItem::getTarget(). Cependant $node->get('imagefield')->referencedEntities()fonctionne grâce au fait $node->get('imagefield')retourne instance de EntityReferenceFieldItemListqui implémente la referencedEntities()méthode. Votre solution n’est donc que partielle: elle fonctionne pour les listes mais pas pour les champs uniques.