A. Pérez A. Pérez - 1 month ago 19
PHP Question

Drupal 8 YAML Form Module: Populate Select Element

I'm a newbie in Drupal and I'm trying to create custom forms in Drupal 8 using 'YAML Form' module:
https://www.drupal.org/project/yamlform

I have added some 'select' elements with some custom options from the Form elements page provided by the module, but I don't find the way to populate these elements from database.

How can I do it? Step by step if possible, please.

Thanks in advance!

Answer

I don't know if it's the best way to do it but it works for me:

I've created a custom module and in the '.module' file I've implemented the hook hook_yamlform_options_YAMLFORM_OPTIONS_ID_alter() when the part YAMLFORM_OPTIONS_ID is the ID of a custom set of options I've added in the Options page (admin/structure/yamlform/settings/options/manage). The code of the hook, basically, gets data from database and sets it in the $options array with the desired keys and values.

Then when I access to the Options page again, in the row of the new custom set of options, the column ALTERED has the value 'Yes'. Click to 'Edit' this row and then click to 'Reset' button. The result is that the options value/text table is updated automatically with the data returned from database.