diff --git a/CHANGELOG.md b/CHANGELOG.md index bbd3c885..bf15da11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] + +### Fixed + +- Fix an error displayed on the UI when trying to create a bloc display condition based on "Type" or "Model" fields for custom assets + ## [1.24.3] - 2026-07-30 ### Fixed diff --git a/inc/containerdisplaycondition.class.php b/inc/containerdisplaycondition.class.php index ae621035..8c87b234 100644 --- a/inc/containerdisplaycondition.class.php +++ b/inc/containerdisplaycondition.class.php @@ -275,7 +275,9 @@ public static function showSearchOptionCondition($searchoption_id, $itemtype, ?s if ($so['datatype'] == 'dropdown' || ($so['datatype'] == 'itemlink' && $so['table'] !== $itemtypetable)) { $twig_params['is_dropdown'] = true; - $twig_params['dropdown_itemtype'] = getItemTypeForTable($so['table']); + //No need to call getItemTypeForTable if we are in the case of a custom asset + $twig_params['dropdown_itemtype'] = $so['itemtype'] ?? getItemTypeForTable($so['table']); + $twig_params['list_conditions'] = self::getComparisonOperators( true, is_a($twig_params['dropdown_itemtype'], CommonTreeDropdown::class, true), @@ -324,7 +326,9 @@ public static function getRawValue($searchoption_id, $itemtype, $value) $raw_value = ''; if ($so['datatype'] == 'dropdown' || ($so['datatype'] == 'itemlink' && $so['table'] !== $itemtypetable)) { - $dropdown_itemtype = getItemTypeForTable($so['table']); + //No need to call getItemTypeForTable if we are in the case of a custom asset + $dropdown_itemtype = $so['itemtype'] ?? getItemTypeForTable($so['table']); + $dbu = new DbUtils(); $dropdown = $dbu->getItemForItemtype($dropdown_itemtype); if ($dropdown->getFromDB($value)) { diff --git a/tests/Units/ContainerDisplayConditionTest.php b/tests/Units/ContainerDisplayConditionTest.php new file mode 100644 index 00000000..ee25d270 --- /dev/null +++ b/tests/Units/ContainerDisplayConditionTest.php @@ -0,0 +1,84 @@ +. + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2013-2023 by Fields plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/fields + * ------------------------------------------------------------------------- + */ + +declare(strict_types=1); + +namespace GlpiPlugin\Field\Tests\Units; + +use Glpi\Tests\DbTestCase; +use Glpi\Tests\GLPITestCase; +use PluginFieldsContainerDisplayCondition; +use Search; + +final class ContainerDisplayConditionTest extends DbTestCase +{ + public function setUp(): void + { + GLPITestCase::setUp(); + $this->login(); + } + + public function tearDown(): void + { + GLPITestCase::tearDown(); + } + + public function testGetRawValueOnCustomAssetTypeAndModel(): void + { + $definition = $this->initAssetDefinition(); + $asset_class = $definition->getAssetClassName(); + + $type = $this->createItem($definition->getAssetTypeClassName(), ['name' => 'Laptop']); + $model = $this->createItem($definition->getAssetModelClassName(), ['name' => 'ProBook']); + + $type_table = $definition->getAssetTypeClassName()::getTable(); + $model_table = $definition->getAssetModelClassName()::getTable(); + + $type_so_id = null; + $model_so_id = null; + foreach (Search::getOptions($asset_class) as $so_id => $so) { + if (($so['table'] ?? null) === $type_table) { + $type_so_id = $so_id; + } + + if (($so['table'] ?? null) === $model_table) { + $model_so_id = $so_id; + } + } + + ob_start(); + PluginFieldsContainerDisplayCondition::getRawValue($type_so_id, $asset_class, $type->getID()); + $this->assertSame('Laptop', ob_get_clean()); + + ob_start(); + PluginFieldsContainerDisplayCondition::getRawValue($model_so_id, $asset_class, $model->getID()); + $this->assertSame('ProBook', ob_get_clean()); + } +}