0

Lets say I have image fields in my node. When I get that fields values in the twig it returns the image URL without any issue.

file_url(node.field_image.entity.uri.value)

But now I want to add a specific parameter (lets say ?param=0) to the end of that link. Without hard coding the parameter in the twig file is there a way to write a hook that adds that parameter for all the image links in every node. So if I have to change the param I only need to change it from one place.

  • How is the image link created ? is it another field ? Link to itself as a bigger image ? – GiorgosK Mar 5 at 8:46
  • @GiorgosK No this is an normal image field in the node. I want to know how to add a param without putting it like file_url(node.field_image.entity.uri.value)?param=0. Is there a way to do this like globally. If I change from one place it should apply to all the images. – i am batman Mar 5 at 9:08
0

You can do this with a preprocess function THEMENAME_preprocess_field and check whether the field is an image and it has a URL

function THEMENAME_preprocess_field(&$variables) {
  if($variables["element"]["#field_type"] == "image"){
    if(isset($variables["items"][0]["content"]["#url"])){
      $variables["items"][0]["content"]["#url"]->setOption('query', ['param' => '0']);
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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