- 🇮🇪Ireland markconroy
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'.
Hi everyone one last clarification from my side:
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 @sonam’s workaround in comment #2 (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 so no one wastes time searching for it.
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.
- 🇬🇧United Kingdom catch
@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 → ? 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 .
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.
- 🇮🇪Ireland markconroy
2 things from me on this issue:
- I think it should be closed as "Works as designed"
- 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 wrappinga
tag.
And one friendly tip: when pasting code, please put it inside "
" tags.
- 🇬🇧United Kingdom catch
This is intentional see #3142818: Remove target=_blank from links in media settings forms → and also 📌 Target="_blank" attribute add to external links. Postponed and various other issues.
- 🇧🇪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... →
- 🇳🇿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 → .
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';
}
}
}
}
}- 🇮🇳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.