0

I'm encountering an issue when calling a preprocess function to load a styled image URL. This issue is based on the question here.

The following Preprocess Paragraph function sets the style based on the Manage Display. An error occurs if the Manage Display setting is "None (original image)".

How do we prevent errors during the preprocessing of the function when attempting to load an image style that doesn't exist or when Manage Display is set to None (original image)?

use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\image\Entity\ImageStyle;

function Mymodulename_preprocess_paragraph(&$vars) {
  $paragraph = $vars['paragraph'];
  if($paragraph->field_img[0]){
    // Get image style name
    $component = EntityViewDisplay::load('paragraph.sing.default')->getComponent('field_img');
    $img_style = $component['settings']['image_style'];
    $img = $paragraph->field_img[0]->entity->getFileUri();
    $style_img_url = ImageStyle::load($img_style)->buildUrl($img);
    $vars['style_img_url'] = $style_img_url;
  }
}

Render

<img src="{{ style_img_url }}">
2
  • I wrote an answer and deleted it, because I did not at first understand the question. You have the file's URI. Can you not return that if there is no image style?
    – cilefen
    Feb 6 at 17:26
  • Your question is on my question title. Knowing that syntax could potentially be one of the solutions.
    – CocoSkin
    Feb 6 at 17:46

2 Answers 2

1

This is intended to use the original file URL if no image style is selected.

use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\image\Entity\ImageStyle;

function Mymodulename_preprocess_paragraph(&$vars) {
  $paragraph = $vars['paragraph'];
  if($paragraph->field_img[0]){
    // Get image style name
    $component = EntityViewDisplay::load('paragraph.sing.default')->getComponent('field_img');
    $img_style = $component['settings']['image_style'];
    if (!empty($img_style)) {
      $img = $paragraph->field_img[0]->entity->getFileUri();
      $style_img_url = ImageStyle::load($img_style)->buildUrl($img);
      $vars['style_img_url'] = $style_img_url;
    }
    else {
      $vars['style_img_url'] = $paragraph->field_img[0]->entity->getFileUrl();
    }
  }
}
5
  • I haven't tested this.
    – cilefen
    Feb 6 at 20:15
  • The "if empty" syntax works here but outputs the wrong URL. It uses "public:", such as "public://original_file_name.jpg."
    – CocoSkin
    Feb 6 at 21:05
  • 1
    Try ...->entity->createFileUrl(); instead of getFileUrl()
    – Hudri
    Feb 7 at 9:03
  • The createFileUrl() does the trick.
    – CocoSkin
    Feb 8 at 15:59
  • Should I update the code?
    – cilefen
    Feb 8 at 16:03
0

Source code to get the original file URL on site of img field if no image style is selected along with added checks as well.

use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\image\Entity\ImageStyle;

function Mymodulename_preprocess_paragraph(&$vars) {
  $paragraph = $vars['paragraph'];
  // Added check paragraph type Here sing is machine name of paragraph
  // & field_img exist or not
  if($paragraph->getType() == 'sing' && $paragraph->field_img[0]){
    // Get image style name
    $component = EntityViewDisplay::load('paragraph.sing.default')->getComponent('field_img');
    $img_style = $component['settings']['image_style'];
    if($img_style){
      $img = $paragraph->field_img[0]->entity->getFileUri();
      $style_img_url = ImageStyle::load($img_style)->buildUrl($img);
      $vars['style_img_url'] = $style_img_url;
    }else{
      // Get original img path
      $vars['style_img_url'] = $paragraph->field_img[0]->entity->createFileUrl();
      //$vars['style_img_url'] = file_create_url($img);
    }
  }   
}

Note: Here use file_create_url() to get the original file URL i.e. is accessible by the user

if file_create_url() not work then can use createFileUrl()

2
  • I get the error message The website encountered an unexpected error. Try again later. when using the file_create_url().
    – CocoSkin
    Feb 8 at 14:51
  • if you want to debug why this file_create_url() not work , you can check recent logs ,
    – mini
    Feb 8 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.