Skip to content
Permalink
Browse files

Custom StreamField Logic w/ StructValues

  • Loading branch information
KalobTaulien committed Feb 8, 2019
1 parent bc89fa7 commit 4218b53508dc2697dcf032c0f5c56dce38cb929a
Showing with 45 additions and 0 deletions.
  1. +1 −0 flex/models.py
  2. +9 −0 mysite/templates/streams/button_block.html
  3. +35 −0 streams/blocks.py
@@ -20,6 +20,7 @@ class FlexPage(Page):
("simple_richtext", blocks.SimpleRichtextBlock()),
("cards", blocks.CardBlock()),
("cta", blocks.CTABlock()),
("button", blocks.ButtonBlock()),
],
null=True,
blank=True,
@@ -0,0 +1,9 @@
<div class="container mb-sm-5 mt-sm-5">
<div class="row">
<div class="col-md-5 offset-md-1 col-sm-12 text-center">
<h1>
<a href="{{ self.url }}">{{ self.url }}</a>
</h1>
</div>
</div>
</div>
@@ -82,3 +82,38 @@ class Meta: # noqa
template = "streams/cta_block.html"
icon = "placeholder"
label = "Call to Action"


class LinkStructValue(blocks.StructValue):
"""Additional logic for our urls."""

def url(self):
button_page = self.get('button_page')
button_url = self.get('button_url')
if button_page:
return button_page.url
elif button_url:
return button_url

return None

# def latest_posts(self):
# return BlogDetailPage.objects.live()[:3]


class ButtonBlock(blocks.StructBlock):
"""An external or internal URL."""

button_page = blocks.PageChooserBlock(required=False, help_text='If selected, this url will be used first')
button_url = blocks.URLBlock(required=False, help_text='If added, this url will be used secondarily to the button page')

# def get_context(self, request, *args, **kwargs):
# context = super().get_context(request, *args, **kwargs)
# context['latest_posts'] = BlogDetailPage.objects.live().public()[:3]
# return context

class Meta: # noqa
template = "streams/button_block.html"
icon = "placeholder"
label = "Single Button"
value_class = LinkStructValue

1 comment on commit 4218b53

@KalobTaulien

This comment has been minimized.

Copy link
Member Author

KalobTaulien commented on 4218b53 Feb 15, 2019

The video and full tutorial for this commit can be found at https://learnwagtail.com/tutorials/adding-custom-streamfield-logic/

Please sign in to comment.
You can’t perform that action at this time.