'Populate select box options of a objectbrick, dynamically
I have a class "groups", that have many objects. for example "All","Mobile","Watches".
We have select box in a objectbrick. we want to populate select box with the objects of "groups" class.
Solution 1:[1]
That is not possible out of the box but can be done with the help of this plugin: https://github.com/ThomasKeil/pimcore-plugin-DynamicDropdown
To install the plugin do these steps:
- within /plugins folder create a new folder called DynamicDropdown
- on this page https://github.com/ThomasKeil/pimcore-plugin-DynamicDropdown download the zip of the plugin
- extract contents of the zip to the folder created in the first step (so that the DynamicDropdown includes folders: controllers, lib, static, texts, views, ...)
- in the administration backend go to Tools (Wrench icon) > Extensions
- enable the plugin
- reload Pimcore backend
- go to ObjectBricks and add the DynamicDropdown field to the ObjectBrick
- in the field settings you should then pick the folder with objects that provide the options, the Object Class of the options and which method is used for the display values
I think the plugin config is pretty self-explanatory.
Solution 2:[2]
Pimcore X has this function out of the box. Just provide a class to 'Options Provider Class or Service Name:' property.
Sample:
public function getOptions($context, $fieldDefinition)
{
$object = isset($context["object"]) ? $context["object"] : null;
if (!$object) {
return [];
}
$groups = $object->getGroups();
$result = [];
foreach ($groups as $name => $group) {
$result[] = ["key" => $group->getTitle() . ' (' . $name . ')', "value" => $name];
}
return $result;
}
if you need to get all allowedObjectBricks, which are coupled with current object using the field 'someObjectBricks', then try the following:
$productBricks = $object->getSomeObjectBricks();
$objectBricksNames = $productBricks->getAllowedBrickTypes();
foreach ($objectBricksNames as $name) {
try {
$definitions = Objectbrick\Definition::getByKey($name);
} catch (\Exception $e) {
continue;
}
}
See the documentation https://pimcore.com/docs/pimcore/current/Development_Documentation/Objects/Object_Classes/Data_Types/Dynamic_Select_Types.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
