Skip to content

Commit

Permalink
Add buttons with register_page_header_buttons-hook (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
th3hamm0r authored Oct 7, 2022
1 parent a0b7da5 commit 51df05e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 31 additions & 10 deletions wagtail_localize/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ def register_submit_translation_permission():
)


if WAGTAIL_VERSION >= (4, 0, 0):

def set_button_icon(button, icon_name):
button.icon_name = icon_name
return button

else:

def set_button_icon(button, icon_name):
return button


def page_listing_more_buttons(page, page_perms, next_url=None):
if not page.is_root() and page_perms.user.has_perm(
"wagtail_localize.submit_translation"
Expand All @@ -148,8 +160,9 @@ def page_listing_more_buttons(page, page_perms, next_url=None):
if has_locale_to_translate_to:
url = reverse("wagtail_localize:submit_page_translation", args=[page.id])

yield wagtailadmin_widgets.Button(
_("Translate this page"), url, priority=60
yield set_button_icon(
wagtailadmin_widgets.Button(_("Translate this page"), url, priority=60),
"wagtail-localize-language",
)

# If the page is the source for translations, show "Sync translated pages" button
Expand All @@ -159,20 +172,24 @@ def page_listing_more_buttons(page, page_perms, next_url=None):
if next_url is not None:
url += "?" + urlencode({"next": next_url})

yield wagtailadmin_widgets.Button(
_("Sync translated pages"), url, priority=65
yield set_button_icon(
wagtailadmin_widgets.Button(
_("Sync translated pages"), url, priority=65
),
"resubmit",
)


if WAGTAIL_VERSION >= (4, 0, 0):

@hooks.register("register_page_listing_more_buttons")
def register_page_listing_more_buttons(page, page_perms, next_url=None):
for button in page_listing_more_buttons(page, page_perms, next_url):
yield button
hooks.register("register_page_header_buttons", page_listing_more_buttons)
hooks.register("register_page_listing_more_buttons", page_listing_more_buttons)

else:

if WAGTAIL_VERSION >= (2, 16, 0):
hooks.register("register_page_header_buttons", page_listing_more_buttons)

@hooks.register("register_page_listing_more_buttons")
def register_page_listing_more_buttons(
page, page_perms, is_parent=False, next_url=None
Expand Down Expand Up @@ -413,5 +430,9 @@ def format_message(self, log_entry):

@hooks.register("register_icons")
def register_icons(icons):
# icon id "wagtail-localize-convert" (which translates to `.icon-wagtail-localize-convert`)
return icons + ["wagtail_localize/icons/wagtail-localize-convert.svg"]
return icons + [
# icon id "wagtail-localize-convert" (which translates to `.icon-wagtail-localize-convert`)
"wagtail_localize/icons/wagtail-localize-convert.svg",
# icon id "wagtail-localize-language" (which translates to `.icon-wagtail-localize-language`)
"wagtail_localize/icons/wagtail-localize-language.svg",
]

0 comments on commit 51df05e

Please sign in to comment.