question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create fields for structural block types

See original GitHub issue

I 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:open
  • Created 8 years ago
  • Reactions:19
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

17reactions
gasmancommented, Feb 3, 2017

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 accept ListBlock / StructBlock as the top-level block).

13reactions
ddiazpintocommented, Aug 29, 2018

In my opinion, the best approach is to have an specific field for each structural block type:

  • StructField for StructBlock
  • ListField for ListBlock
  • StreamField for StreamBlock

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 usual

Of course ListField could be considered just a particular case of StreamField and be omitted, but if ListBlock exists, I think ListField should exists too.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found