Skip to content

Commit a51ce0a

Browse files
Add hook in ticket parameter to add and change new variables (#20880)
1 parent 333b13e commit a51ce0a

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Glpi/ContentTemplates/TemplateDocumentation.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
namespace Glpi\ContentTemplates;
3737

3838
use Glpi\ContentTemplates\Parameters\ParametersTypes\ParameterTypeInterface;
39+
use Glpi\Plugin\Hooks;
3940
use Glpi\Toolbox\MarkdownBuilder;
41+
use Plugin;
4042

4143
/**
4244
* Class used to build the twig variable documentation for a given set of
@@ -107,6 +109,8 @@ public function addSection(
107109
array $parameters,
108110
?string $fields_prefix = null
109111
) {
112+
global $PLUGIN_HOOKS;
113+
110114
// Check if this section is already defined, needed as some parameters
111115
// might have the same references
112116
if (isset($this->sections[$title])) {
@@ -130,6 +134,16 @@ public function addSection(
130134
// Keep track of parameters needing aditionnal references
131135
$references = [];
132136

137+
//additional parameters
138+
foreach (array_keys($PLUGIN_HOOKS[Hooks::GET_CONTENT_TEMPLATE_PARAMETER] ?? []) as $plugin) {
139+
$plugin_parameters = Plugin::doOneHook((string) $plugin, Hooks::GET_CONTENT_TEMPLATE_PARAMETER, $fields_prefix);
140+
if (is_array($plugin_parameters)) {
141+
foreach ($plugin_parameters as $plugin_parameter) {
142+
$parameters[] = $plugin_parameter;
143+
}
144+
}
145+
}
146+
133147
// Add a row for each parameters
134148
foreach ($parameters as $parameter) {
135149
/** @var ParameterTypeInterface $parameter */

src/Glpi/ContentTemplates/TemplateManager.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737

3838
use CommonITILObject;
3939
use Glpi\Error\ErrorHandler;
40+
use Glpi\Plugin\Hooks;
4041
use Glpi\RichText\RichText;
42+
use Plugin;
4143
use Twig\Environment;
4244
use Twig\Error\Error;
4345
use Twig\Error\SyntaxError;
@@ -107,14 +109,26 @@ public static function renderContentForCommonITIL(
107109
CommonITILObject $itil_item,
108110
string $template
109111
): ?string {
112+
global $PLUGIN_HOOKS;
113+
110114
$parameters = $itil_item->getContentTemplatesParametersClassInstance();
111115

116+
$parameters_value = $parameters->getValues($itil_item);
117+
foreach (array_keys($PLUGIN_HOOKS[Hooks::GET_CONTENT_TEMPLATE_VALUE] ?? []) as $plugin) {
118+
$plugin_parameters = Plugin::doOneHook((string) $plugin, Hooks::GET_CONTENT_TEMPLATE_VALUE, ['node_name' => $parameters->getDefaultNodeName(), 'id' => $itil_item->fields['id']]);
119+
if (is_array($plugin_parameters)) {
120+
foreach ($plugin_parameters as $key => $value) {
121+
$parameters_value[$key] = $value;
122+
}
123+
}
124+
}
125+
112126
try {
113127
$html = TemplateManager::render(
114128
$template,
115129
[
116130
'itemtype' => $itil_item::class,
117-
$parameters->getDefaultNodeName() => $parameters->getValues($itil_item),
131+
$parameters->getDefaultNodeName() => $parameters_value,
118132
]
119133
);
120134
} catch (Error $e) {

src/Glpi/Plugin/Hooks.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,20 @@ class Hooks
560560
*/
561561
public const SHOW_ITEM_STATS = 'show_item_stats';
562562

563+
/**
564+
* Register a function to display new variables in reply messages.
565+
* The function is called with the name of actual item.
566+
* The function is expected to return an array with parmeter.
567+
*/
568+
public const GET_CONTENT_TEMPLATE_PARAMETER = 'get_content_template_parameter';
569+
570+
/**
571+
* Register a function to display new variables in reply messages.
572+
* The function is called with type and id of the actuel item.
573+
* The function is expected to return an array with parameters values.
574+
*/
575+
public const GET_CONTENT_TEMPLATE_VALUE = 'get_content_template_value';
576+
563577
/**
564578
* Register a function to add additional permission restrictions for the item.
565579
* The function is called with the item as a parameter.
@@ -1398,6 +1412,8 @@ public static function getItemHooks(): array
13981412
self::POST_SHOW_TAB,
13991413
self::POST_PREPAREADD,
14001414
self::SHOW_ITEM_STATS,
1415+
self::GET_CONTENT_TEMPLATE_PARAMETER,
1416+
self::GET_CONTENT_TEMPLATE_VALUE,
14011417
self::TIMELINE_ACTIONS,
14021418
];
14031419
}

0 commit comments

Comments
 (0)