Last active
December 15, 2021 00:21
-
-
Save davision/fb88e93556638df53ede1f27f83152d7 to your computer and use it in GitHub Desktop.
How to get URL from Media entity field inside a paragraph in Drupal 8
This file contains 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\Core\Cache\CacheableMetadata; | |
use Drupal\Core\Url; | |
use Drupal\media\Entity\Media; | |
use Drupal\Core\Site\Settings; | |
use Drupal\file\Entity\File; | |
/** | |
* Preprocess functions for background iamge in Tagline paragraph | |
*/ | |
function mytheme_preprocess_paragraph__tagline(&$variables) { | |
$variables['background'] = FALSE; | |
$paragraph = $variables['paragraph']; | |
$field = $paragraph->get('field_background_image')->first(); | |
if ($field) { | |
$image_field = $paragraph->get('field_background_image')->first()->getValue(); | |
$media = Media::load($image_field['target_id']); | |
$media_field = $media->get('field_media_image')->first()->getValue(); | |
$file = File::load($media_field['target_id']); | |
if ($file) { | |
$background = $file->getFileUri(); | |
$variables['background'] = file_create_url($background); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment