I really would like to make this if/else field value check just a little bit more sexy.
What's Best Practice for dealing with nested functions chains to get entity field values (check if they exist / check if they are not empty)
if($entity->hasField('field_intro_teaser_image_media_')){
$media_field = $entity->field_intro_teaser_image_media_;
if($media_field->entity){
$media_field_entity = $media_field->entity;
if($media_field_entity->hasField('field_media_image')){
$media_field_entity_image = $media_field_entity->field_media_image;
if($media_field_entity_image){
$media_field_entity_image_entity = $media_field_entity_image->entity;
if($media_field_entity_image_entity){
$media_field_entity_image_entity_uri = $media_field_entity_image->entity->getFileUri();
if($media_field_entity_image_entity_uri){
// ... yay we got the damn uri!
}
}
}
}
}
}
The one liner would be:
$media_field = $entity->field_intro_teaser_image_media_->entity->field_media_image->entity->getFileUri();