I'm altering the way images are rendered in the node preprocess function, and it is working exactly as expected for images in "Image" fields:
$field_view = $fields['field_image']->view(['label' => 'hidden']);
$field_view['#view_mode'] = 'full';
This results in the regular image array, so when I add it to my twig template, it gets rendered as an image tag.
However, we'd like to start using Media fields instead, and I can't figure out how to get the render array to come back with an image instead of just a text link. If I use the same thing for a media field, I just get a link with the image title as the text of the link:
$field_view = $fields['field_media_image']->view([
'label' => 'hidden',
]);
This is what the 'file' image render array looks like; it renders as an image, which is what I want to get for the media image field:
#theme => "field"
#title => "Image"
#label_display => "hidden"
#view_mode => "full"
#language => "und"
#field_name => "field_image"
#field_type => "image"
#field_translatable => true
#entity_type => "node"
#bundle => "page"
#object => Drupal\node\Entity\Node (29)
#items => Drupal\file\Plugin\Field\FieldType\FileFieldItemList (9)
#formatter => "image"
#is_multiple => false
#third_party_settings => array (0)
0 => array (6)
#cache => array (3)
#weight => 0
However, when I just use the same view()
function on the media image field, I get this, which doesn't render as an image:
#theme => "field"
#title => "Media Image"
#label_display => "hidden"
#view_mode => "_custom"
#language => "und"
#field_name => "field_media_image"
#field_type => "entity_reference"
#field_translatable => false
#entity_type => "node"
#bundle => "page"
#object => Drupal\node\Entity\Node (29)
#items => Drupal\Core\Field\EntityReferenceFieldItemList (9)
#formatter => "entity_reference_label"
#is_multiple => false
#third_party_settings => array (0)
0 => array (5)
#cache => array (3)
#weight => 0
I would guess that the reason this renders as a textual link is because of #formatter => "entity_reference_label"
but whenI change this value, I continue to get the same output.
template.theme
file. I understand how to do this for a regular Drupal image field, but not a media entity.