fields icon indicating copy to clipboard operation
fields copied to clipboard

Hiding fields depending on category does not work

Open DaviidMM opened this issue 1 year ago • 6 comments

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • [X] I have searched the existing issues

GLPI Version

10.0.15

Plugin version

1.21.15

Bug description

When I setup a block to be hidden depending on ticket category, block is not hidden in simplified interface. Haven't tried in standard interface since it does not meet our requirements.

Block configuration:

Block config 1

Block fields:

Block fields

Block hiding condition:

Block condition Block condition 2

When a user begins to create a ticket, block is shown (which is expected to happen)

Ticket creation before setting category

But when the user sets the category by which the block has to be hidden, some erratic movement happens and the field is still shown with broken CSS, a.k.a. moved rightwards and wider.

Ticket creation after setting category

I've tried setting the condition to the ticket's title and this works correctly, block is successfully hidden and works like a charm.

Relevant log output

No response

Page URL

No response

Steps To reproduce

  1. Create a block and set it's type to "Insertion in the form (before save button)".
  2. Configure some fields in the block.
  3. Set a hiding conditional rule that depends on ticket's category.
  4. Test a ticket creation by setting the category specified on step 3 and block is still present.

Your GLPI setup information

No response

Anything else?

No response

DaviidMM avatar Oct 15 '24 10:10 DaviidMM

Still having this issue...

DaviidMM avatar Jan 13 '25 09:01 DaviidMM

I was having the same issue. And I, so called "fixed" it. In field.class.php I added || strpos($current_url, 'tracking.injector.php') !== false to the if statements. Because, when you start using ticket form - your address is not 'helpdesk.public.php' anymore it's 'tracking.injector.php'

if (strpos($current_url, 'helpdesk.public.php') !== false || strpos($current_url, 'tracking.injector.php') !== false) {
            echo "<div id='{$html_id}' class='card-body row mx-0' style='border-top:0'>";
            echo "<div class='offset-md-1 col-md-8 col-xxl-6'>";
            $field_options = [
                'label_class' => 'col-lg-3',
                'input_class' => 'col-lg-9',
            ];
        } else {
            echo "<div id='{$html_id}'>";
        }
        $display_condition = new PluginFieldsContainerDisplayCondition();

        if ($display_condition->computeDisplayContainer($item, $c_id))
        {
            self::showDomContainer(
                $c_id,
                $item,
                $type,
                $subtype,
                $field_options ?? [],
            );
        }
        if (strpos($current_url, 'helpdesk.public.php') !== false || strpos($current_url, 'tracking.injector.php') !== false) {
            echo '</div>';
        }

This will fix the alignment issue.

Next, I edited showForTab function like so

public static function showForTab($params)
    {
        $item = $params['item'];

        if($item->fields['type'] == "")
        {
            $item->fields['type'] = $params['options']['type'];
        }
        if($item->fields['itilcategories_id'] == "")
        {
            $item->fields['itilcategories_id'] = $params['options']['itilcategories_id'];
        }

There, if we don't have type in $item or we don't have itilcategories_id- we got them from $params options.

This will fix all issues with containers, and you will be able to show or hide them according to your conditions. If there is a better way to fix this issue - tell me.

akstis-typer avatar Apr 10 '25 09:04 akstis-typer

Hi @akstis-typer

can you propose a PR ?

stonebuzz avatar Apr 10 '25 10:04 stonebuzz

Hi! I've tried the code, but it didn’t work as expected in my case. I had to move a few lines of your code further down in fields.inc.php to make it work properly.

 public static function showForTab($params)
    {
        $item = $params['item'];

        $functions = array_column(debug_backtrace(), 'function');
        ...
        $display_condition = new PluginFieldsContainerDisplayCondition();

        // @akstis-typer code starts here ------
        if($item->fields['type'] == "")
        {
            $item->fields['type'] = $params['options']['type'];
        }
        if($item->fields['itilcategories_id'] == "")
        {
            $item->fields['itilcategories_id'] = $params['options']['itilcategories_id'];
        }
        // @akstis-typer code ends here ------

        if ($display_condition->computeDisplayContainer($item, $c_id)) {
        ...

The issue seemed to be related to this part of the code:

if ($item->isEntityAssign()) {
    $current_entity = $item->getEntityID();
    if (!in_array($current_entity, $entities)) {
        return;
    }
}

I’m not entirely sure what this block was doing, but in my case it was preventing the code from working as expected — the values assigned by @akstis-typer snippet were overriden once again to empty values.

Hope this helps!

DaviidMM avatar Apr 10 '25 12:04 DaviidMM

There has been no activity on this issue for some time and therefore it is considered stale and will be closed automatically in 10 days.

If this issue is related to a bug, please try to reproduce on latest release. If the problem persist, feel free to add a comment to revive this issue. If it is related to a new feature, please open a topic to discuss with community about this enhancement on suggestion website.

You may also consider taking a subscription to get professionnal support or contact GLPI editor team directly.

github-actions[bot] avatar May 21 '25 08:05 github-actions[bot]

@DaviidMM please try fix proposed in #942

trasher avatar May 23 '25 09:05 trasher