Two new options have been added for the Select form field in the form builder in Site Studio 7.5.0.
The previous two options have been renamed as described:
Manually set options - previously "Custom select field"
Options from existing select field - previously "Existing select field"
Options from external data source - new
Options from custom function - new
Selecting "Options from external data source" will display a new field "External URL". You must enter a url endpoint that returns JSON data in the correct format.
Selecting "Options from custom function" will display a new field "Function name". You must enter the name of a function that is globally available that returns the data in the correct format (or a promise that resolves to the correct data).
An example module (example_custom_select) is also included to further outline usage expectations.
Demo
Enable this module from /admin/modules
Drupal route
Create a new Component by navigating to: admin/cohesion/components/components/add
Add a Select field to the Component form builder
Double-click the new Select field and choose Options from external data source as the Type
Enter /sitestudio/select-options to use the Drupal route supplied by this example module
Observe the dynamic values supplied by this endpoint in the Default value options.
Javascript function
Create a new Component by navigating to: admin/cohesion/components/components/add
Add a Select field to the Component form builder
Double-click the new Select field and choose Options from custom function as the Type
Enter exampleGetOptions1 to use the Javascript function supplied by this example module
Observe the dynamic values supplied by this endpoint in the Default value options.
Example code
This module provides an example of both the Drupal and Javascript implementations of this feature.
Drupal route
Drupal route - defines an endpoint for returning JSON.
Note: an Event subscriber must be utilised to ensure a custom Javascript library is attached during Page Builder interaction.
JSON format
When writing a function to retrieve options for the Select form element, it is crucial to adhere to specific guidelines to ensure consistency and compatibility. This documentation provides a set of rules and examples for creating a valid options retrieval function.
Rules for a Valid Options Retrieval Function
Return Type:
The function must return an array of objects representing options.
Each object in the array should have a label property and a value property.
Two new options have been added for the Select form field in the form builder in Site Studio 7.5.0.
The previous two options have been renamed as described:
Manually set options - previously "Custom select field"
Options from existing select field - previously "Existing select field"
Options from external data source - new
Options from custom function - new
Selecting "Options from external data source" will display a new field "External URL". You must enter a url endpoint that returns JSON data in the correct format.
Selecting "Options from custom function" will display a new field "Function name". You must enter the name of a function that is globally available that returns the data in the correct format (or a promise that resolves to the correct data).
An example module (example_custom_select) is also included to further outline usage expectations.
Demo
Enable this module from /admin/modules
Drupal route
Create a new Component by navigating to: admin/cohesion/components/components/add
Add a Select field to the Component form builder
Double-click the new Select field and choose Options from external data source as the Type
Enter /sitestudio/select-options to use the Drupal route supplied by this example module
Observe the dynamic values supplied by this endpoint in the Default value options.
Javascript function
Create a new Component by navigating to: admin/cohesion/components/components/add
Add a Select field to the Component form builder
Double-click the new Select field and choose Options from custom function as the Type
Enter exampleGetOptions1 to use the Javascript function supplied by this example module
Observe the dynamic values supplied by this endpoint in the Default value options.
Example code
This module provides an example of both the Drupal and Javascript implementations of this feature.
Drupal route
Drupal route - defines an endpoint for returning JSON.
Note: an Event subscriber must be utilised to ensure a custom Javascript library is attached during Page Builder interaction.
JSON format
When writing a function to retrieve options for the Select form element, it is crucial to adhere to specific guidelines to ensure consistency and compatibility. This documentation provides a set of rules and examples for creating a valid options retrieval function.
Rules for a Valid Options Retrieval Function
Return Type:
The function must return an array of objects representing options.
Each object in the array should have a label property and a value property.