Created
June 3, 2016 12:11
-
-
Save waako/ac55f5b2d1360beba89bfebbc7cf8754 to your computer and use it in GitHub Desktop.
Drupal Paragraphs get image url with image style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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