Field value isn't set properly when using conditional field as base field

Created on 22 May 2023, over 1 year ago
Updated 23 May 2023, over 1 year ago

Problem/Motivation

I have created a custom entity type in the Announcements β†’ module with a conditional field as a base field on the entity type to control visibility. When editing an existing entity, the default value of the conditional ui isn't displayed, resulting any existing values being removed on save.

Steps to reproduce

Install the Announcements module, and create an announcement (/announcements/add) with some visibility conditions. Save then entity and edit it again. Your visibility settings from before should no longer display.

Proposed resolution

Make it work?

Remaining tasks

Review.

User interface changes

Fixes Default values for base fields.

API changes

Values are loaded as expected.

Data model changes

None.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States richgerdes New Jersey, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @richgerdes
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    1 pass
  • @richgerdes opened merge request.
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States richgerdes New Jersey, USA

    I've opened an MR with a fix for this. This processed the default value on save to ensure that the value is checked for the existence of the "conditions" key. If the key isn't found, then the module will nest the value under the key. This is also written in a way that supports any modules which might want to extend this field type and add additional properties.

  • πŸ‡ΊπŸ‡ΈUnited States richgerdes New Jersey, USA
  • Status changed to Fixed over 1 year ago
  • πŸ‡­πŸ‡ΊHungary nagy.balint

    Thanks!

    It is a good question though why the setValue works differently for a base field than normal field.

    @richgerdes
    In the Announcements module's base field definitions:
    at $fields['region'] the type should likely be 'options_select' as the 'options' does not exist for me at least, so if the manage form display is saved, it will throw an error if inline entity form is installed and that will be selected by default as the first item.

    and likely visibility should have the type "'type' => 'condition_field_default'," instead of 'q'

  • πŸ‡ΊπŸ‡ΈUnited States richgerdes New Jersey, USA

    @nagy.balint, thanks for the quick review.

    I'm not 100% sure the bug here is the fault of the condition_field module itself. Seems like some odd behavior in core that isn't mapping columns the database to field properties well. I probably will dig into it further, but i think our use case here is pretty unique. I suspect that its rare an array is the only property in a base field, which is why the existing core code didn't handle it.

    Also thanks for the code review on the module and going above and beyond. I had been meaning to fix the form settings, but kept forgetting. Should be fixed now. Thanks for testing it out!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024