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:
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.
Enable this module from /admin/modules
admin/cohesion/components/components/add
Select
field to the Component form builderOptions from external data source
as the Type
/sitestudio/select-options
to use the Drupal route supplied by this example moduleDefault value
options.admin/cohesion/components/components/add
Select
field to the Component form builderOptions from custom function
as the Type
exampleGetOptions1
to use the Javascript function supplied by this example moduleDefault value
options.This module provides an example of both the Drupal and Javascript implementations of this feature.
Note: an Event subscriber must be utilised to ensure a custom Javascript library is attached during Page Builder interaction.
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.
Return Type:
// Valid Example
[
{ label: 'Option 1', value: 'option1' },
{ label: 'Option 2', value: 'option2' },
// ...
]
Optional Grouping:
// Valid Example
[
{ label: 'Option 1', value: 'option1', group: 'Group A' },
{ label: 'Option 2', value: 'option2', group: 'Group B' },
// ...
]
Function Structure:
// Valid Example
const validOptionsFunction = function () {
return [
{ label: 'Option 1', value: 'option1' },
{ label: 'Option 2', value: 'option2' },
// ...
];
};
window.getOptions = validOptionsFunction;
window.exampleGetOptions = async function () {
return new Promise((resolve) => {
setTimeout(() => {
resolve([
{ label: 'Foo', value: 'foo' },
{ label: 'Bar', value: 'bar' },
{ label: 'Wut', value: 'wut', group: 'Other' },
]);
}, 1000); // Simulate a delay of 1 second
});
};
window.exampleGetOptions1 = function () {
return [
{ label: 'Foo', value: 'foo' },
{ label: 'Bar', value: 'bar' },
{ label: 'Wut', value: 'wut', group: 'Other' },
];
};
window.exampleGetOptions2 = function () {
return 'This is not an array of options.';
};
window.exampleGetOptions3 = [
{ label: 'this is not a function', value: 'this is not a function' },
];
window.exampleGetOptions4 = async function () {
return [
{ label: 'Foo', value: 'foo' },
{ label: 'Bar', value: 'bar' },
{ label: 'Wut', foo: 'Other' }, // Invalid property 'foo'
];
};
When retrieving options from a URL it is crucial that the data returned from the URL is in the correct format and is returned as JSON data.
Return Type:
[
{ "label": "Option 1", "value": "option1" },
{ "label": "Option 2", "value": "option2" }
]
Optional Grouping:
[
{ "label": "Option 1", "value": "option1", "group": "Group A" },
{ "label": "Option 2", "value": "option2", "group": "Group B" }
]
If this content did not answer your questions, try searching or contacting our support team for further assistance.
Mon Aug 18 2025 15:01:25 GMT+0000 (Coordinated Universal Time)