Skip to content

Commit

Permalink
Merge branch 'ccike-website-feature/title-field-patch'
Browse files Browse the repository at this point in the history
  • Loading branch information
Diogo Marques committed Aug 25, 2023
2 parents 012e6a3 + 88827d0 commit d83d00a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 93 deletions.
32 changes: 30 additions & 2 deletions wagtail_modeltranslation/patch_wagtailadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@
TRANSLATE_SLUGS)
from wagtail_modeltranslation.utils import compare_class_tree_depth

SIMPLE_PANEL_CLASSES = [FieldPanel] + CUSTOM_SIMPLE_PANELS
try:
# Wagtail 5.0.2 onwards.
from wagtail.admin.panels import TitleFieldPanel
SIMPLE_PANEL_CLASSES = [FieldPanel, TitleFieldPanel]
except ImportError:
TitleFieldPanel = None
SIMPLE_PANEL_CLASSES = [FieldPanel]

SIMPLE_PANEL_CLASSES += CUSTOM_SIMPLE_PANELS
COMPOSED_PANEL_CLASSES = [MultiFieldPanel, FieldRowPanel] + CUSTOM_COMPOSED_PANELS
INLINE_PANEL_CLASSES = [InlinePanel] + CUSTOM_INLINE_PANELS

Expand Down Expand Up @@ -186,7 +194,27 @@ def _patch_simple_panel(self, model, original_panel):
new_stream_block.meta.required = False
localized_field.stream_block = new_stream_block

localized_panel = panel_class(localized_field_name)
if panel_class == TitleFieldPanel and TRANSLATE_SLUGS:
if TRANSLATE_SLUGS:
# When a title field is changed its corresponding localized slug may need to
# be updated.
localized_panel = panel_class(
localized_field_name,
targets=[build_localized_fieldname(target, language)
for target in original_panel.targets])
elif language == mt_settings.DEFAULT_LANGUAGE:
# Slugs are not translated, so when a title field in the default language is
# updated we must update the slug it is linked to.
localized_panel = panel_class(
localized_field_name,
targets=original_panel.targets)
else:
# Slugs are not translated and this title field is in a non-default language.
# There is no slug to link the title to, so the TitleFieldPanel becomes a
# plain FieldPanel.
localized_panel = FieldPanel(localized_field_name)
else:
localized_panel = panel_class(localized_field_name)

# Pass the original panel extra attributes to the localized
if hasattr(original_panel, 'classname'):
Expand Down

This file was deleted.

12 changes: 2 additions & 10 deletions wagtail_modeltranslation/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@


@hooks.register('insert_editor_js')
def translated_slugs():
js_files = [
'wagtail_modeltranslation/js/wagtail_translated_slugs.js',
]

js_includes = format_html_join('\n', '<script src="{0}"></script>', (
(static(filename),) for filename in js_files)
)

def translation_settings():
lang_codes = []
for lang in settings.LANGUAGES:
lang_codes.append("'%s'" % lang[0])
Expand Down Expand Up @@ -70,7 +62,7 @@ def translated_slugs():
locale_picker_restore='true' if wmt_settings.LOCALE_PICKER_RESTORE else 'false',
)

return js_languages + js_includes
return js_languages


if wmt_settings.LOCALE_PICKER:
Expand Down

0 comments on commit d83d00a

Please sign in to comment.