Skip to content

Instantly share code, notes, and snippets.

@segovia94
Last active August 27, 2021 06:06
Show Gist options
  • Save segovia94/413e61a3985df68bc42f5e9be90e6dfb to your computer and use it in GitHub Desktop.
Save segovia94/413e61a3985df68bc42f5e9be90e6dfb to your computer and use it in GitHub Desktop.
Drupal 8 preprocess custom image render from node
<?php
$node = \Drupal::routeMatch()->getParameter('node');
// Add a Banner to pages when a Featured Image is used
if ($node && !$node->field_featured_image->isEmpty()) {
$image = $node->field_featured_image->entity;
// Create the Render Array
$variables['banner_image'] = [
'#theme' => 'image_style',
'#uri' => $image->uri->value,
'#alt' => $image->alt,
'#title' => $image->title,
'#style_name' => 'banner_image'
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment