Link field target not defaulting to "_blank" in Media entity references

Created on 15 July 2025, about 23 hours ago

Problem/Motivation

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Sign in to follow issues

Comments & Activities

  • Issue created by @sonam_goswami55
  • 🇮🇳India himanshu raj Noida

    @sonam
    Tested Workaround – Two Options to Set target="_blank" for Media Link Fields
    This can be handled cleanly without needing core changes. Here are two approaches we’ve tested:

    1. Twig template override (for direct control):


    {{ content.field_media }}

    Useful when rendering the media reference manually in custom templates.

    2. Theme-level preprocess hook (global):

    function Theme_preprocess_field(array &$variables) {
    if ($variables['element']['#field_name'] === 'field_image_link') {
    foreach ($variables['items'] as &$item) {
    if (!empty($item['#url']) && $item['#url']->isExternal()) {
    $item['#options']['attributes']['target'] = '_blank';
    $item['#options']['attributes']['rel'][] = 'noopener';
    }
    }
    }
    }
    This makes all external links in that field open in a new tab site-wide.

  • I’m seeing the same behaviour (external links rendered via **`field_image_link`** open in the same tab).
    Until we have a core‑level fix, the snippet below solves it at theme level by adding `target="_blank"` and the recommended `rel` attributes for external URLs:

    ```php
    /**
    * Theme‑level work‑around: set target="_blank" on external media link fields.
    */
    function MYTHEME_preprocess_field(array &$variables): void {
    // Limit to the specific link field.
    if ($variables['element']['#field_name'] === 'field_image_link') {
    foreach ($variables['items'] as &$item) {
    // Skip if a target is already provided.
    if (!isset($item['#options']['attributes']['target'])) {
    $url = $item['#url']->toString();
    // External link? (not on current host)
    if (strpos($url, \Drupal::request()->getSchemeAndHttpHost()) === FALSE) {
    $item['#options']['attributes']['target'] = '_blank';
    $item['#options']['attributes']['rel'][] = 'noopener';
    $item['#options']['attributes']['rel'][] = 'noreferrer';
    }
    }
    }
    }
    }

  • 🇳🇿New Zealand quietone

    In Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies .

  • 🇺🇸United States smustgrave

    Doesn't appear anything to review

  • 🇧🇪Belgium BramDriesen Belgium 🇧🇪

    RE #4, what did you test? There is no patch #2, the patch you are referencing (3323974-media-link-blank-target-11.x.patch) does not exist, nor here, nor in issue #3323974 which I guess is not even closely related to this one.

    Did you use AI to generate your comment? If yes, be aware this is covered under the credit abuse policy. https://www.drupal.org/drupalorg/docs/marketplace/abuse-of-the-contribut...

  • 🇮🇪Ireland markconroy

    2 things from me on this issue:

    1. I think it should be closed as "Works as designed"
    2. If keeping it open, both approaches in #3 are incorrect. At minimum, create a variable in hook_preprocess, and use that variable for the href on the wrapping a tag.

    And one friendly tip: when pasting code, please put it inside "" tags.

  • Thanks for the response.

    I'm not suggesting to force target="_blank" globally — rather, it would be helpful to have an optional checkbox in the media field UI that lets site builders choose to open external links in a new tab if desired.

    This could be an opt-in setting, improving user experience especially when media (like images) with links are used inside Paragraphs or Blocks.

  • RE#7 Thanks for the heads‑up, and sorry for the mix‑up.I was juggling a few tickets and accidentally copied the patch name from another issue. The tests I ran here were all done manually; there was no AI involved. I’ve edited my earlier comment to remove the wrong filename .Appreciate the reminder about the credit‑abuse policy .

Production build 0.71.5 2024