fields icon indicating copy to clipboard operation
fields copied to clipboard

[Feature] Allow multiple "Insertion in the form" (dom) blocks per entity

Open JeremieMercier opened this issue 10 months ago • 20 comments

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

JeremieMercier avatar Apr 04 '25 11:04 JeremieMercier

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?

stonebuzz avatar Apr 09 '25 09:04 stonebuzz

Hi @stonebuzz,

I retrieve the block row ID like this: image

Then, I update the field using the following method: image

So I haven’t encountered any issue with the API — unless I misunderstood how you're updating the fields via the API?

JeremieMercier avatar Apr 09 '25 10:04 JeremieMercier

It is also possible to update fields from the fields plugin during ticket update.

PUT {{URL_GLPI}} /Ticket/5

stonebuzz avatar Apr 09 '25 11:04 stonebuzz

Yes, it works

Just include the block ID in the field name, like: plugin_fields_15_bloconechamponefield

image image

JeremieMercier avatar Apr 09 '25 11:04 JeremieMercier

Hello, do you have time to review the PR ?

JeremieMercier avatar Apr 16 '25 13:04 JeremieMercier

I applied the changes for PHPStan 🤞

JeremieMercier avatar Apr 16 '25 16:04 JeremieMercier

I don't understand @trasher

image

With php 7.4 :

image

JeremieMercier avatar Apr 17 '25 12:04 JeremieMercier

A syntax error, it was slipped in after applying the suggestion 😅

JeremieMercier avatar Apr 17 '25 13:04 JeremieMercier

Are we good this time ? 😅🤞

JeremieMercier avatar Apr 17 '25 13:04 JeremieMercier

Hi, what's the next step ?

JeremieMercier avatar Apr 23 '25 12:04 JeremieMercier

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

  1. Retrieve Containers for Specific Itemtype and Entity

    • Verify that findContainers returns the correct containers for a given itemtype and entity.
  2. Retrieve Containers with Subtype

    • Verify that findContainers returns the correct containers when a subtype is specified.
  3. Retrieve Containers with Recursive Entity

    • Verify that findContainers considers recursive entities.

Unit Tests for Hooks

  1. preItemAdd with Multiple Containers

    • Verify that the preItemAdd hook correctly handles data for multiple containers.
  2. postItemAdd with Multiple Containers

    • Verify that the postItemAdd hook correctly updates field values for multiple containers.
  3. preItemUpdate with Multiple Containers

    • Verify that the preItemUpdate hook correctly handles data for multiple containers during item update.

Unit Tests for populateData

  1. populateData with Prefix

    • Verify that populateData correctly extracts field values by removing the prefix.
  2. populateData with Multiple Selection Fields

    • Verify that populateData correctly handles fields allowing multiple selections.

Unit Tests for showForTab

  1. showForTab with Multiple Containers

    • Verify that showForTab correctly displays containers for a given item.
  2. showForTab with Insufficient Rights

    • Verify that showForTab returns nothing if the user does not have sufficient rights on the containers.

Unit Tests for API

  1. Ticket input update

  2. Container update

Unit Tests for rights

stonebuzz avatar Apr 24 '25 07:04 stonebuzz

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.

JeremieMercier avatar May 07 '25 23:05 JeremieMercier

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

akstis-typer avatar May 23 '25 06:05 akstis-typer

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.

JeremieMercier avatar May 25 '25 21:05 JeremieMercier

Hello, Tank you very much @JeremieMercier for this work. That's a realy needed feat for us ! Any followup of this PR ? Best regards

CorentinS6 avatar Jul 18 '25 11:07 CorentinS6

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

JeremieMercier avatar Jul 21 '25 15:07 JeremieMercier

And code generated by AI ?

CorentinS6 avatar Aug 26 '25 07:08 CorentinS6

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.

JeremieMercier avatar Aug 28 '25 08:08 JeremieMercier

@stonebuzz , is this work good enough for the merge now?

CorentinS6 avatar Aug 28 '25 09:08 CorentinS6

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?

JuanPabloToniolo-Udesa avatar Dec 12 '25 11:12 JuanPabloToniolo-Udesa