Problem/Motivation

When adding a media entity with a link field (e.g., `field_image_link`) in a Paragraph or Block, there's no option to enforce a default `target="_blank"`.
This causes external links (like those wrapped around images) to open in the same tab, which is not ideal UX and inconsistent across pages.

Steps to reproduce

1. Create a media field that accepts a link (e.g., `field_image_link`).
2. Reference it in a Paragraph or Custom Block.
3. Leave the `target` value blank or choose "None".
4. Visit the frontend — the link opens in the **same tab**, even if it's external.

Proposed resolution

Introduce a default configuration option for link fields within media entities to set the `target="_blank"` by default when referencing external links.

Alternatively, allow site builders to configure default `target` behavior in field settings or via the display formatter.

Remaining tasks

- [ ] Confirm behavior in latest versions of D10 & D11.
- [ ] Discuss whether this should be core behavior or optional.
- [ ] Add patch or configuration setting (if approved).

User interface changes

Possibly a checkbox in the field UI:
**☑ Open external links in a new tab (_blank)**

Introduced terminology

- `target="_blank"`: Opens link in new tab/window.

API changes

None, unless a formatter plugin is updated.

Data model changes

None at this stage.

Release notes snippet

This fix ensures media link fields can default to opening in a new tab to improve UX and consistency across content.

Comments

sonam_goswami55 created an issue. See original summary.

sonam_goswami55’s picture

Issue summary: View changes
Issue tags: -Problem/Motivation: When adding a media entity with a link field (e.g., -field_image_link) in a Paragraph or Block, -there's no option to enforce a default target attribute (_blank). This causes external links from images to open in the same tab, -which isn't ideal UX and can be inconsistent across pages. Steps to Reproduce: 1.Create a media field that accepts links. 2.Reference it in a paragraph or block. 3.Leave target value blank or choose, -the link opens in the same tab. +media, +link field, +target _blank, +UX, +Accessibility
himanshu raj’s picture

@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):

href="{{ content.field_image_link.0['#url'] }}" target="_blank" rel="noopener">
{{ 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.

sagarsingh24’s picture

 #3 working

Environment tested
- Drupal 11.2
- PHP 8.3
What I did
1. Reproduced the issue: external links rendered by `field_image_link` opened in the same tab.
2. Applied #3
3. Cleared caches and rebuilt theme registry.
- External links now open in a new tab with `target="_blank" rel="noopener noreferrer"`.
- Internal links continue to open in the same tab as expected.

quietone’s picture

Version: 11.0.x-dev » 11.x-dev
Issue tags: -media, -link field, -target _blank, -UX

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.

smustgrave’s picture

Status: Needs review » Active

Doesn't appear anything to review

bramdriesen’s picture

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...

catch’s picture

Status: Active » Postponed (maintainer needs more info)
markconroy’s picture

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 "<code>" tags.

sonam_goswami55’s picture

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.

sagarsingh24’s picture

RE#7 Thanks for the heads‑up, and sorry for the mix‑up.I was juggling a few tickets and accidentally copied the 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 .

catch’s picture

@sagarsingh24 can you clarify which issue 3323974-media-link-blank-target-11.x.patch is uploaded to? Since it's not on issue #3323974: Subtheme _Variables.scss ?

sagarsingh24’s picture

Apologies for the confusion earlier.
There is no patch called 3323974‑media‑link‑blank‑target‑11.x.patch on Drupal.org.
I accidentally copied that filename into my first comment while moving personal notes between tickets.
What I actually tested was @himanshuraj workaround in comment #3 (the theme‑level hook_preprocess_field() snippet). I applied it locally, cleared caches, and confirmed it fixes the issue on Drupal 11.2 / PHP 8.3.
I’ve removed the stray filename from my earlier comment
I’m fairly new to the issue queue and was trying to write a professional‑sounding report; in doing so I mixed up my notes. I’ll be more careful going forward.
Thanks for everyone’s patience, and apologies for the confusion.

sagarsingh24’s picture

Sorry again

markconroy’s picture

If this is going anywhere, I don't think it should be going to media.

The _actual_ request seems to be "As a site builder, if I add a link field to an entity, I want the option to have that link open in a new tab". Is that accurate?

If so, I think this should be tagged with 'link.module' instead of 'media system'.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.