Add ability to specify attributes to pass into rendered block in admin interface. #11706
Replies: 2 comments
-
I'd recommend we use See https://docs.wagtail.org/en/stable/reference/panel_api.html#panel & #10629 We also support See https://docs.wagtail.org/en/stable/topics/images.html#attrs |
Beta Was this translation helpful? Give feedback.
-
I have a sort of workaround for field blocks and choosers for now. It sets the data attribute on the django field widget rather than the block, but that works for almost all of my case. Ideally this could be done on the Block level to allow non-field related blocks to take the data attributes dictionary. class DataFieldBlockMixin:
def __init__(self, *args, attrs={}, **kwargs):
super().__init__(*args, **kwargs)
self.attrs = self.field.widget.attrs.update(attrs)
class DataCharBlock(DataFieldBlockMixin, CharBlock):
pass
class DataExtendedURLBlock(DataFieldBlockMixin, ExtendedURLBlock):
pass
class DataChooserBlockMixin:
def __init__(self, *args, attrs={}, chooser_attrs={}, **kwargs):
super().__init__(*args, **kwargs)
self.attrs = attrs
self.chooser_attrs = chooser_attrs
@cached_property
def field(self):
field = super().field
field.widget.attrs.update(self.attrs)
return field
@cached_property
def widget(self):
chooser = super().widget
for key, value in self.chooser_attrs.items():
if hasattr(chooser, key):
setattr(chooser, key, value)
return chooser
class DataPageChooserBlock(DataChooserBlockMixin, PageChooserBlock):
pass
class DataDocumentChooserBlock(DataChooserBlockMixin, DocumentChooserBlock):
pass Added bonus to set chooser attributes as well: DataCharBlock(
attrs={'data-link-block-type': 'page'}
)
DataPageChooserBlock(
attrs={'data-link-block-type': 'page'},
chooser_attrs={'show_edit_link': False}
) That renders the input elements with: <input type="text" name="content-0-value-anchor_target"
data-link-block-type="page"
id="content-0-value-anchor_target"
aria-describedby="content-0-value-anchor_target-helptext">
<input type="hidden" name="content-0-value-page"
data-link-block-type="page"
id="content-0-value-page" value=""> |
Beta Was this translation helpful? Give feedback.
-
It would be very useful to be able to add attributes to the Block initialiser in the same way css classnames can be so that any type of blocks rendered in the admin interface can take these on without the need for custom forms.
For example, with a CharBlock:
would render:
At the moment, the only way I can find to do this is to add the attribute as a class (i.e.
classname="thing-class data-thing-bit--something"
) and use some JS to convert that, or use a custom form (which means a custom block for each type to do this with). The JS method limits values to slug compatible values which isn't always valid.Beta Was this translation helpful? Give feedback.
All reactions