Hi Kirby pros,
I came up with an idea for an easy-to-use solution for my project. Scenario
I have a unpublished page with a collection of addresses and these addresses can be changed or new items added comfortable via Panel. On Panel for the public page the editor have a drop-down field with e.g. the name and then I can print out the address on the page. Question
Is it possible to show (part of) content from a page into the panel from another page?
Info
Without hundreds of lines of code ’cause I’m a frond-end guy without real PHP skills. Unfortunately.
This is no problem. The solution depends on your setup. How do you want to add the collection of addresses? As children of “addresses” or as a structure field?
If you use a structure field, you would need a custom field to query the items from it into your select. If you use child pages, you don’t.
I’ve set up a structured field and I already have an ”special field“. But I’ve no idea for a scripted/dynamic connection between the list and the select. My stupid and un-dynamic solution is at the moment to put the surname into the ”special field“ and the exact same list of surnames for the select. Not ideal.
Pages doesn’t make sense 'cause of the type of content. 30 – 40 addresses are much more easily to maintain as a compact list.
Ok, a custom field that fetches the entries from the structure field could look like this (this is just a simple copy of the select field with a modification that fetches the entries from a page called “addresses”, you would have to adapt this to your page name):
<?php
class AddressesField extends BaseField {
public function __construct() {
$this->type = 'addresses';
$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(page('addresses')->addresses()) {
$addresses = page('addresses')->addresses()->toStructure();
}
foreach($addresses as $address) {
$name = $address->addr_name();
$this->options[] = $name;
}
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;
}
}
In your blueprint with the select, you would then use this field instead of the select field:
If you want only the last name, you would need to split the field values in the custom field first. But maybe it’s better to have both first and last name to select from, in case the last names are not unique, anyway.