You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When defining the preview value for StreamField blocks to be used in the block previews, you only have the following options:
Set preview_value on the block's Meta class
Pass preview_value when instantiating the block
Subclass the block and override get_preview_value()
The first two options are "eager", i.e. the value needs to be evaluated at startup time. This makes them unsuitable for values that require database access, e.g. chooser blocks like ImageBlock and DocumentChooserBlock. The last option requires subclassing the block. If you have multiple instances of the chooser blocks in different context, having to subclass each block class in order to have a different preview value is not very friendly.
Describe the solution you'd like
The default implementation of Block.get_preview_value() should allow self.meta.preview_value to be a callable with no arguments, in similar vein to Django's model fields default option. In fact, perhaps it's good to also tackle #7992 as part of this. This can be done by checking for callable() in get_preview_value() and get_default() and calling the value if it is indeed callable.
If #7992 is handled, this feature would already be possible by proxy (i.e. by defining a callable default and not defining a preview_value). However, it's still nice to have in case you want the preview value to be different from the default value (or have no default value at all).
Additional context
We'll also need new test cases in the following test:
Is your proposal related to a problem?
When defining the preview value for StreamField blocks to be used in the block previews, you only have the following options:
preview_value
on the block'sMeta
classpreview_value
when instantiating the blockget_preview_value()
The first two options are "eager", i.e. the value needs to be evaluated at startup time. This makes them unsuitable for values that require database access, e.g. chooser blocks like
ImageBlock
andDocumentChooserBlock
. The last option requires subclassing the block. If you have multiple instances of the chooser blocks in different context, having to subclass each block class in order to have a different preview value is not very friendly.Describe the solution you'd like
The default implementation of
Block.get_preview_value()
should allowself.meta.preview_value
to be a callable with no arguments, in similar vein to Django's model fieldsdefault
option. In fact, perhaps it's good to also tackle #7992 as part of this. This can be done by checking forcallable()
inget_preview_value()
andget_default()
and calling the value if it is indeed callable.wagtail/wagtail/blocks/base.py
Lines 309 to 319 in 4a97e2d
wagtail/wagtail/blocks/base.py
Lines 153 to 161 in 4a97e2d
Describe alternatives you've considered
If #7992 is handled, this feature would already be possible by proxy (i.e. by defining a callable
default
and not defining apreview_value
). However, it's still nice to have in case you want the preview value to be different from the default value (or have no default value at all).Additional context
We'll also need new test cases in the following test:
wagtail/wagtail/admin/tests/test_block_preview.py
Lines 145 to 172 in 4a97e2d
Working on this
Anyone can contribute to this. View our contributing guidelines, add a comment to the issue once you’re ready to start.
The text was updated successfully, but these errors were encountered: