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
Comment #2
sonam_goswami55 commentedComment #3
himanshu raj@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.
Comment #4
sagarsingh24 commented#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.
Comment #5
quietone commentedIn 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.
Comment #6
smustgrave commentedDoesn't appear anything to review
Comment #7
bramdriesenRE #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...
Comment #8
catchThis is intentional see #3142818: Remove target=_blank from links in media settings forms and also #2652272: Review (and in most cases remove) use of target="_blank" attribute outside the installer or update.php and various other issues.
Comment #9
markconroy commented2 things from me on this issue:
hrefon the wrappingatag.And one friendly tip: when pasting code, please put it inside "
<code>" tags.Comment #10
sonam_goswami55 commentedThanks 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.
Comment #11
sagarsingh24 commentedRE#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 .
Comment #12
catch@sagarsingh24 can you clarify which issue
3323974-media-link-blank-target-11.x.patchis uploaded to? Since it's not on issue #3323974: Subtheme _Variables.scss ?Comment #13
sagarsingh24 commentedApologies 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.
Comment #14
sagarsingh24 commentedSorry again
Comment #15
markconroy commentedIf 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'.