From 10d050364a9ef02236533e1fc6b8dc1e74f06916 Mon Sep 17 00:00:00 2001 From: a_evrard Date: Tue, 2 Sep 2025 08:20:16 +0200 Subject: [PATCH 1/2] Add hook in ticket parameter to add and change new variables --- .../Parameters/TicketParameters.php | 8 ++++++-- src/Glpi/Plugin/Hooks.php | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Glpi/ContentTemplates/Parameters/TicketParameters.php b/src/Glpi/ContentTemplates/Parameters/TicketParameters.php index 8512d8430f5..6f3ba9e0607 100644 --- a/src/Glpi/ContentTemplates/Parameters/TicketParameters.php +++ b/src/Glpi/ContentTemplates/Parameters/TicketParameters.php @@ -39,11 +39,13 @@ use Glpi\ContentTemplates\Parameters\ParametersTypes\ArrayParameter; use Glpi\ContentTemplates\Parameters\ParametersTypes\AttributeParameter; use Glpi\ContentTemplates\Parameters\ParametersTypes\ObjectParameter; +use Glpi\Plugin\Hooks; use Item_Ticket; use KnowbaseItem; use KnowbaseItem_Item; use Location; use OLA; +use Plugin; use RequestType; use Session; use SLA; @@ -74,7 +76,7 @@ protected function getTargetClasses(): array public function getAvailableParameters(): array { - return array_merge(parent::getAvailableParameters(), [ + $availableParameters = array_merge(parent::getAvailableParameters(), [ new AttributeParameter("type", _n('Type', 'Types', 1)), new AttributeParameter("global_validation", _n('Approval', 'Approvals', 1)), new AttributeParameter("tto", __('Time to own'), 'date("d/m/y H:i")'), @@ -88,6 +90,8 @@ public function getAvailableParameters(): array new ArrayParameter("knowbaseitems", new KnowbaseItemParameters(), KnowbaseItem_Item::getTypeName(Session::getPluralNumber())), new ArrayParameter("assets", new AssetParameters(), Item_Ticket::getTypeName(Session::getPluralNumber())), ]); + + return Plugin::doHookFunction(Hooks::SHOW_ITEM_RESP_PARAM, $availableParameters); } protected function defineValues(CommonDBTM $ticket): array @@ -161,6 +165,6 @@ protected function defineValues(CommonDBTM $ticket): array } } - return $values; + return Plugin::doHookFunction(Hooks::SHOW_ITEM_RESP_VALUE, $values); } } diff --git a/src/Glpi/Plugin/Hooks.php b/src/Glpi/Plugin/Hooks.php index 61f03a14929..473f5637057 100644 --- a/src/Glpi/Plugin/Hooks.php +++ b/src/Glpi/Plugin/Hooks.php @@ -565,6 +565,20 @@ class Hooks */ public const SHOW_ITEM_STATS = 'show_item_stats'; + /** + * Register a function to display new variables in reply messages. + * The function is called with the array of available variables as a parameter. + * The function is expected to return new array of available variables. + */ + public const SHOW_ITEM_RESP_PARAM = 'show_item_resp_param'; + + /** + * Register a function to display new variables in reply messages. + * The function is called with the array of available variables as a parameter. + * The function is expected to return new array of available variables. + */ + public const SHOW_ITEM_RESP_VALUE = 'show_item_resp_value'; + /** * Register a function to add additional permission restrictions for the item. * The function is called with the item as a parameter. @@ -1408,6 +1422,8 @@ public static function getItemHooks(): array self::POST_SHOW_TAB, self::POST_PREPAREADD, self::SHOW_ITEM_STATS, + self::SHOW_ITEM_RESP_PARAM, + self::SHOW_ITEM_RESP_VALUE, self::TIMELINE_ACTIONS, ]; } From f3026a028f158ad9d785c171e4aeb7621d605a58 Mon Sep 17 00:00:00 2001 From: a_evrard Date: Thu, 30 Oct 2025 09:28:51 +0100 Subject: [PATCH 2/2] Extend new parameter in answer template to generic way --- .../Parameters/TicketParameters.php | 8 ++------ .../ContentTemplates/TemplateDocumentation.php | 13 +++++++++++++ src/Glpi/ContentTemplates/TemplateManager.php | 15 ++++++++++++++- src/Glpi/Plugin/Hooks.php | 16 ++++++++-------- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/Glpi/ContentTemplates/Parameters/TicketParameters.php b/src/Glpi/ContentTemplates/Parameters/TicketParameters.php index 6f3ba9e0607..8512d8430f5 100644 --- a/src/Glpi/ContentTemplates/Parameters/TicketParameters.php +++ b/src/Glpi/ContentTemplates/Parameters/TicketParameters.php @@ -39,13 +39,11 @@ use Glpi\ContentTemplates\Parameters\ParametersTypes\ArrayParameter; use Glpi\ContentTemplates\Parameters\ParametersTypes\AttributeParameter; use Glpi\ContentTemplates\Parameters\ParametersTypes\ObjectParameter; -use Glpi\Plugin\Hooks; use Item_Ticket; use KnowbaseItem; use KnowbaseItem_Item; use Location; use OLA; -use Plugin; use RequestType; use Session; use SLA; @@ -76,7 +74,7 @@ protected function getTargetClasses(): array public function getAvailableParameters(): array { - $availableParameters = array_merge(parent::getAvailableParameters(), [ + return array_merge(parent::getAvailableParameters(), [ new AttributeParameter("type", _n('Type', 'Types', 1)), new AttributeParameter("global_validation", _n('Approval', 'Approvals', 1)), new AttributeParameter("tto", __('Time to own'), 'date("d/m/y H:i")'), @@ -90,8 +88,6 @@ public function getAvailableParameters(): array new ArrayParameter("knowbaseitems", new KnowbaseItemParameters(), KnowbaseItem_Item::getTypeName(Session::getPluralNumber())), new ArrayParameter("assets", new AssetParameters(), Item_Ticket::getTypeName(Session::getPluralNumber())), ]); - - return Plugin::doHookFunction(Hooks::SHOW_ITEM_RESP_PARAM, $availableParameters); } protected function defineValues(CommonDBTM $ticket): array @@ -165,6 +161,6 @@ protected function defineValues(CommonDBTM $ticket): array } } - return Plugin::doHookFunction(Hooks::SHOW_ITEM_RESP_VALUE, $values); + return $values; } } diff --git a/src/Glpi/ContentTemplates/TemplateDocumentation.php b/src/Glpi/ContentTemplates/TemplateDocumentation.php index f68df134cc2..7e3d10f6d22 100644 --- a/src/Glpi/ContentTemplates/TemplateDocumentation.php +++ b/src/Glpi/ContentTemplates/TemplateDocumentation.php @@ -36,7 +36,9 @@ namespace Glpi\ContentTemplates; use Glpi\ContentTemplates\Parameters\ParametersTypes\ParameterTypeInterface; +use Glpi\Plugin\Hooks; use Glpi\Toolbox\MarkdownBuilder; +use Plugin; /** * Class used to build the twig variable documentation for a given set of @@ -110,6 +112,7 @@ public function addSection( ) { // Check if this section is already defined, needed as some parameters // might have the same references + global $PLUGIN_HOOKS; if (isset($this->sections[$title])) { return; } @@ -131,6 +134,16 @@ public function addSection( // Keep track of parameters needing aditionnal references $references = []; + //additional parameters + foreach ($PLUGIN_HOOKS[Hooks::GET_CONTENT_TEMPLATE_PARAMETER] as $plugin => $hook_callable) { + $plugin_parameters = Plugin::doOneHook($plugin, Hooks::GET_CONTENT_TEMPLATE_PARAMETER, $fields_prefix); + if (is_array($plugin_parameters)) { + foreach ($plugin_parameters as $plugin_parameter) { + array_push($parameters, $plugin_parameter); + } + } + } + // Add a row for each parameters foreach ($parameters as $parameter) { /** @var ParameterTypeInterface $parameter */ diff --git a/src/Glpi/ContentTemplates/TemplateManager.php b/src/Glpi/ContentTemplates/TemplateManager.php index b844a6e95f9..85422ee9c6f 100644 --- a/src/Glpi/ContentTemplates/TemplateManager.php +++ b/src/Glpi/ContentTemplates/TemplateManager.php @@ -37,7 +37,9 @@ use CommonITILObject; use Glpi\Error\ErrorHandler; +use Glpi\Plugin\Hooks; use Glpi\RichText\RichText; +use Plugin; use Twig\Environment; use Twig\Error\Error; use Twig\Error\SyntaxError; @@ -107,14 +109,25 @@ public static function renderContentForCommonITIL( CommonITILObject $itil_item, string $template ): ?string { + global $PLUGIN_HOOKS; $parameters = $itil_item->getContentTemplatesParametersClassInstance(); + $parameters_value = $parameters->getValues($itil_item); + foreach ($PLUGIN_HOOKS[Hooks::GET_CONTENT_TEMPLATE_VALUE] as $plugin => $hook_callable) { + $plugin_parameters = Plugin::doOneHook($plugin, Hooks::GET_CONTENT_TEMPLATE_VALUE, ['NodeName' => $parameters->getDefaultNodeName(), 'id' => $itil_item->fields['id']]); + if (is_array($plugin_parameters)) { + foreach ($plugin_parameters as $key => $value) { + $parameters_value[$key] = $value; + } + } + } + try { $html = TemplateManager::render( $template, [ 'itemtype' => $itil_item->getType(), - $parameters->getDefaultNodeName() => $parameters->getValues($itil_item), + $parameters->getDefaultNodeName() => $parameters_value, ] ); } catch (Error $e) { diff --git a/src/Glpi/Plugin/Hooks.php b/src/Glpi/Plugin/Hooks.php index 473f5637057..b8d29886f38 100644 --- a/src/Glpi/Plugin/Hooks.php +++ b/src/Glpi/Plugin/Hooks.php @@ -567,17 +567,17 @@ class Hooks /** * Register a function to display new variables in reply messages. - * The function is called with the array of available variables as a parameter. - * The function is expected to return new array of available variables. + * The function is called with the name of actual item. + * The function is expected to return an array with parmeter. */ - public const SHOW_ITEM_RESP_PARAM = 'show_item_resp_param'; + public const GET_CONTENT_TEMPLATE_PARAMETER = 'get_content_template_parameter'; /** * Register a function to display new variables in reply messages. - * The function is called with the array of available variables as a parameter. - * The function is expected to return new array of available variables. + * The function is called with type and id of the actuel item. + * The function is expected to return an array with parameters values. */ - public const SHOW_ITEM_RESP_VALUE = 'show_item_resp_value'; + public const GET_CONTENT_TEMPLATE_VALUE = 'get_content_template_value'; /** * Register a function to add additional permission restrictions for the item. @@ -1422,8 +1422,8 @@ public static function getItemHooks(): array self::POST_SHOW_TAB, self::POST_PREPAREADD, self::SHOW_ITEM_STATS, - self::SHOW_ITEM_RESP_PARAM, - self::SHOW_ITEM_RESP_VALUE, + self::GET_CONTENT_TEMPLATE_PARAMETER, + self::GET_CONTENT_TEMPLATE_VALUE, self::TIMELINE_ACTIONS, ]; }