Skip to content

Instantly share code, notes, and snippets.

@waako
Created June 3, 2016 12:11
Show Gist options
  • Save waako/ac55f5b2d1360beba89bfebbc7cf8754 to your computer and use it in GitHub Desktop.
Save waako/ac55f5b2d1360beba89bfebbc7cf8754 to your computer and use it in GitHub Desktop.
Drupal Paragraphs get image url with image style
<?php
use Drupal\image\Entity\ImageStyle;
/**
* Implements hook_preprocess_HOOK() for paragraph--para-slide.html.twig.
*/
function themename_preprocess_paragraph__para_slide(&$variables) {
$paragraph = $variables['paragraph'];
if (!$paragraph->field_image->isEmpty()) {
$image_uri = $paragraph->field_image->entity->getFileUri();
$style = \Drupal\image\Entity\ImageStyle::load('banner_wide');
$image = $style->buildUrl($image_uri);;
$variables['attributes']['style'][] = 'background-image: url("' . $image . '");';
$variables['attributes']['style'][] = 'background-size: cover;';
$variables['attributes']['style'][] = 'background-position: center center;';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment