从Wagtail RichTextField链接选择器中删除“内部链接”选项

我的公司将Wagtail作为无头API运行,更多地将Wagtail用作存储少量内容而不是整个页面的方式。因此,偶尔会有一些对我们没有意义的功能。在这种情况下,这就是“内部链接”功能。由于我们本身不管理“页面”,因此我想从富文本字段上的选择器中删除此选项,如下所示。

从Wagtail RichTextField链接选择器中删除“内部链接”选项

我已经确定了几个可以删除的管理模板,以删除此功能,但我想首先查看是否有一些东西可以简单地禁用此“内部链接”选项,以便它甚至不会显示。 / p>

_link_types.html template允许我删除“内部链接”作为选择,但Wagtail似乎默认为“内部链接”,这意味着即使该选项消失了,“内部链接”选择器仍会显示。除非有一个可以关闭的简单选项,我应该在哪里查看默认选择“外部链接”?

qch197999 回答:从Wagtail RichTextField链接选择器中删除“内部链接”选项

下面是一种方法,感觉有点,如果有一种更自然的方法可以这样做,但希望能有所帮助。

有关Wagtail Hooks的说明,请参阅文档。

步骤1-隐藏内部链接选项

  • 使用钩子insert_editor_css注入一些CSS以“隐藏”第一个链接。
  • 这实现了与您尝试过的_link_types模板替代相同的目标,但仅将此作用域“限制”到了编辑器模式。
  • 这很重要,因为您要避免破坏“移动页面”以及显示页面选择器的情况。 css有点笨拙,但希望能完成工作。

步骤2-将内部链接选项替换为模态的外部链接

  • 使用钩子insert_editor_js覆盖window.chooserUrls.pageChooser值,该值将再次在编辑器页面上和仅用于模态。
  • 将此值设置为所需的新“默认”,在下面的代码中,我们将其设置为“外部链接”选项。
  • 您可以查看如何在editor_js.html模板中全局设置这些值。

代码


# 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;
}
本文链接:https://www.f2er.com/3101248.html

大家都在问