Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
Created October 19, 2018 17:36
Show Gist options
  • Save DanLaufer/88311a03808e0dfa723f75dd43e8c2df to your computer and use it in GitHub Desktop.
Save DanLaufer/88311a03808e0dfa723f75dd43e8c2df to your computer and use it in GitHub Desktop.
Drupal 8 - Act on files in php
// load file from id
$file_entity = \Drupal::entityTypeManager()->getStorage('file')->load($target_id);
//Get Uri/Url of file
//in preprocess field
$svg_uri = $vars['items']['0']['content']['#file']->getFileUri();
//in preprocess node
$svg_uri = $node->field_svg_image->entity->get('uri')->getValue();
$img_path = file_create_url($svg_uri);
//in preprocess paragraph
$temp_file = $current_hero->field_gated_content->entity;
$gated_file_url = \Drupal\Core\Url::fromUri(file_create_url($temp_file->getFileUri()))->toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment