wagtail / wagtail-localize

Translation plugin for Wagtail CMS
https://wagtail-localize.org/
Other
226 stars 86 forks source link

widget_from_block returns unknown type for ImageChooser/DocumentChooser inside a ListBlock #638

Closed tspnn closed 2 years ago

tspnn commented 2 years ago

When a ListBlock contains an ImageChooserBlock or a DocumentChooserBlock, function widget_from_block returns {"type": "unknown"} because content_components is None

Model:

class HomePage(Page):
    body = StreamField(
        [
            ("text", blocks.TextBlock()),
            ("images", blocks.ListBlock(ImageChooserBlock())),
            ("documents", blocks.ListBlock(DocumentChooserBlock())),
        ],
        blank=True,
        null=True,
        use_json_field=True,
    )

    content_panels = Page.content_panels + [
        FieldPanel("body"),
    ]

Wagtail Admin

Possible solution: Add an extra elif statement that checks if the block is an instance of a ListBlock and content_components is None:

if isinstance(block, blocks.PageChooserBlock):
    ...
elif isinstance(block, blocks.ListBlock) and content_components is not None:
    return widget_from_block(block.child_block, content_components[1:])
# add this
elif isinstance(block, blocks.ListBlock) and content_components is None:
    return widget_from_block(block.child_block)
zerolab commented 2 years ago

I would've thought we had this covered by tests. Suppose not.

Will try to make a new patch release in the next 2-3 days. Or happily review a PR