|
|
## 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:
|
|
|
|
|
|
1. **_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.
|
|
|
2. **_Create a new 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).
|
|
|
3. **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](https://network.guifi.net/en/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:
|
|
|
|
|
|
1. Drupal content type fields.
|
|
|
1. Create the field, and as for field storage, select "Select list".
|
|
|
2. Create the query function at the module: Ex. (guifi.module)\
|
|
|
`function imgroup_select(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {`
|
|
|
|
|
|
` $database = \Drupal::database();`\
|
|
|
` ` $query = $database->query(`"SELECT field_key_value, title `\
|
|
|
<span dir="">FROM {guifi__field_type} t, {guifi__field_weight} w, {guifi_field_data} l, {guifi__field_key} k </span>\
|
|
|
`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' AND\
|
|
|
`w.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;\
|
|
|
}
|
|
|
3. 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](/admin/config/development/configuration/single/export)
|
|
|
4. 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`
|
|
|
2. Dynamic Select List using [Webforms](https://www.drupal.org/project/webform):
|
|
|
1. Add element field,use "[remote select](https://www.drupal.org/project/webform_remote_select)" field type. Once you edit the properties, specify:
|
|
|
1. The endpoint provided by the mentioned above view. Example:\
|
|
|
<http://127.0.0.1/api/v1/select_options?option_type=execution_status&_format=json>
|
|
|
2. "Headers":
|
|
|
|
|
|
`{'Content-Type': 'application/json'}`
|
|
|
3. "Response items key":
|
|
|
|
|
|
key
|
|
|
4. "Response items value":
|
|
|
|
|
|
title |
|
|
\ No newline at end of file |