Default valueadmin/cohesion/components/components/addSelect field to the Component form builderOptions from custom function as the TypeexampleGetOptions1 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.