Skip to content

Field display logic not working on multi select fields #996

Description

@BastianHammer

Issue

My goal:
Based on a multi select option, I'd like to show another field.

I've added 4 fields through the studio:

  • test1_c (text field)
  • test2_c (text field)
  • singleselect_c (drop down)
  • optionfield_c (multi select)

The drop down and the multi select both use the same list:

  • test1
  • test2

Then, I add the code:

sudo -u www-data vi extensions/myExtension/modules/Accounts/Service/FieldDefinitions/OptionFieldDisplayLogicMapper.php

<?php

namespace App\Extension\myExtension\modules\Accounts\Service\FieldDefinitions;

use App\FieldDefinitions\Entity\FieldDefinition;
use App\FieldDefinitions\LegacyHandler\FieldDefinitionMapperInterface;

class OptionFieldDisplayLogicMapper implements FieldDefinitionMapperInterface
{
    public function getKey(): string
    {
        return 'optionfield-display-logic-mapper';
    }

    public function getModule(): string
    {
        return 'accounts';
    }

    public function map(FieldDefinition $definition): void
    {
        $vardefs = $definition->getVardef();

        if (isset($vardefs['test1_c'])) {
            $vardefs['test1_c']['displayLogic']['show_only_if_test1_selected'] = [
                'key' => 'displayType',
                'modes' => ['detail', 'edit', 'create'],
                'params' => [
                    'fieldDependencies' => ['optionfield_c'],
                    'targetDisplayType' => 'none',
                    'activeOnFields' => [
                        'optionfield_c' => [
                            ['operator' => 'not-equal', 'values' => ['test1']],
                        ],
                    ],
                ],
            ];
        }

        if (isset($vardefs['test2_c'])) {
            $vardefs['test2_c']['displayLogic']['show_only_if_test2_selected'] = [
                'key' => 'displayType',
                'modes' => ['detail', 'edit', 'create'],
                'params' => [
                    'fieldDependencies' => ['optionfield_c'],
                    'targetDisplayType' => 'none',
                    'activeOnFields' => [
                        'optionfield_c' => [
                            ['operator' => 'not-equal', 'values' => ['test2']],
                        ],
                    ],
                ],
            ];
        }

        $definition->setVardef($vardefs);
    }
}

and clear the cache:

sudo -u www-data /usr/bin/php8.4 bin/console cache:clear

If I replace optionfield_c with singleselect_c in the code, it's working.
I can select the option and the correct field is being displayed (right away, without saving the record).

If I use optionfield_c from the multi select and have nothing selected, it's all good:

Image

If I edit the record and select test 1, nothing happens :( :

Image

(no error in the browser console as well)

After saving the record, both text fields show up :( :

Image

Possible Fix

No response

Steps to Reproduce the Issue

1. See above

Context

No response

Version

8.10.2

What browser are you currently using?

Firefox

Browser Version

No response

Environment Information

MariaDB, PHP 8.4

Operating System and Version

Ubuntu 24

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority:ImportantIssues & PRs that are important; broken functions; errors; there are workaroundsSeverity: ModerateMinor ImpactType: BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions