# Dynamically fetch corresponding title to structure

**URL:** <https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 4, 2022, 8:35pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957 "2022-11-04T20:35:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 4, 2022, 8:35pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/1 "2022-11-04T20:35:08Z")

</div>

Hey there,  
I’m using a structure field to get an overview of my website and manage categories.  
I can create a new row that is a new category.  
In the second column of that row, I would like to display the corresponding projects.  
So I though about a query that would match the title of that category, but it seems out of bound for a query.  
Maybe something else?

Something like :

```yml
title: Site

sections:
  content:
    type: fields
    fields:
      structure:
        label: Structure
        type: structure
        fields:
          action:
            label: Action
            type: text
          projects:
            label: Projects
            type: query
            query: site.find('title of the action in that row')
      pages:
        label: Projets
        type: pages
        query: site.page("projets").children

```

Thanks.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 4, 2022, 9:52pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/2 "2022-11-04T21:52:43Z")

</div>

Your `projects` field is missing a field type. But apart from that you won’t be able to read the value of the action field on the fly (at least not without some custom solution).

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 4, 2022, 10:28pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/3 "2022-11-04T22:28:27Z")

</div>

And what could be the custom solution ? Looking into page methods, or something like that?

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 13, 2022, 12:48pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/4 "2022-11-13T12:48:28Z")

</div>

I’m bumping it,  
since I would really like to find a way to do it.  
Also, maybe it woud be simpler to do another one, linked to the first idea :  
I would like to be able to select a category for a project in the panel, using the `type: select`  
And at the bottom, display all the items with the same categorie as the one selected in the dropdown.

Maybe it’s a bit complex to make it without reloading the page though.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2022, 1:17pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/5 "2022-11-13T13:17:58Z")

</div>

> [@paubou](#):
>
> Maybe it’s a bit complex to make it without reloading the page though

You are trying this inside a structure field. So the challenge would be to fetch the action of the right item in your blueprint, which is impossible.

The only way this could work on the blueprint side would be if you save first and then write the result to file using a hook.

This is probably not what you want. So you would have to do this in the Panel Vue part, with a custom (structure) field where the second field listens to the first.

If this is supposed to be an overview of your site, wouldn’t it make more sense to either use an info field that calculates all that and displays a table? Or even a custom panel area with a list/table. I don’t really see the advantage of trying to do this in a structure field. Might be missing something.

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 13, 2022, 3:15pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/6 "2022-11-13T15:15:36Z")

</div>

Or maybe I’m the one missing something ! Surely a table view would be easier.  
Do I need to check for a hook that listen to the left column in order to display the corresponding content in the right?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2022, 3:32pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/7 "2022-11-13T15:32:24Z")

</div>

> [@paubou](#):
>
> Do I need to check for a hook that listen to the left column in order to display the corresponding content in the right?

A hook will only work when you actually save the content to disk. And then you would need to populate the second field per row with this content as well.

As I said, the alternative would be a custom structure field that you would need to develop.

There is definitely no out of the box way to achieve what you want to do.

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 13, 2022, 3:35pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/8 "2022-11-13T15:35:16Z")

</div>

Alright, but what if we put aside the table and structure field and that I focus on the fact that I would like to show, on a project page, the articles in the same category as the one selected for the project ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2022, 3:39pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/9 "2022-11-13T15:39:04Z")

</div>

> [@paubou](#):
>
> that I focus on the fact that I would like to show, on a project page, the articles in the same category as the one selected for the project ?

That would be possible for example with an info field and a custom query that returns a list.

Again, this field would not react on the fly when you select the category, but only after you actually save.

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 13, 2022, 3:48pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/10 "2022-11-13T15:48:32Z")

</div>

```auto
      ailleurs:
        label: "{{page.category}}"
        type: pages
        parent: site.find("projets").children.filterBy('category', {{page.category}})

```

something like that, right ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2022, 3:52pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/11 "2022-11-13T15:52:51Z")

</div>

That doesn’t make sense, because this is a select field that does not display anything unless you actually select the pages or populate the field via a hook or so.

Maybe we have a misunderstanding here. I’m still not sure why you would want to display all pages with that category. If you want to select from that set, then yes, that would work.

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 13, 2022, 3:57pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/12 "2022-11-13T15:57:09Z")

</div>

I’m using pages wrong here then I guess… I would like to show a list of all the projects with the same category.  
Here is my blueprint :

```auto
sections:
  content:
    type: fields
    fields:
      category:
        label: Categorie
        type: select
        options:
          type: query
          query: site.structure.toStructure
          text: "{{ item.action }}"
          value: "{{ item.action }}"
      ailleurs:
        label: "{{ page.category }}"
        type: pages
        parent: # the list

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2022, 4:15pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/13 "2022-11-13T16:15:55Z")

</div>

What I mean is something like this:

```auto
ailleurs:
        label: "Other pages with category {{ page.category }}"
        type: info
        theme: none
        text: "{{page.siblings.filterBy('category', page.category).__toString}}"

```

Only instead of this text query you would create a custom page method that displays the data in a nice list. If that is what you want.

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 13, 2022, 4:27pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/14 "2022-11-13T16:27:31Z")

</div>

> [@texnixe](#):
>
> ```auto
> ailleurs:
> label: "Other pages with category {{ page.category }}"
> type: info
> theme: none
> text: "{{page.siblings.filterBy('category', page.category).__toString}}"
> 
> ```

That’s working, yeah, but I thought that page would return me a list of the pages, clickable. (Like _related_ in the template for example)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2022, 4:30pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/15 "2022-11-13T16:30:37Z")

</div>

That’s what you would have to do in your custom page method.

For example

```auto
 public function getListOfSiblingsWithCategory(): string
    {
        $html = '<ul>';

        foreach ($this->siblings(false)->filterBy('category', $this->category()) as $page) {
            $html .= '<li>' . Html::a($page->url(), $page->title()) . '</li>';
        }
        $html .= '</ul>';
        return $html;
    }

```

```auto
 ailleurs:
        label: "Other pages with category {{ page.category }}"
        type: info
        theme: none
        text: "{< page.getListOfSiblingsWithCategory() >}"

```

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 13, 2022, 4:50pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/16 "2022-11-13T16:50:47Z")

</div>

Alright! I’m starting to understand! haha. Thank you.  
I thought it would have been possible to show a page without formatting an `info` field but just by using a `pages` one.  
Since custom page methods are pretty new to me, I checked the doc, but you code doesn’t seem to belong in `/plugins/page-methods/index.php` right?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2022, 4:55pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/17 "2022-11-13T16:55:19Z")

</div>

Sorry, I quickly tested my code for possible errors in a page model, as a custom page method, it would have to look a bit different:

```auto
Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'getListOfSiblingsWithCategory' => function () {
            $html = '<ul>';

            foreach ($this->siblings(false)->filterBy('category', $this->category()) as $page) {
                 $html .= '<li>' . Html::a($page->url(), $page->title()) . '</li>';
            }
            $html .= '</ul>';
        
            return $html;
        }
    ]
]);

```

> [@paubou](#):
>
> I thought it would have been possible to show a page without formatting an `info` field but just by using a `pages` one.

A pages field is a specialized (multi)select field. Like a select field, it gives you options from which you can select pages, only the selected ones are then stored in the content file and shown in the list.

An alternative to using the info field could be the pagesdisplay section plugin:

[https://getkirby.com/plugins/rasteiner/pagesdisplay](https://getkirby.com/plugins/rasteiner/pagesdisplay)

Note that this is not a field, but a section.

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 13, 2022, 4:59pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/18 "2022-11-13T16:59:04Z")

</div>

> [@texnixe](#):
>
> A pages field is a specialized (multi)select field. Like a select field, it gives you options from which you can select pages, only the selected ones are then shown.

That’s true, yeah, but when I create sections, I seem to be able to fetch the project list for example using `section`

```auto
  project: sections/projets

```

then `projets.yml`

```auto
type: pages
label: Projets
layout: cards
parent: kirby.page("projets")
template: projet
template: projet
size: tiny
text: "*{{ page.title }}* {{ page.category }}"

```

returns a list of these project without me needing to add them.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2022, 5:02pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/19 "2022-11-13T17:02:04Z")

</div>

> [@paubou](#):
>
> That’s true, yeah, but when I create sections, I seem to be able to fetch the project list for example using `section`

That’s true, but a standard section doesn’t allow queries. Therefore I posted the link to the plugin above which would let you do that.

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 13, 2022, 5:03pm UTC](https://forum.getkirby.com/t/dynamically-fetch-corresponding-title-to-structure/26957/20 "2022-11-13T17:03:52Z")

</div>

Thanks for the plugin!
