Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Get objects (date field) from multiple pages (Twig)

blueprints form theme

Started by Val TOP 2 years ago · 7 replies · 178 views
2 years ago

Hi everyone,

I'm creating a website in which I have a "Calendar" page. On this page, I'd like to display the different events that are in the child pages of "Productions" in a calendar that I've created myself. An "Event" page (child of the "Productions" page) contains events with 3 fields (date, url and availability).

What I'd like to do is search for all the events in the "Event" pages and sort them by date.

I can easily access the information on these pages, for example:

TWIG
{% set prods = pages.find('/productions').children().published() %}
{% for event in prods %}
  {{ event.url }}
{% endfor %}

But I can't create a condition like:

if you find an event, in all the events, that happens on this date, then ...

for example:

TWIG
{% if event.header.dates.date|date("d.m.y") == " 10.09.24" %}
{% endif %}

How do I get my condition to pass through all the dates and choose the right one?

Thank you in advance for your help.

Edit: Here's my blueprint:

YAML
form:
  fields:
    tabs:
      fields:
        content:
          fields:
            dates:
              type: section
              title: "Dates"
              underline: true
              fields:
                header.dates:
                  type: list
                  label: "Add a date"
                  style: horizontal
                  fields:
                    .date:
                      type: datetime
                      label: "Date"
                    .url:
                      type: text
                      label: "Link to shop"
                    .available:
                      type: toggle
                      label: "Availabilty?"
                      highlight: 1
                      default: 1
                      options:
                        1: Available
                        0: No longer available
                      validate:
                        type: bool
last edited 05/23/24 by Val TOP
2 years ago

If your {{ event.url }} works, shouldn't you check for {{ event.date }}?

2 years ago

@ValTOP,

  • Please show the frontmatter of the page containing the definition of the 'dates' variable.
  • And dump the following values in Twig so you'll know what you are comparing with. Use:
    • {{ event.header.dates.date }}
    • {{ child.header.dates.date|date("d.m.y") }}
2 years ago

Because my dates are in a list type field (see blueprint in my original post)

2 years ago

I added the blueprint of the child page in the post.
The thing is that one “Event” page can have multiple events. That’s why I use a list type field.

2 years ago

@ValTOP, Instead of making us trying to understand your blueprint, one or two sample frontmatters would so be much easier to grasp...

2 years ago

Sorry, here it is:

YAML
dates:
    -
        date: '29-12-2024 17:00'
        url: 'https://google.com'
        available: true
    -
        date: '12-09-2024 14:00'
        url: 'https://google.com'
        available: true
    -
        date: '13-09-2024 16:00'
        url: 'https://google.com'
        available: false
2 years ago

@ValTOP, Did you try looping through the list of events?

TWIG
{% for event in child.header.dates %} 
  {% if event.date|date("d.m.y") == "29.12.24" %}
      {{ 'process event' }}
  {% endif %}
{% endfor %}

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1133 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 60 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 131 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 107 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 125 7 months ago