add a button link component

This commit is contained in:
2026-07-02 23:44:53 +02:00
parent 31453af238
commit b0698247b6
14 changed files with 371 additions and 29 deletions
+48
View File
@@ -1,3 +1,4 @@
from django.core.exceptions import ValidationError
from grapple.helpers import register_streamfield_block
from grapple.models import (
GraphQLImage,
@@ -7,6 +8,7 @@ from grapple.models import (
GraphQLString,
)
from wagtail import blocks
from wagtail.blocks import StructBlockValidationError
from wagtail.embeds.blocks import EmbedBlock
from wagtail.images.blocks import ImageChooserBlock
@@ -281,6 +283,51 @@ class ScheduleBlock(blocks.StructBlock):
label = "Timeplan"
@register_streamfield_block
class ButtonLinkBlock(blocks.StructBlock):
text = blocks.CharBlock(
max_length=64,
required=True,
label="Tekst",
help_text="Teksten som vises på knappen.",
)
page = blocks.PageChooserBlock(
required=False,
label="Intern side",
help_text="Velg en underside på denne nettsiden.",
)
anchor = blocks.CharBlock(
max_length=64,
required=False,
label="Anker",
help_text="Anker på siden, uten «#» (valgfritt)",
)
external_url = blocks.URLBlock(
required=False,
label="Ekstern lenke",
)
graphql_fields = [
GraphQLString("text", required=True),
GraphQLPage("page"),
GraphQLString("anchor"),
GraphQLString("external_url"),
]
def clean(self, value):
result = super().clean(value)
if value.get("page") and value.get("external_url"):
error = ValidationError(
"Velg enten en intern side eller en ekstern lenke, ikke begge."
)
raise StructBlockValidationError(block_errors={"page": error, "external_url": error})
return result
class Meta:
icon = "link"
label = "Knapp med lenke"
BASE_BLOCKS = [
("paragraph", blocks.RichTextBlock(label="Rik tekst")),
("image", ImageWithTextBlock()),
@@ -291,6 +338,7 @@ BASE_BLOCKS = [
("accordion", AccordionBlock()),
("fact_box", FactBoxBlock()),
("schedule", ScheduleBlock()),
("button_link", ButtonLinkBlock()),
("embed", EmbedBlock()),
("raw_html", blocks.RawHTMLBlock()),
]