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(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 is probably the better solution.