How to Manually Build Render Array

Created on 10 July 2023, 12 months ago

Problem/Motivation

I'd like to move an image field rendering from the node content to a region on the page.

Proposed resolution

I currently build a render array for a regular image field like this:

if (isset($fields['field_image'])) {
  $field_view = $fields['field_image']->view(['label' => 'hidden']);
  $field_view['0']['#image_style'] = 'large';
  $field_view['#view_mode'] = 'full';
  $variables['page']['lead'][] = $field_view;
}

This works to render the image inside the "lead" region of the page.

My attempts (so far) to replicate this using a responsive image field that is configured to render as a responsive image with the image_replace module functionality results in just the original image rendered in the lead region, lacking the setsrc tag:

if (isset($fields['field_responsive_lead_image'])) {
  $field_view = $fields['field_responsive_lead_image']->view(['label' => 'hidden']);
  $field_view['#view_mode'] = 'responsive_image';
  $field_view['#image_style'] = 'lead_image';
  $variables['page']['lead'][] = $field_view;
}

What array elements do I need to configure (presumably instead of [or in addition to] $field_view['#image_style'] = 'lead_image') to trigger the image_replace module to step in and render a responsive image?

πŸ’¬ Support request
Status

Active

Version

2.0

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

Production build 0.69.0 2024