4

My company is running Wagtail as a headless API, using it more as a way to store bits of content rather than entire pages. As such there's the occasional feature that doesn't make sense for us. In this case it's the "internal link" feature. Since we don't manage "pages" per se I'd like to remove this option from the chooser found on the rich text field, as seen below.

enter image description here

I've identified several admin templates which could be overridden to remove this functionality, but I wanted to first see if there's something which can simply disable this "internal link" option so that it just doesn't even show up.

The _link_types.html template would allow me to remove Internal Link as a choice, but it appears Wagtail defaults to Internal Link which means that even if the option is gone, the Internal Link chooser still shows up. Barring a simple option that can be toggled off, where should I be looking to default selection to External Link?

commadelimited
  • 5,656
  • 6
  • 41
  • 77

1 Answers1

3

Below is an approach, it kind of feels a bit hacky and it would be great if there was a more natural way to do this but hopefully this helps.

See the documentation for an explanation of the Wagtail Hooks.

Step 1 - hide the internal link option

  • Use the hook insert_editor_css to inject some css to 'hide' the first link.
  • This achieves the same goal as the _link_types template override you have attempted but 'scopes' this to the editor modal only.
  • This is important as you want to avoid breaking the 'move page' and scenarios where the page chooser will be shown. The css feels a bit hacky but hopefully gets the job done.

Step 2 - override the internal link option to external link for modals

  • Use the hook insert_editor_js to override the window.chooserUrls.pageChooser value, this will again be on the editor page only & for the modals only.
  • Set this value to the new 'default' you want, in the code below we have set this to the external link option.
  • You can see how these values are set globally in the editor_js.html template.

Code


# file: wagtail_hooks.py

from django.contrib.staticfiles.templatetags.staticfiles import static
from django.utils.html import format_html
from django.urls import reverse

from wagtail.core import hooks


@hooks.register('insert_editor_css')
def editor_css():
    """Add /static/css/admin.css to the admin."""
    return format_html(
        '<link rel="stylesheet" href="{}">',
        static("css/admin.css")
    )


@hooks.register('insert_editor_js')
def editor_js():
    return format_html(
        """
        <script>
            window.chooserUrls.pageChooser = '{}';
        </script>
        """,
        reverse('wagtailadmin_choose_page_external_link')
    )
/* file: static/css/admin.css */

.modal-content .link-types :first-child {
  /* hide the 'internal' link option from the page chooser */
  display: none;
}

.modal-content .link-types {
  /* ensure the 'before' element can be positioned absolute */
  position: relative;
}

.modal-content .link-types::before {
  /* hide the left '|' bar */
  background: white;
  bottom: 0;
  content: '';
  left: 0;
  position: absolute;
  top: 0;
  width: 5px;
}
LB Ben Johnston
  • 4,751
  • 13
  • 29
  • Note: Originally I tried to override the page chooser URL to a custom view that would give us more control over the rendering, however this is not possible & I have raised this issue - https://github.com/wagtail/wagtail/issues/5710 (feel free to comment with your thoughts on it). – LB Ben Johnston Nov 17 '19 at 05:36
  • This is probably less hacky than my solution. I added a class to the "External Link" anchor tag, then a line of Javascript which "clicks" the external link whenever the Internal Link modal is displayed. :D. The user is "forwarded" to the External Link" modal. – commadelimited Nov 18 '19 at 14:36
  • I accepted this as the answer, but with 2 changes. I didn't want to bother with the CSS route, so instead I overrode the `_link_types.html` file (https://github.com/wagtail/wagtail/blob/master/wagtail/admin/templates/wagtailadmin/chooser/_link_types.html#L4-L12) and simply removed that option. That's easier, and cleaner. Thanks for your suggestion LB! – commadelimited Nov 18 '19 at 15:50
  • Thanks @commadelimited - glad this has helped. – LB Ben Johnston Nov 19 '19 at 08:35