Fetch query from parent page

Try this

<?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.

1 Like