├── README.md ├── fields └── selectastructure │ └── selectastructure.php ├── package.json ├── select-a-structure.gif └── select-a-structure.php /README.md: -------------------------------------------------------------------------------- 1 | # Select-A-Structure 2 | 3 | This is a select field for [Kirby](https://getkirby.com) that pulls it's options from a specified structure field found on _any_ page of the website. Credit to [Sonja](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/6) for the intial version, I just made it a little more awesome and packaged it as a plugin. 4 | 5 | ![](select-a-structure.gif) 6 | 7 | ## Installation 8 | 9 | Clone or [download](https://github.com/CalebGrove/select-a-structure/archive/master.zip) this repo, and rename the folder to `select-a-structure`. Move it into your `site/plugins` folder (if that folder doesn't exist, create it). 10 | 11 | If you are using the [Kirby CLI](https://github.com/getkirby/cli), you can install it using this command: 12 | 13 | ~~~~ 14 | kirby plugin:install CalebGrove/select-a-structure 15 | ~~~~ 16 | 17 | ## Setup 18 | 19 | First, create your structure field anywhere on the website and populate it. 20 | 21 | Then, put this into your blueprint where you want the select field to appear: 22 | 23 | ~~~~ yaml 24 | fieldname: 25 | label: Field Label 26 | type: selectastructure 27 | structurepage: staffpage 28 | structurefield: stafflist 29 | optionkey: staffname 30 | ~~~~ 31 | 32 | You'll want to change these required options: 33 | 34 |
35 |
structurepage: 36 |
The URI of the page that hosts the structure field. You can use subpages too, like this: page/subpage. The current page can by selected by leaving this blank. To point to the site() page, use /
37 | 38 |
structurefield:
39 |
The name of the structure field found on structurepage.
40 | 41 |
optionkey:
42 |
The name of the field inside the structure used for the options in the select field.
43 |
44 | 45 | ## Usage 46 | 47 | Use in your templates just like any select field. The data stored by the field exactly matches the text shown in the options. 48 | 49 | _And you're ready to rock and roll!_ 50 | -------------------------------------------------------------------------------- /fields/selectastructure/selectastructure.php: -------------------------------------------------------------------------------- 1 | type = 'selectastructure'; 12 | $this->icon = 'chevron-down'; 13 | $this->label = 'category'; 14 | $this->options = array(); 15 | 16 | } 17 | 18 | public function options() { 19 | return FieldOptions::build($this); 20 | } 21 | 22 | public function option($value, $text, $selected = false) { 23 | return new Brick('option', $this->i18n($text), array( 24 | 'value' => $value, 25 | 'selected' => $selected 26 | )); 27 | } 28 | 29 | public function input() { 30 | $select = new Brick('select'); 31 | $select->addClass('selectbox'); 32 | $select->attr(array( 33 | 'name' => $this->name(), 34 | 'id' => $this->id(), 35 | 'required' => $this->required(), 36 | 'autocomplete' => $this->autocomplete(), 37 | 'autofocus' => $this->autofocus(), 38 | 'readonly' => $this->readonly(), 39 | 'disabled' => $this->disabled(), 40 | )); 41 | 42 | $default = $this->default(); 43 | 44 | if(!$this->required()) { 45 | $select->append($this->option('', '', $this->value() == '')); 46 | } 47 | 48 | if($this->readonly()) { 49 | $select->attr('tabindex', '-1'); 50 | } 51 | 52 | // First, let's pull the page and field from the blueprint. 53 | if($this->structurepage() == '/') { 54 | $structurepage = site(); 55 | } else if(!$this->structurepage()) { 56 | $structurepage = $this->page(); 57 | } 58 | else { 59 | $structurepage = page($this->structurepage()); 60 | } 61 | $structurefield = $this->structurefield(); 62 | $structurefield = $structurepage->$structurefield(); 63 | $optionkey = $this->optionkey(); 64 | 65 | // If the strucure field exists, toStrucure() it. 66 | if($this->page($structurepage)->field($structurefield)) { 67 | $structure = $structurefield->toStructure(); 68 | } 69 | 70 | // Build the list of options. 71 | foreach($structure as $entry) { 72 | $entry = $entry->$optionkey(); 73 | $this->options[] = $entry; 74 | } 75 | 76 | // Add the options to the select field. 77 | foreach($this->options() as $value => $text) { 78 | $select->append($this->option($text, $text, $this->value() == $text)); 79 | } 80 | 81 | $inner = new Brick('div'); 82 | $inner->addClass('selectbox-wrapper'); 83 | $inner->append($select); 84 | 85 | $wrapper = new Brick('div'); 86 | $wrapper->addClass('input input-with-selectbox'); 87 | $wrapper->append($inner); 88 | 89 | if($this->readonly()) { 90 | $wrapper->addClass('input-is-readonly'); 91 | } else { 92 | $wrapper->attr('data-focus', 'true'); 93 | } 94 | 95 | return $wrapper; 96 | 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "select-a-structure", 3 | "description": "A select field for Kirby populated by a structure field.", 4 | "author": "Caleb Grove ", 5 | "version": "1.2.0", 6 | "type": "kirby-plugin", 7 | "license": "MIT" 8 | } 9 | -------------------------------------------------------------------------------- /select-a-structure.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/calebgrove/select-a-structure/a56c60a0b66b5b3b720e2160770d92849ef42af9/select-a-structure.gif -------------------------------------------------------------------------------- /select-a-structure.php: -------------------------------------------------------------------------------- 1 | set('field', 'selectastructure', __DIR__ . DS . 'fields' . DS . 'selectastructure'); 3 | --------------------------------------------------------------------------------