[Feature] Allow multiple "Insertion in the form" (dom) blocks per entity
This PR provides the ability to define multiple "Insertion in the form" blocks for the same item based on its entity
- Created a new method findContainers() (based on findContainer()) that returns all 'dom' containers for an item based on its entity (with parent entity handling via getAncestorsOf()).
- Adapted hooks (pre_item_add, pre_item_update, post_item_add, post_item_update) to manage multiple containers using the _plugin_fields_data_multi array.
- Updated the populateData() function to extract input values by stripping the prefix, ensuring that data is saved into the correct columns of the injection table.
- Modified the container.form.php file to "clean" the form data (by removing the prefix) before calling updateFieldsValues(), thereby enabling the saving of domtab containers.
Please test these modifications and verify that everything works as expected for all object types supported by the Fields plugin. Note that my tests were successful, although I focused exclusively on 'Ticket' type objects for my use case.
Checklist before requesting a review
Please delete options that are not relevant.
- [x] 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.
- [x] This change requires a documentation update.
Issues : #744, #789, #834
Hi @JeremieMercier
I am questioning the plugin's ability to reconcile the correct container during API calls, an issue we have encountered in the past. For example, when updating a ticket and including the "fields" in the payload, could you verify this point?
Hi @stonebuzz,
I retrieve the block row ID like this:
Then, I update the field using the following method:
So I haven’t encountered any issue with the API — unless I misunderstood how you're updating the fields via the API?
It is also possible to update fields from the fields plugin during ticket update.
PUT {{URL_GLPI}} /Ticket/5
Yes, it works
Just include the block ID in the field name, like: plugin_fields_15_bloconechamponefield
Hello, do you have time to review the PR ?
I applied the changes for PHPStan 🤞
I don't understand @trasher
With php 7.4 :
A syntax error, it was slipped in after applying the suggestion 😅
Are we good this time ? 😅🤞
Hi, what's the next step ?
Hi @JeremieMercier
The feature you are proposing is substantial and introduces significant changes. To ensure its smooth integration and functionality, we must be extremely vigilant. This means we need to "lock down" our testing to guarantee that everything works as expected.
It is important to acknowledge that the plugin is already complex to maintain, especially with the new features introduced in GLPI. Given this complexity, we must approach any additional features with caution to ensure they do not further complicate maintenance and support.
To ensure the proper functioning of this PR, here is a concise list of unit tests that should be added:
Unit Tests for findContainers
-
Retrieve Containers for Specific Itemtype and Entity
- Verify that
findContainersreturns the correct containers for a given itemtype and entity.
- Verify that
-
Retrieve Containers with Subtype
- Verify that
findContainersreturns the correct containers when a subtype is specified.
- Verify that
-
Retrieve Containers with Recursive Entity
- Verify that
findContainersconsiders recursive entities.
- Verify that
Unit Tests for Hooks
-
preItemAddwith Multiple Containers- Verify that the
preItemAddhook correctly handles data for multiple containers.
- Verify that the
-
postItemAddwith Multiple Containers- Verify that the
postItemAddhook correctly updates field values for multiple containers.
- Verify that the
-
preItemUpdatewith Multiple Containers- Verify that the
preItemUpdatehook correctly handles data for multiple containers during item update.
- Verify that the
Unit Tests for populateData
-
populateDatawith Prefix- Verify that
populateDatacorrectly extracts field values by removing the prefix.
- Verify that
-
populateDatawith Multiple Selection Fields- Verify that
populateDatacorrectly handles fields allowing multiple selections.
- Verify that
Unit Tests for showForTab
-
showForTabwith Multiple Containers- Verify that
showForTabcorrectly displays containers for a given item.
- Verify that
-
showForTabwith Insufficient Rights- Verify that
showForTabreturns nothing if the user does not have sufficient rights on the containers.
- Verify that
Unit Tests for API
-
Ticket input update
-
Container update
Unit Tests for rights
Hi @stonebuzz Thanks for the message
I fully understand the concerns and the importance of thorough testing. I'm currently working on implementing the required tests and will make sure all the listed cases are properly covered.
While working on the tests already partially in place, I’ve been able to identify and fix some issues especially related to "dropdown" field handling and container restriction management.
I’ll keep updating here as I progress.
Hi, @JeremieMercier. What tool you are using to send HTTP requests? I'm newby in WEB development so I'm not entirely shure what tool is that. Thank you :)
Edit: I've done some research and found Postman, as a tool to send HTTP requests and creating API tests
I’ve added all the requested unit tests (except the API ones):
- findContainers : basic cases, sub-types, entity recursion
- Hooks: preItem, postItemAdd, preItemUpdate with multiple containers
- populateData: prefix handling and multi-selection fields
- showForTab: displaying multiple containers and checking rights
Please note I’m not a testing expert, so the tests may not follow every usual best practice. Feel free to share any feedback so I can improve.
The API tests still need to be written by whoever is willing to tackle them.
Hello, Tank you very much @JeremieMercier for this work. That's a realy needed feat for us ! Any followup of this PR ? Best regards
Hello, Tank you very much @JeremieMercier for this work. That's a realy needed feat for us ! Any followup of this PR ? Best regards
Hello @CorentinS6, From my side everything seems good, it remains to be seen if a developer of the project can finalize the tests
And code generated by AI ?
And code generated by AI ?
Hello, All the logic is 100% mine, however for the tests, it's another story, I used AI to learn and understand the use of the tests in addition to videos like this one from Grafikart, but as I said, I'm not an expert..., the tests can also be redone by a more competent person if necessary knowing that basically, there were no tests set up on the plugin.
@stonebuzz , is this work good enough for the merge now?
This will be very useful, now I can only add one block to all entities, I need one per entity and this seems to solve the problem, is there something I can help with?