Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Populate a selectize field with taxonomy options

admin form

Solved by pamtbaau View solution

Started by nix 3 years ago · 2 replies · 403 views
3 years ago

Hello everyone,

I tried to implement an automatic option populating based on the method proposed here. I added a function in my theme.php and use it in a custom blueprint.
The function is:

PHP
  public static function taxonomyValues(string $taxon, string $fieldType)
  {
    /** @var Grav */
    $grav = Grav::instance();

    /** @var Admin */
    $admin = $grav['admin'];
    $admin->enablePages();

    /** @var Taxonomy */
    $taxonomy = $grav['taxonomy'];

    $keys = $taxonomy->getTaxonomyItemKeys($taxon);

    $values = [];

    foreach ($keys as $key) {
      if ($fieldType === 'select') {
        $values[$key] = $key;
      } else {
        $values[] = [
          'text' => $key,
          'value' => $key
        ];
      }
    }
    dump($values);
    return $values;
  }

It works fine with select, but it doesn't seem to work with selectize. My yaml look like:

YAML
header.test:
  type: selectize
  autocomplete: on
  style: vertical
  label: test
  data-options@: ['\Grav\Theme\NixTemplate::taxonomyValues', 'artiste','selectize']
  validate:
    type: commalist

If i dump the $values in the php, I can see an "array of array":

JS
array:2 [  0 => array:2 [    "text" => "artiste1"
    "value" => "artiste1"
  ]
  1 => array:2 [    "text" => "artiste2"
    "value" => "artiste2"
  ]
]

But if i click in the field in the admin, I can't see any suggestion of completion, do you know what I am doing wrong?

3 years ago Solution

@nix, I think the field definition isn't correct. Have a look at the example for the Selectize field in the docs.

Pay attention to how the options attribute is indended below selectize. The data-options@ should replace the options field in the exact same position.

3 years ago

Thanks that was it, I missed the selectize. Here is the working yaml:

YAML
header.test:
  type: selectize
  selectize:
    data-options@: ['\Grav\Theme\NixholTemplate::taxonomyValues', 'artiste','selectize']
  autocomplete: on
  style: vertical
  label: test
  validate:
    type: commalist

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 57 14 hours ago
Support · by Anna, 3 days ago
2 65 17 hours ago
Support · by Justin Young, 18 hours ago
1 33 18 hours ago
Support · by Duc , 1 week ago
2 68 5 days ago
Support · by Colin Hume, 1 week ago
2 60 6 days ago