Render image programmatically using field->view()

Created on 29 May 2024, 4 months ago
Updated 31 May 2024, 4 months ago

I'd like to render a Bynder image field outside of the normal node rendering area, so I'm using a page preprocessing function to accomplish this. with a non-bynder image field, I'm able to do the following:

          $field_view = $fields['field_image']->view(['label' => 'hidden']);
          $field_view['#view_mode'] = 'full';
          $variables['page']['lead'][] = $field_view;

But attempting to do something similar with the Bynder field results in a textual link to the asset's edit media URL:

⇄#type => string (4) "link"
⇄#title => string (14) "Cherry Trees 5"
⇄⧉#url => Drupal\Core\Url (12)
⇄⧉#options => array (3)
⇄⧉#entity => Drupal\media\Entity\Media (30)
⇄⧉#cache => array (3)

How do I get an actual image rendering like the default field output:

⇄#media => Drupal\media\Entity\Media (30)
⇄#view_mode => string (10) "lead_image"
⇄⧉#cache => array (5)
⇄#theme => string (5) "media"
⇄#weight => integer 0
⇄⧉#pre_render => array (1)
💬 Support request
Status

Closed: works as designed

Version

4.2

Component

Code

Created by

🇺🇸United States safetypin Memphis, Tennessee

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @safetypin
  • Status changed to Closed: works as designed 4 months ago
  • 🇺🇸United States safetypin Memphis, Tennessee

    I found a stack overflow answer that lead me to the following set of options that do what I was looking for:

              $display_options = [
                'label' => 'hidden',
                'type' => 'entity_reference_entity_view',
                'settings' => [
                  'view_mode' => 'lead_image',
                ],
              ];
              $field_view = $fields['field_bynder']->view($display_options);
    
Production build 0.71.5 2024