0

How do I add target='blank' to a href in a Drupal 8 custom module? Below I have the image being wrapped by the uri. I don't know how to add target='blank'

  $element[$delta] = array(
        '#theme' => 'image_formatter',
        '#item' => $item,
        '#image_style' => '',
        '#url' => $uri,
      );
0

I think (not checked) that you need to add "target" => "_blank" ((note underscore)) to the #attributes array. However, that attribute must appear on an element that encloses the , not the itself, and I'm not sure if adding it to the structure in your question would do that... have a go.

If it doesn't work, simplest option is to override the twig template in question and then extend attributes with "attributes.setAttribute('target', '_blank')" in the appropriate place.

Determine the right file name to override the twig template with by using twig debug (look in the sites/default/local.services.yml for the setting to change). , and then view-source of the page (noting there are now lots and lots of html comments), find the existing image html and look before it to see the table of possible templates. There is lots more about this in the official Drupal documentation on twig.

| improve this answer | |

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.