Need and initial considerations
When creating a field that provides several list of known options (i.e. status, etc), in drupal, there are several options available:
-
Static "Select list"
Pros: Native. No development except the list creation while defining the entity form.
Simple access (views, etc), option data available directly at the entity storage.
Cons: Not reusable: Hard coded on entity definition at every form (or webform) it has.
Static: Requires a drupal developer/admin to change the options list at field form properties. -
Using a custom entity reference
Pros: Native & dynamic, list of values can be updated at any time by any privileged user, without having to be a developer.
Reusable on other fields & entities.
Highly extensible.Cons: Requires object storage / data model extension by creating a new entity type for every select list.
Object storage has the pointer to the relation, but not the data itself. Views and data access a bit more complex (require define relations). -
Dynamic select list.
Pros: Dynamic, list of values can be updated at any time by any privileged user, without having to be a developer.Reusable on other fields & entities.
Object storage contains the data without having to add relations.
Cons: Requires some custom development (not drupal native).
Implementing a Dynamic Select List (type 3)
The Dynamic Select list relies on "guifi_options" Entity type, so the first action is add the list of options into that table, with every key & title pair, and groped by a type.
To browse the guifi_options content named "select_options" available at the url guifimgmt/admin/options. On that view you can restrict to any option type, and get the list of options for that type.
Once introduced the list of options you want, and to add the dynamic select list to the forms:
- Drupal content type fields.
-
Create the field, and as for field storage, select "Select list".
-
Create the query function at the module: Ex. (guifi.module)
function imgroup_select(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
$database = \Drupal::database();
"SELECT field_key_value, title
FROM {guifi__field_type} t, {guifi__field_weight} w, {guifi_field_data} l, {guifi__field_key} k
WHERE t.field_type_value = 'im_groups'
AND t.entity_id = w.entity_id AND w.entity_id=l.id
AND w.entity_id=k.entity_id AND
w.bundle='guifi_options'\
AND l.bundle='guifi_options' AND k.bundle='guifi_options'\
ANDw.entity_id=l.id AND w.entity_id=k.entity_id
ORDER BY w.field_weight_value, title");
$result = $query->fetchAllKeyed(0,1);\
return $result;\
}` -
Export yaml definition for the field created by going to "export configuration" for the field storage you just created at /admin/config/development/configuration/single/export
-
At the .yml obtained at the step above find "allowed_values_function: " section and specify the function you created in the module at step 2. Ex.:
allowed_values_function: im_groups
-
- Dynamic Select List using Webforms:
- Add element field,use "remote select" field type. Once you edit the properties, specify:
- The endpoint provided by the mentioned above view. Example:
http://127.0.0.1/api/v1/select_options?option_type=execution_status&_format=json - "Headers":
{'Content-Type': 'application/json'}
- "Response items key":
key
- "Response items value":
title
- The endpoint provided by the mentioned above view. Example:
- Add element field,use "remote select" field type. Once you edit the properties, specify: