# Fetch query from parent page

**URL:** <https://forum.getkirby.com/t/fetch-query-from-parent-page/1290>\
**Category:** Questions\
**Tags:** v2\
**Created:** [June 8, 2015, 3:51pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290 "2015-06-08T15:51:55Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![unli355](https://avatars.discourse-cdn.com/v4/letter/u/e274bd/32.png) [@unli355](https://forum.getkirby.com/u/unli355)\
**Post date:** [June 8, 2015, 3:51pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/1 "2015-06-08T15:51:55Z")

</div>

Hey, i need to get the result of a structure as a select from a children page.  
I have the main page with this:

categorias:  
label: Categorias  
type: structure  
entry: {{categoria}}  
fields:  
categoria:  
label: Nome da Categoria  
type: text

And the page inside it i need the option to choose from all the entries added in “categoria” on a select.  
Is it possible?  
Thanks in advance

---

<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:** [June 8, 2015, 4:44pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/2 "2015-06-08T16:44:27Z")

</div>

Yes, you can do that via a custom form field : [http://getkirby.com/docs/panel/custom-form-fields](http://getkirby.com/docs/panel/custom-form-fields)

You can adapt this example by just replacing the value for $categories with the structure fields:

```auto
<?php

class CategoryField extends BaseField {

  public function __construct() {
    $this->type = 'category';
    $this->icon = 'chevron-down';
    $this->label = 'category';
    $this->options = array();

  }  
public function options() {
    return FieldOptions::build($this);
  }
  public function option($value, $text, $selected = false) {
    return new Brick('option', $this->i18n($text), array(
      'value' => $value,
      'selected' => $selected
    ));
  }
  	public function input() {
  		$select = new Brick('select');
  		$select->addClass('selectbox');
  		$select->attr(array(
	      'name' => $this->name(),
	      'id' => $this->id(),
	      'required' => $this->required(),
	      'autocomplete' => $this->autocomplete(),
	      'autofocus' => $this->autofocus(),
	      'readonly' => $this->readonly(),
	      'disabled' => $this->disabled(),
	    ));
  	    
    $select = $select->append($this->option('', '', $this->value() == ''));

  	
  	if($this->readonly()) {
      $select->attr('tabindex', '-1');
    }

//this part uses a field with a comma separated list, replace this with the structure field
    if($this->page()->parent()->categories() != "") {
      $categories = $this->page()->parent()->categories()->split();
    }
    else {
      $categories = site()->categories()->split();
    }
   
    foreach($categories as $category) {
      $this->options[$category] = $category;
    }

    foreach($this->options() as $value => $text) {
      $select->append($this->option($value, $text, $this->value() == $value));
    }

    $inner = new Brick('div');
    $inner->addClass('selectbox-wrapper');
    $inner->append($select);

    $wrapper = new Brick('div');
    $wrapper->addClass('input input-with-selectbox');
    $wrapper->append($inner);

    if($this->readonly()) {
      $wrapper->addClass('input-is-readonly');
    } else {
      $wrapper->attr('data-focus', 'true');
    }

    return $wrapper;
  	
  }

}

```

---

<div class="post-metadata">

**Author:** ![unli355](https://avatars.discourse-cdn.com/v4/letter/u/e274bd/32.png) [@unli355](https://forum.getkirby.com/u/unli355)\
**Post date:** [June 8, 2015, 5:05pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/3 "2015-06-08T17:05:41Z")

</div>

Not sure i undrstand sorry, how do i add that field to the blueprint after i create it?  
I need the select field in the panel, on the page inside the main one. It isnt for the frontend.  
Isnt there any option on this page that does that?  
[http://getkirby.com/docs/cheatsheet/panel-fields/select](http://getkirby.com/docs/cheatsheet/panel-fields/select)  
Ive been trying but the select keeps coming up empty.

---

<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:** [June 8, 2015, 5:24pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/4 "2015-06-08T17:24:20Z")

</div>

What you need to do:

1. Save the above file in `site/fields/category as category.php`
2. In your subpage blueprint, use type “category” instead of “select”

---

<div class="post-metadata">

**Author:** ![unli355](https://avatars.discourse-cdn.com/v4/letter/u/e274bd/32.png) [@unli355](https://forum.getkirby.com/u/unli355)\
**Post date:** [June 8, 2015, 8:24pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/5 "2015-06-08T20:24:57Z")

</div>

ok i got it to work but it just shows one option with all the categories like this:

- categoria: Painting - categoria: Design - categoria: Webdesign - categoria: Filmography

Sorry for being a noob but this is getting a bit complex for what i thought i would need to know when i bought kirby.

---

<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:** [June 8, 2015, 9:19pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/6 "2015-06-08T21:19:51Z")

</div>

Try this

```php
<?php

class CategoryField extends BaseField {

  public function __construct() {
    $this->type = 'category';
    $this->icon = 'chevron-down';
    $this->label = 'category';
    $this->options = array();

  }  
public function options() {
    return FieldOptions::build($this);
  }
  public function option($value, $text, $selected = false) {
    return new Brick('option', $this->i18n($text), array(
      'value' => $value,
      'selected' => $selected
    ));
  }
  	public function input() {
  		$select = new Brick('select');
  		$select->addClass('selectbox');
  		$select->attr(array(
	      'name' => $this->name(),
	      'id' => $this->id(),
	      'required' => $this->required(),
	      'autocomplete' => $this->autocomplete(),
	      'autofocus' => $this->autofocus(),
	      'readonly' => $this->readonly(),
	      'disabled' => $this->disabled(),
	    ));
  	    
    $select = $select->append($this->option('', '', $this->value() == ''));

    
  	
  	if($this->readonly()) {
      $select->attr('tabindex', '-1');
    }

    if($this->page()->parent()->categorias()) {
      $categories = $this->page()->parent()->categorias()->toStructure();
    }
   
    foreach($categories as $category) {
      $category = $category->categoria();
      $this->options[] = $category;
    }

    foreach($this->options() as $value => $text) {
      $select->append($this->option($value, $text, $this->value() == $value));
    }

    $inner = new Brick('div');
    $inner->addClass('selectbox-wrapper');
    $inner->append($select);

    $wrapper = new Brick('div');
    $wrapper->addClass('input input-with-selectbox');
    $wrapper->append($inner);

    if($this->readonly()) {
      $wrapper->addClass('input-is-readonly');
    } else {
      $wrapper->attr('data-focus', 'true');
    }

    return $wrapper;
  	
  }

}

```

Pls note that this will only work with the structure field called “categorias” and the field “categoria” within that structure field. If you rename the field, you have to change this bit of code.

---

<div class="post-metadata">

**Author:** ![unli355](https://avatars.discourse-cdn.com/v4/letter/u/e274bd/32.png) [@unli355](https://forum.getkirby.com/u/unli355)\
**Post date:** [June 9, 2015, 8:29am UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/7 "2015-06-09T08:29:57Z")

</div>

Your awesome, thanks.  
Only one thing left, it’s returning select number and i need the actual name.

---

<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:** [June 9, 2015, 9:08am UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/8 "2015-06-09T09:08:03Z")

</div>

I think these lines

```php
foreach($this->options() as $value => $text) {
      $select->append($this->option($value, $text, $this->value() == $value));

```

have to be changed to:

```php
foreach($this->options() as $value => $text) {
      $select->append($this->option($text, $text, $this->value() == $text));
    }

```

Actually, I don’t really know why it **does not work** to assign the key value pairs in the foreach loop like this:

```php
foreach($categories as $category) {
      $category = $category->categoria();
      $this->options[$category] = $category;
    }

```

---

<div class="post-metadata">

**Author:** ![unli355](https://avatars.discourse-cdn.com/v4/letter/u/e274bd/32.png) [@unli355](https://forum.getkirby.com/u/unli355)\
**Post date:** [June 9, 2015, 10:24am UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/9 "2015-06-09T10:24:00Z")

</div>

It’s working, thanks for everything.  
Hope i don’t get stuck anymore lol  
This was probably the hardest part on my template, only problem with this is that categories with spaces break the code, i think ill add 2 fields to the structure, one for display and other for the code itself.

---

<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:** [June 9, 2015, 10:46am UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/10 "2015-06-09T10:46:45Z")

</div>

Another way of doing it would be to stick to the numbers as keys and map the keys to the values in your config file and call them via `c::get()` in your template.

---

<div class="post-metadata">

**Author:** ![rhawbert](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rhawbert/32/586_2.png) [@rhawbert](https://forum.getkirby.com/u/rhawbert)\
**Post date:** [September 4, 2015, 9:31pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/11 "2015-09-04T21:31:28Z")

</div>

I tried to apply this to a page of mine… same structure and idea (select in child pages loading values from a structure field) but I think something has changed since 2.1 update. I’m getting an error:

> Warning: Illegal offset type in isset or empty in /Users/me/Sites/mysite/kirby/toolkit/lib/silo.php on line 30

The array manipulation on the code below must be doing something wrong:

```php
if($this->page()->parent()->categorias()) {
  $categories = $this->page()->parent()->categorias()->toStructure();
}
   
foreach($categories as $category) {
  $category = $category->categoria();
  $this->options[] = $category;
}

foreach($this->options() as $value => $text) {
  $select->append($this->option($value, $text, $this->value() == $value));
}

```

My blueprint… is using the custom field like:

```yml

level:
  label: Difficulty level
  type: category  

```

---

<div class="post-metadata">

**Author:** ![unli355](https://avatars.discourse-cdn.com/v4/letter/u/e274bd/32.png) [@unli355](https://forum.getkirby.com/u/unli355)\
**Post date:** [September 5, 2015, 8:56am UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/12 "2015-09-05T08:56:32Z")

</div>

Wish i could help, but i’m not using this anymore. I ended up having a page for each category and then use:  
category:  
label: Additional Category  
type: select  
options: query  
query:  
page: work  
fetch: children  
value: '{{uid}}'  
text: ‘{{title}}’

---

<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:** [September 5, 2015, 9:00am UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/13 "2015-09-05T09:00:40Z")

</div>

Try this:

```php
<?php

class CategoryField extends BaseField {

  public function __construct() {
    $this->type = 'category';
    $this->icon = 'chevron-down';
    $this->label = 'category';
    $this->options = array();

  }  
public function options() {
    return FieldOptions::build($this);
  }
  public function option($value, $text, $selected = false) {
    return new Brick('option', $this->i18n($text), array(
      'value' => $value,
      'selected' => $selected
    ));
  }
  	public function input() {
  		$select = new Brick('select');
  		$select->addClass('selectbox');
  		$select->attr(array(
	      'name' => $this->name(),
	      'id' => $this->id(),
	      'required' => $this->required(),
	      'autocomplete' => $this->autocomplete(),
	      'autofocus' => $this->autofocus(),
	      'readonly' => $this->readonly(),
	      'disabled' => $this->disabled(),
	    ));
  	    
    $select = $select->append($this->option('', '', $this->value() == ''));

    
  	
  	if($this->readonly()) {
      $select->attr('tabindex', '-1');
    }

    if(kirby()->site()->event_types()) {
      $event_types = kirby()->site()->event_types()->toStructure();
    }
   
    foreach($event_types as $event_type) {
      $text = $event_type->event_name();
      $value = $event_type->event_code();
      $keys[] = $value;
      $values[] = $text;
    }
    $this->options = array_combine($keys, $values);
    
    foreach($this->options() as $value => $text) {
      $select->append($this->option($value, $text, $this->value() == $value));
    }

    $inner = new Brick('div');
    $inner->addClass('selectbox-wrapper');
    $inner->append($select);

    $wrapper = new Brick('div');
    $wrapper->addClass('input input-with-selectbox');
    $wrapper->append($inner);

    if($this->readonly()) {
      $wrapper->addClass('input-is-readonly');
    } else {
      $wrapper->attr('data-focus', 'true');
    }

    return $wrapper;
  	
  }

}

```

Note: This will probably fail if either the event\_type or event\_name fields are empty, so you would either have to require the fields to be filled in or handle missing fields entries in the code. The external API solution I suggested in this thread [Panel: querying "site.txt" structure fields as pages/subpages fields' values](http://forum.getkirby.com/t/panel-querying-site-txt-structure-fields-as-pages-subpages-fields-values/1960/10) is probably the better solution.

---

<div class="post-metadata">

**Author:** ![rhawbert](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rhawbert/32/586_2.png) [@rhawbert](https://forum.getkirby.com/u/rhawbert)\
**Post date:** [September 5, 2015, 1:58pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/14 "2015-09-05T13:58:27Z")

</div>

Hey, thanks again @texnixe, you’re a life saver.

This new code is giving me the same errors (imagem below)… I’m starting to doubt my local environment (php).

I’ve tried everything I could to fix it, but again… hit a wall ☹

![](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/1X/62cc738dd2898da299cca0b238f3f10f2b1b5d6b.png)

[edit] I didn’t invested much on the API solution you’ve mentioned because of the v::url() issue… and also because I didn’t wanted to fiddle with the core (scary stuff).

---

<div class="post-metadata">

**Author:** ![rhawbert](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rhawbert/32/586_2.png) [@rhawbert](https://forum.getkirby.com/u/rhawbert)\
**Post date:** [September 5, 2015, 3:06pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/15 "2015-09-05T15:06:19Z")

</div>

Haha, found it!

Instead of converting it to kirby objects via toStructure() like you’ve posted:

```php
if(kirby()->site()->event_types()) {
  $event_types = kirby()->site()->event_types()->toStructure();
}

```

I’ve kept them as arrays all the way, sacrificing the beauty of the kirby way by accessing the associative array directly.

```php
if(kirby()->site()-> event_types()) {
  $event_types = kirby()->site()->event_types()->yaml();
}

foreach($event_types as $event_type) {
  $text = $event_type['event_name'];
  $value = $event_type['event_code'];
  $keys[] = $value;
  $values[] = $text;
}

```

Your array manipulation was sound & solid @texnixe 😄

[edit] Also Regarding the “why using [‘event\_code’]”… just found out that when one changes the order in the structure field it changes the selected option in every page/subpage that uses it. So using the “code” will certify that the structure field’s content order won’t matter…

---

<div class="post-metadata">

**Author:** ![Caleb](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/caleb/32/2889_2.png) [@Caleb](https://forum.getkirby.com/u/Caleb)\
**Post date:** [March 21, 2017, 4:08pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/16 "2017-03-21T16:08:34Z")

</div>

I know it’s been awhile, but I’ve wrapped @texnixe’s code in a nice, portable, and customizable plugin:

> **[calebgrove/select-a-structure](https://github.com/CalebGrove/select-a-structure)**
>
> select-a-structure - A select field for Kirby populated by a structure field.

---

<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:** [March 21, 2017, 4:10pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/17 "2017-03-21T16:10:46Z")

</div>

Thanks for sharing ❤.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:50pm UTC](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/18 "2025-02-06T16:50:22Z")

</div>

Archived on February 6, 2025, 4:50pm UTC


