Conversation
Rom1-B
left a comment
There was a problem hiding this comment.
Please add "Using the Fields Plugin via GLPI REST API" to a new *.md file.
Reference ContextDocumentation ScopePlugin Concerned VersionFields 1.23.3 GLPI 11 DocumentationUsing the Fields Plugin via GLPI REST APITable of Contents
OverviewThe Fields plugin allows custom fields to be created and updated via the GLPI REST API. Custom fields are passed directly alongside native GLPI fields in the Minimum versions required:
PrerequisitesBefore making API calls, ensure:
AuthenticationInit Session with credentialscurl -X GET \
'https://glpi.example.com/apirest.php/initSession' \
-H 'Content-Type: application/json' \
-H 'Authorization: Basic BASE64_LOGIN_PASSWORD' \
-H 'App-Token: YOUR_APP_TOKEN'Init Session with user tokencurl -X GET \
'https://glpi.example.com/apirest.php/initSession' \
-H 'Content-Type: application/json' \
-H 'Authorization: user_token YOUR_USER_TOKEN' \
-H 'App-Token: YOUR_APP_TOKEN'Response{
"session_token": "abc123def456"
}
Understanding Field NamingWhen a field is created in the Fields plugin, its internal name (used as the API key) is derived from the label:
Examples:
The exact key to use depends on the field type — see Field Types Reference.
Container Types & TargetingThe Fields plugin supports three container types:
Auto-detection (recommended for
|
Checklist before requesting a review
Please delete options that are not relevant.
Description
When GLPI is booted to serve a REST API request,
plugin_init_fields()runs before the API session is established (token auth happens after boot). Because thepre_item_update,pre_item_add,item_addandpre_item_purgehooks were registered inside theSession::getLoginUserID()guard, they were never registered for API calls. As a result, any PUT/POST toTicket(or any other Fields-enabled itemtype) silently ignored all plugin field values and only persisted native GLPI fields.A secondary issue in
PluginFieldsContainer::preItem()caused the same failure: theelse { return false; }branch on the profile rights check unconditionally rejected requests that had no active profile in session (cron, API token sessions).Fix
pre_item_update,pre_item_add,item_add,pre_item_purge),ITEM_TRANSFERandplugin_fields_register_plugin_types()outside the session guard. They are now registered as soon as the plugin is active, regardless of session state. Permission checks remain enforced inside the hook callbacks.return falseon missing session profile; the check is now skipped (not failed) when no profile is active in session.Tests
ContainerItemUpdateTest.php— 4 new tests :testCrudHooksRegisteredWithoutSession— asserts hooks are registered after a session-lessplugin_init_fields()calltestUpdateTicketInApiLikeContext— full API lifecycle simulation (boot without session → restore session → update ticket with plugin fields)testCreateTicketInApiLikeContext— same for ticket creationtestUpdateTicketWithExplicitCidInApiLikeContext— same with explicitc_id