How can I implement a multiple choice for fields in Wagtail? - wagtail

How can I implement a multiple choice for fields in Wagtail? For example, I would like the user to be able to select h2, h3, or h4 for a field in the Wagtail editorial dashboard. Is this possible?

Define a field with a choices argument:
HEADING_SIZE_CHOICES = [
('h2', 'h2'),
('h3', 'h3'),
('h4', 'h4'),
]
class MyPage(Page):
heading_size = models.CharField(max_length=10, choices=HEADING_SIZE_CHOICES)
content_panels = Page.content_panels + [
FieldPanel('heading_size')
]
This will then display in the page editor as a select dropdown, and can be used like any other field:
<{{ page.heading_size }}>{{ page.title }}</{{ page.heading_size }}>

Related

Height of FieldPanel for Rich Text Editor in Wagtail 2.8

Working on a site in Wagtail 2.8 and my model has a RichTextField.
It shows up in the admin editor but it defaults to a single line if their is no text in the field (as in when the model is created).
Is their a way to have the FieldPanel show up larger?
My Content Panels decleration in the model class. Description is the rich text field.
content_panels = (
Page.content_panels
+ [
MultiFieldPanel(
[
ImageChooserPanel("image"),
FieldPanel("start_date"),
FieldPanel("end_date"),
FieldPanel("starting_location"),
FieldPanel("starting_address"),
FieldPanel("lattitude"),
FieldPanel("longitude"),
FieldPanel("ride_difficulty"),
FieldPanel("google_map_frame"),
],
heading="Event Details",
),
# This is the Rich Text field
FieldPanel("description", heading="Event Description", classname="full"),
]
I have handled it using a custom CSS. Please find the below given example. You can add the required CSS to meet your requirements here.
.Draftail-Editor{
border: 1px solid #e6e6e6!important;
border-radius: 5px!important;
padding-bottom: 70px!important;
}
And using webhook registered the CSS. I don't see any other widgets to help in this respect.
#hooks.register('insert_editor_css')
def editor_css():
return format_html(
'<link rel="stylesheet" href="{}">',
static('css/custom_admin.css')
)

How do I assign an edit panel for a StructBlock in Wagtail?

I have a StructBlock like this:
class JumbotronBlock(blocks.StructBlock):
fullwidth= blocks.BooleanBlock(requried=False, help_text="Full width of the viewport")
heading= blocks.CharBlock(required=True, help_text="Heading for the jumbotron")
lead= blocks.CharBlock(required= False, help_text= "Lead text for Jumbotron")
link= LinkBlock(required=False)
bg_image= ImageChooserBlock(required=False)
bg_color= blocks.CharBlock(required=False, help_text="Hex value for background color")
classes= blocks.CharBlock(required=False, help_text="CSS classes for jumbotron")
styles= blocks.CharBlock(required=False, help_text="Custom style definitions for jumbotron")
class Meta:
template="streams/jumbotron_block.html"
icon= "placeholder"
label= "Jumbotron"
And My HomePage Class model is this:
class HomePage(Page):
template= "home/home_page.html"
header= Jumbotron()
body = StreamField([
('Richtext', RichTextBlock()),
('CTA', CTABlock()),
('Section', SectionBlock()),
])
content_panels = Page.content_panels + [
??which panel here??('header'),
StreamFieldPanel('body'),
]
Which panel should I use to add the Jumbotron to the edit panels? Thanks.
StructBlock is not usable as a model field - it is only valid inside a StreamField. You need to define the fields of your jumbotron block as Django model fields within your HomePage model, and - if you want to group them together visually in the edit view - place them in a MultiFieldPanel.

Custom label for FieldPanel in Wagtail

Is there any way to set custom label to FieldPanel in Wagtail? I want to do something like:
class BlogPage(Page):
intro = models.CharField(max_length=255)
panels = [
FieldPanel('intro', label = "My custom label")
]
The model field accepts a verbose_name property, which will be picked up for use as a form field label:
class BlogPage(Page):
intro = models.CharField(max_length=255, verbose_name="My custom label")
panels = [
FieldPanel('intro')
]

How do I deliver a nested JSON array for content reference with export view display?

I created a product content type with content reference to article(one product to many articles). Then I added field "Content: ID" "Content: Title" and "(field_article: Content) Content: Title" to a new REST export View. "(field_article: Content) Content: Title" come from the relationship "Content referenced from field_article" I added in advance.
I have been working on drupal8.
The output is:
[
{
"nid":"3",
"title":"Product1 title",
"article_title":"Article1 title"
},
{
"nid":"3",
"title":"Product1 title",
"article_title":"Article2 title"
}
]
What I would like to achieve is something like this:
[
{
"nid":"3",[enter image description here][1]
"title":"Product1 title",
"articles":
[
{
"title":"Article1 title"
},
{
"title":"Article2 title"
}
]
}]
From the same question on Drupal Answers.
I've created a very simple module REST Export Nested to support nested JSON using Views Field Views.
After installing "REST Export Nested":
Install and enable Views Field
Views
Create view display of referenced content (Articles in your case) of type "REST export" or "REST export nested"
Add a relationship to the parent entity and contextual filter of parent entity ID
Create a view display of the parent entity of type "REST export
nested"
Add required fields (e.g. "nid", "title")
Add a field of type "Views field" (e.g. "articles"), configure with
the correct View and display and pass "nid" as the contextual filter
The module doesn't depend on Views Field View and may work with other fields which produce a JSON string.

Wagtail: Can snippets have InlinePanel if the model has a ForeignKey relationship?

I've got the situation where a Wagtail snippet is a model that has a FK relationship. I can't figure out how to make that available in the CMS as an inline.
Given:
#register_snippet
class TeamMember(models.Model):
name = models.CharField(max_length=80)
(other fields)
content_panels = [
FieldPanel('name'),
(etc.)
#InlinePanel('tasks', label="Team Tasks")
]
class Task(models.Model):
team_member = ForeignKey('TeamMember', related_name='tasks')
(other fields)
how do I allow Task to be an inline to TeamMember?
Or is this only possible if TeamMember is a Page?
You need to change the ForeignKey to ParentalKey. You may also need to change the TeamMember class to inherit from ClusterableModel.
#register_snippet
class TeamMember(ClusterableModel):
name = models.CharField(max_length=80)
panels = [
FieldPanel('name'),
InlinePanel('tasks', label="Team Tasks")
]
class Task(models.Model):
team_member = ParentalKey('TeamMember', related_name='tasks')
task = models.CharField(max_length=80)
panels = [
FieldPanel('task')
]

Resources