Skip to content

Commit

Permalink
Add support for external links to call to action
Browse files Browse the repository at this point in the history
  • Loading branch information
trickeydan committed Dec 30, 2023
1 parent f630af8 commit 9cdff87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion kmicms/core/blocks/elements.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Any

from django.core.exceptions import ValidationError
from wagtail import blocks
from wagtail.images.blocks import ImageChooserBlock

Expand Down Expand Up @@ -33,7 +36,6 @@ class Meta:

class CallToActionBlock(blocks.StructBlock):
label = blocks.CharBlock()
link = blocks.PageChooserBlock()
style = blocks.ChoiceBlock(
choices=[
("primary", "Primary"),
Expand All @@ -46,9 +48,18 @@ class CallToActionBlock(blocks.StructBlock):
("dark", "Dark"),
]
)
link = blocks.PageChooserBlock(required=False, label="Internal Link")
external_link = blocks.URLBlock(required=False, label="External Link")

class Meta:
template = "core/blocks/elements/call-to-action.html"
help_text = "bees"

def clean(self, value: Any) -> Any:
result = super().clean(value)
if not (bool(result["link"]) ^ bool(result["external_link"])):
raise ValidationError("Please add either an internal or external link.")
return result


class CardBlock(blocks.StructBlock):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load wagtailcore_tags %}
<a class="btn btn-{{ value.style }} me-2" href="{% pageurl value.link %}">
<a class="btn btn-{{ value.style }} me-2" href="{% if value.link %}{% pageurl value.link %}{% else %}{{ value.external_link }}{% endif %}">
{{ value.label }}
</a>

0 comments on commit 9cdff87

Please sign in to comment.