Skip to content

Commit

Permalink
feat(glpiselectfield): hook to allow plugins to declare their itemtypes
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed Jun 17, 2021
1 parent 0a1c267 commit 91926b2
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions inc/field/glpiselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ public function getDesignSpecializationField(): array {
$itemtype = $decodedValues['itemtype'];
}

// Get additional itemtypes from plugins
$additionalTypes = Plugin::doHookFunction('formcreator_get_glpi_object_types', []);
// Cleanup data from plugins
$cleanedAditionalTypes = [];
foreach ($additionalTypes as $groupName => $itemtypes) {
if (!is_string($groupName)) {
continue;
}
$cleanedAditionalTypes[$groupName] = [];
foreach ($itemtypes as $itemtype => $typeName) {
if (!class_exists($itemtype)) {
continue;
}
if (array_search($itemtype, $cleanedAditionalTypes[$groupName])) {
continue;
}
$cleanedAditionalTypes[$groupName][$itemtype] = $typeName;
}
}
// Merge new itemtypes to predefined ones
$optgroup = array_merge_recursive($optgroup, $cleanedAditionalTypes);

array_unshift($optgroup, '---');
$field = Dropdown::showFromArray('glpi_objects', $optgroup, [
'value' => $itemtype,
Expand Down Expand Up @@ -272,15 +294,6 @@ public function getObjects() {
$optgroup[__("Assets")][PluginDatabasesDatabase::class] = PluginDatabasesDatabase::getTypeName(2) . ' (' . _n('Plugin', 'Plugins', 1) . ')';
}

if ((new Plugin())->isActivated("genericobject")) {
$optgroup[__('Plugin') . " " . __('Objects management', 'genericobject')] = [];
$genericObjectType = new PluginGenericobjectType();
$genericObjectTypes = $genericObjectType->find();
foreach ($genericObjectTypes as $object) {
$optgroup[__('Plugin') . " " . __('Objects management', 'genericobject')][$object["itemtype"]] = $object["itemtype"]::getTypeName();
}
}

return $optgroup;
}
}

0 comments on commit 91926b2

Please sign in to comment.