Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Allow multiple fields in optionkey #10

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ fieldname:
type: selectastructure
structurepage: staffpage
structurefield: stafflist
optionkey: staffname
optionid: uniqueId
optionkey: staffname, staffsurname
optiondivider: ','
~~~~

You'll want to change these required options:
Expand All @@ -38,8 +40,19 @@ You'll want to change these required options:
<dt><code>structurefield:</code></dt>
<dd>The name of the structure field found on <code>structurepage</code>.</dd>

<dt><code>optionid:</code></dt>
<dd>The name of the field inside that structure field. It must contain unique values for each entry! It could be integers or hashes, or even your custom unique IDs—as long as they are unique!</dd>

<dt><code>optionkey:</code></dt>
<dd>The name of the field <strong>inside</strong> the structure used for the options in the select field.</dd>
<dd>The name of the field(s) <strong>inside</strong> the structure used for the options in the select field. May be a comma-separated list of fields, too.</dd>

</dl>

There's one additional option that is not required:

<dl>
<dt><code>optiondivider:</code></dt>
<dd>Divider between different fields in the option in the select field. Use apostrophes to allow spaces in the divider, e.g. <code>', '</code> to get comma and a space after it. Defaults to <code> — </code>.</dd>
</dl>

## Usage
Expand Down
19 changes: 15 additions & 4 deletions fields/selectastructure/selectastructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ public function input() {
else {
$structurepage = page($this->structurepage());
}
// Below lines need serious refractoring
$structurefield = $this->structurefield();
$structurefield = $structurepage->$structurefield();
$optionkey = $this->optionkey();
$optionid = $this->optionid();
$optionkey = str::split($this->optionkey());
$optiondivider = $this->optiondivider() != null ? $this->optiondivider() : ', ';

// If the strucure field exists, toStrucure() it.
if($this->page($structurepage)->field($structurefield)) {
Expand All @@ -69,13 +72,21 @@ public function input() {

// Build the list of options.
foreach($structure as $entry) {
$entry = $entry->$optionkey();
$this->options[] = $entry;
$string = '';
foreach($optionkey as $key) {
if($entry->$key() != '') {
$string = $string . $optiondivider . $entry->$key();
}
}
if($entry->$optionid() != '') {
$optionidvalue = $entry->$optionid();
}
$this->options[$optionidvalue->value()] = str::substr($string, str::length($optiondivider)); // remove the unecessary divider in the beginning
}

// Add the options to the select field.
foreach($this->options() as $value => $text) {
$select->append($this->option($text, $text, $this->value() == $text));
$select->append($this->option($value, $text, $this->value() == $value)); // $this->value() == $value instead of $this->value() == $text
}

$inner = new Brick('div');
Expand Down
Empty file modified select-a-structure.php
100755 → 100644
Empty file.