Skip to content

Feat: use the fields in notifications and followup templates - #1363

Open
cgrard wants to merge 2 commits into
pluginsGLPI:mainfrom
cgrard:feature/notification-tags
Open

cgrard wants to merge 2 commits into
pluginsGLPI:mainfrom
cgrard:feature/notification-tags

Conversation

@cgrard

@cgrard cgrard commented Nov 19, 2026 •

Copy link
Copy Markdown

Checklist before requesting a review

  • I have performed a self-review of my code.
  • I have added tests (when available) that prove my fix is effective or that my feature works.
  • I have updated the CHANGELOG with a short functional description of the fix or new feature.
  • This change requires a documentation update (README updated).

Description

Fields values cannot be used in the texts generated by GLPI: notification templates and followup / task / solution templates. This has been requested several times (#359, #1184) and was started in #943, a draft limited to the "Insertion in the form" blocks of tickets, without value formatting, labels, rights or tests, which has not been updated for more than a year.

This PR starts again from main, independently of #941, and covers the points raised in the review of #943: every field type is rendered as displayed in the form, labels follow the recipient language, and the behaviour is covered by tests.

Notification templates

For the items having notifications (tickets, changes, problems, projects, etc.), two tags per active field, listed in the "Tags" tab of the templates:

  • ##fields.<block>.<field>##: value as displayed in the form: dropdown label (also multiple dropdowns and GLPI item dropdowns), Yes / No, date and date-time in the recipient format, linked item name, rich text;
  • ##lang.fields.<block>.<field>##: field label, translated in the recipient language with the existing label translations.

<block> and <field> are the system names, so templates do not break when a label is renamed. Header fields and inactive fields / blocks have no tag.

Rights. As GLPI does for private followups, a value is only sent to a recipient whose profile can read the block (right READ or more on the block, profile in the entity of the item). It is empty for a recipient without GLPI account (requester email address). The label is always sent.

GLPI reads additionnaloption['users_id'] to fill recipient_data for the item_get_datas hook, but does not set it. The plugin sets it from the add_recipient_to_target hook, so the template data is computed by recipient for the items having fields.

Followup, task and solution templates

Variables of the fields of tickets, changes and problems, and of the assets linked to a ticket:

{{ ticket.fields.<block>.<field> }}
{% for asset in ticket.assets %}{{ asset.name }}: {{ asset.fields.<block>.<field> }}{% endfor %}

They are listed in the documentation of the template variables, with the nl2br / raw filter for multiple lines text and rich text, through the existing get_content_template_parameter / get_content_template_value hooks.

As in the item form, these values are only rendered if the profile of the user applying the template can read the block: ajax/itilfollowup.php renders any template for a user who can read the ticket, including a requester. When the template is applied without right checks, as forms do when they create the ticket (Session::callAsSystem()), or by the cron (followups and reminders of pending reasons, sent to the requester), every value is rendered: the administrator chose the template. A typical use is a form submitted by a requester that adds a private followup with the administration data of the chosen server, for the technicians.

ticket.assets is built as TicketParameters::defineValues() does, with the fields in addition, since the values returned by a plugin replace the GLPI ones. This is only done when an asset type has blocks.

Points to discuss

  • GLPI has no hook to declare notification tags: they are added from item_add_targets, which is run by the NotificationTarget constructor, so they are listed in the "Tags" tab. A dedicated hook in GLPI would be cleaner.
  • The variables are not proposed by the {{ autocompletion of the template editor, which only uses the GLPI parameters. The get_content_template_parameter hook is only used by the documentation; making the autocompletion use it would be a separate PR to GLPI.
  • The template applied by a form is recognized with Session::isRightChecksDisabled(), which is marked @internal in GLPI. It is the only way I found to tell a ticket created by a form from a template rendered for the current user.

Tests

tests/Units/NotificationTagTest.php: tags listing, rendering of every field type, empty values, default value, label translation, block of another entity, rights by recipient (no right, read right, email address), recipient passed to the template data, events without item, followup template rendering with and without right on the block, template rendered for a requester, template applied on ticket creation without right checks, linked assets, and documentation of the variables.

Also tested on a production copy (GLPI 12.0.2, 25 blocks): change notifications, ticket notifications seen by a technician and by a self-service profile without right on the block, and a followup template applied by a form and rendered directly for a requester.

Fields values could not be used in the texts generated by GLPI.

Notification templates get two tags per field of the notified item,
listed in the "Tags" tab: ##fields.<block>.<field>## for the value as
displayed in the form, ##lang.fields.<block>.<field>## for the label in
the recipient language. A value is only sent to the recipients whose
profile can read the block in the entity of the item.

Followup, task and solution templates get the fields of tickets, changes
and problems, {{ ticket.fields.<block>.<field> }}, and of the assets
linked to a ticket, {{ asset.fields.<block>.<field> }} in
ticket.assets. They are listed in the documentation of the template
variables.
A followup template can be rendered for any user who can read the
ticket (ajax/itilfollowup.php), so a requester could read the values
of blocks hidden to his profile. As in the item form, the values are
now only rendered if the current profile can read the block, except
without right checks (template applied by a form) and in the cron.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant