Create fields for structural block types
See original GitHub issueI want to make a field that’s a simple list downloads. The following code accomplishes what I want using StreamField, but the API is a little weird.
class ResourcesPage(Page):
files = StreamField([
('files', DocumentChooserBlock())
])
Ideally it would look something like this:
class ResourcesPage(Page):
files = ListField(DocumentChooserBlock())
Here I’ve created a new imaginary type, ListField. There are a few possible solutions (such changing the way StreamField parameters work) but I think it makes sense to have a wrapper field around each of the structural block types.
Here’s an example of how an implementation could look:
from wagtail.wagtailcore.fields import StructField, ListField, StreamField
from wagtail.wagtaildocs.blocks import DocumentChooserBlock
from wagtail.wagtailimages.blocks import ImageChooserBlock
from wagtail.wagtailcore import blocks
class RecipePage(Page):
# StructBlock wrapper
chef = StructField([
('first_name', blocks.CharBlock(required=True)),
('surname', blocks.CharBlock(required=True)),
('photo', ImageChooserBlock()),
('biography', blocks.RichTextBlock())
])
# ListBlock wrapper
ingredients_list = ListField(blocks.CharBlock(label="Ingredient"))
# StreamBlock (just a regular StreamField)
instructions = StreamField([
('paragraph', blocks.RichTextBlock(icon="pilcrow")),
('h1', blocks.CharBlock(classname="title", icon="title"))
])
This could also help us determine how the fields should appear in the admin (example: #1549)
Issue Analytics
- State:
- Created 8 years ago
- Reactions:19
- Comments:20 (4 by maintainers)
Top Results From Across the Web
Creating Customs Blocks and Custom Block Types in Drupal 9
Custom Blocks & Custom Block Types · Click Add custom block type; Add a label and description; Click Save · Click Add field;...
Read more >Creating a new field type | Blockly - Google Developers
In JSON block definitions, fields are described by a string (e.g. field_number , field_textinput ). Blockly maintains a map from these strings to...
Read more >Structure Block - Minecraft Wiki - Fandom
A structure block is used to generate structures manually. They can also be used to save and load structures, alongside structure void blocks....
Read more >Add Fields to Custom Block Library in Drupal 8
Navigate to Manage> Structure> Block Layout> Custom Block Library · Now select the Block Types tab above. · Click on the Add Custom...
Read more >Field as Block | Drupal.org
Usage (Drupal 7) · Enable the module · Open the Manage Display admin page of the entity (for instance: /admin/structure/types/manage/article/ ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The core team has discussed this today, and we’re happy with the principle of adding
ListField
/StructField
types in parallel with StreamField (or, perhaps more simply, allowing StreamField to acceptListBlock
/StructBlock
as the top-level block).In my opinion, the best approach is to have an specific field for each structural block type:
StructField
forStructBlock
ListField
forListBlock
StreamField
forStreamBlock
Regarding to the wagtail admin views:
StructField
wouldn’t need to include a “new” button, just render the block form.ListField
wouldn’t need to show the stream menu to choose the model to add.StreamField
would work as usualOf course
ListField
could be considered just a particular case ofStreamField
and be omitted, but ifListBlock
exists, I thinkListField
should exists too.