Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

external links in rich text blocks now opening in new tab #8195

Merged
merged 7 commits into from
Feb 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions network-api/networkapi/wagtailpages/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from django.templatetags.static import static
from django.core.cache import cache
from django.urls import reverse
from django.utils.html import escape

from wagtail.admin.menu import MenuItem
from wagtail.admin.rich_text.editors.draftail import features as draftail_features
from wagtail.admin.rich_text.converters.html_to_contentstate import InlineStyleElementHandler
from wagtail.core import hooks
from wagtail.core.utils import find_available_slug
from wagtail.core.rich_text import LinkHandler

from networkapi.wagtailpages.pagemodels.products import BuyersGuidePage, ProductPage
from networkapi.wagtailpages.utils import get_locale_from_request
Expand Down Expand Up @@ -84,6 +86,21 @@ def register_large_feature(features):
features.default_features.append('large')


# Updating external links in rich text blocks to open in a new tab
class RichTextExternalLinkNewTabHandler(LinkHandler):
Copy link
Collaborator Author

@danielfmiranda danielfmiranda Feb 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs Referenced: https://docs.wagtail.org/en/latest/extending/rich_text_internals.html#registering-rewrite-handlers

What this new block is doing is adding a new handler for rich text links, and if they are external, add the target='_blank' to open in a new tab

identifier = 'external'

@classmethod
def expand_db_attributes(cls, attrs):
href = attrs["href"]
return '<a href="%s" target="_blank">' % escape(href)


@hooks.register('register_rich_text_features')
def register_external_rich_text_link(features):
features.register_link_type(RichTextExternalLinkNewTabHandler)


# Ensure that pages in the PageChooserPanel listings are ordered on most-recent-ness
@hooks.register('construct_page_chooser_queryset')
def order_pages_in_chooser(pages, request):
Expand Down