Thursday, 24 February 2022

Custom linkhandler does not show up in PageLinkChooser

I am following the Wagtail docs to create a custom link handler:

myapp.handlers.py

from django.contrib.auth import get_user_model
from wagtail.core.rich_text import LinkHandler

class UserLinkHandler(LinkHandler):
    identifier = 'user'

    @staticmethod
    def get_model():
        return get_user_model()

    @classmethod
    def get_instance(cls, attrs):
        model = cls.get_model()
        return model.objects.get(username=attrs['username'])

    @classmethod
    def expand_db_attributes(cls, attrs):
        user = cls.get_instance(attrs)
        return '<a href="mailto:%s">' % user.email

my_app/wagtail_hooks.py

from wagtail.core import hooks
from my_app.handlers import MyCustomLinkHandler

@hooks.register('register_rich_text_features')
def register_link_handler(features):
    features.register_link_type(LinkHandler)

However, the handler does not show up in the admin widget. The expected behaviour is it should be in an option in the link type bar:

enter image description here

I've followed the docs exactly, is there something missing?



from Custom linkhandler does not show up in PageLinkChooser

No comments:

Post a Comment