Condition: field value changed - not working for booleans

Created on 13 June 2023, over 1 year ago
Updated 11 July 2023, over 1 year ago

Problem/Motivation

The condition checks for strict values.

Steps to reproduce

Add a Boolean field to the Article content type, add a condition to display a message if the Boolean has changed.
The condition will always trigger.

🐛 Bug report
Status

Fixed

Version

1.2

Component

Code

Created by

🇬🇧United Kingdom lexsoft London

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

Comments & Activities

  • Issue created by @lexsoft
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Thanks @lexsoft for reporting this. Just wondering where your status message is coming from?

    The comparison in the condition plugin looks like this:

    $changed = $property->getValue() !== $original_property->getValue()
    

    where property comes from the current entity and original_property comes from the original entity. If those 2 deliver the same typed data in different format, that would be terrible as all that data comes directly from the Drupal core API.

    Note, I've changed the version of this issue to 1.2.x as bug reports always need to be addressed in the latest version, from where they can sometimes be back ported.

  • 🇬🇧United Kingdom lexsoft London

    Hi @jurgenhaas,

    I've just done a dpm on the values to debug why it's always triggered.
    dpm($property->getValue());
    dpm($original_property->getValue());

    The test was done on a clean install of Drupal 9.5.9.
    Also, I've noticed that we don't have $entity->original when working with entity reference in inline_entity_form. Is there a way around this?

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Hmm, that sounds bad. I've no idea what we could do about it when core provides the same value in 2 different ways. That makes comparison almost impossible.

    Also, I've noticed that we don't have $entity->original when working with entity reference in inline_entity_form. Is there a way around this?

    AFAIK the property $entity->original is only made available during entity-related events like pre-save and update. Form events don't provide that at all, and referenced entities neither.

  • 🇬🇧United Kingdom lexsoft London

    Can we just change the condition to not check data types, I think in the majority of cases people would want to see if the content has changed rather then data type.
    $changed = $property->getValue() != $original_property->getValue()

    And maybe add another condition where we also check for data types? Something like field value changed strict.

  • 🇬🇧United Kingdom lexsoft London

    I'm thinking maybe just a checkbox to enable/disable strict data types.
    Will provide an example shortly.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    We've just discussed this in the team as well, and @boromino suggested to check, if the value is scalar and if so, if it's 0, 1, "0" or "1" and if so, cast the value to a string before comparison.

    That way we could keep the strict comparison.

    Another option would be to check the field type first, and if it's a boolean, cast the value to a boolean first. But that one is probably to time consuming.

  • 🇬🇧United Kingdom lexsoft London

    Also when used on entity reference the array is different as well:

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    288 pass, 2 fail
  • @lexsoft opened merge request.
  • 🇬🇧United Kingdom lexsoft London

    It might be useful to have 2 more conditions to maybe get data from revisions rather then original.
    1. Check if revisions exists for entity
    2. Check field value changed from revision

    What do you think?

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    289 pass
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Also when used on entity reference using inline_entity_form the array is different as well

    I'd say, we should maybe switch to the other approach to check the field type and do special treatment for booleans and entity references.

    1. Check if revisions exists for entity

    Not sure if that's necessary, if I got this right. There is the action Entity load which allows to load a specific revision. Maybe that needs to be extended a bit to load e.g. the previous revision instead of the latest revision. But from there you can load revisions, which also acts like a condition: if the revision doesn't exist, the model processing ends at that point.

    2. Check field value changed from revision

    You mean like comparing 2 given entity revisions? That sounds useful and that condition could be implemented as a sub-class of this one.

  • 🇬🇧United Kingdom lexsoft London

    I'd say, we should maybe switch to the other approach to check the field type and do special treatment for booleans and entity references.

    That's sound great to me. I can help with testing when it's done.

    You mean like comparing 2 given entity revisions? That sounds useful and that condition could be implemented as a sub-class of this one.

    Essentially I would like to check if the entity I'm saving (current or loaded via reference) has a revision and if it does has the value of the field changed compared to what it's saved in latest revision prior to the one created on save.

    My scenario is really simple.
    I have a node form that clients need to complete.
    After they complete the form a supervisor is notified, reviews the form and flags changes needed on the form.
    Finally here is where I need the condition to check if changes to the specified fields are made, if so I need to notify the supervisor again and the process repeats until done.

  • Assigned to jurgenhaas
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Essentially I would like to check if the entity I'm saving (current or loaded via reference) has a revision and if it does has the value of the field changed compared to what it's saved in latest revision prior to the one created on save.

    When saving a node, the latest revision is always what you get in $entity->original. Why would we need something extra here?

    And when saving a node, referenced entities don't change at all, The field value of the saved entity which references other entities may change, but that is already covered.

    So, if we still needed more conditions, let's address them in new issues as feature requests, please. I'll be working on the bug fix with booleans and entity refs.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    281 pass, 2 fail
  • @jurgenhaas opened merge request.
  • 🇬🇧United Kingdom lexsoft London

    I'm sorry if I made things more confusing, I have not mastered yet ECA.
    Getting this condition to work with Booleans and inline entity forms will probably fix all my problems.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    I've now implemented the comparison such that field types boolean and entity reference get sanitized before comparison. That's included in the new MR !369

  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen
  • 🇬🇧United Kingdom lexsoft London

    I can confirm it works for booleans and entity references using inline entity form.

    There is a weird thing where on a text editor the array is inverted, I wonder if we need to count for that as well.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Very strange indeed. Maybe sorting all the arrays with ksort before comparison?

  • Status changed to Needs work over 1 year ago
  • 🇩🇪Germany mxh Offenburg

    Please fix the tests first, it doesn't make sense to review something that breaks tests.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    291 pass
  • 🇬🇧United Kingdom lexsoft London

    ksort might be worth a try.

    I have added tests for Boolean fields.

  • Status changed to Needs review over 1 year ago
  • 🇬🇧United Kingdom lexsoft London
  • 🇩🇪Germany mxh Offenburg

    Cool, thanks for fixing and extending the tests :)

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    291 pass
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Now also added the ksort for all fields that are neither boolean nor entity references. Please give that a try too and if it resolves your editor issue, please set it to RTBC.

  • Status changed to RTBC over 1 year ago
  • 🇩🇪Germany danielspeicher Steisslingen

    That is a cool approach to tackle this problem

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    291 pass
    • jurgenhaas committed 87c1db52 on 1.2.x
      Issue #3366468 by lexsoft, jurgenhaas: Condition: field value changed -...
    • jurgenhaas committed 6653dd76 on 1.2.x
      Issue #3366468 by lexsoft, jurgenhaas: Condition: field value changed -...
    • jurgenhaas committed 756e101b on 1.1.x
      Issue #3366468 by lexsoft, jurgenhaas: Condition: field value changed -...
    • jurgenhaas committed f5a326f7 on 1.1.x
      Issue #3366468 by lexsoft, jurgenhaas: Condition: field value changed -...
  • Status changed to Fixed over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Merged and back ported to 1.1

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

Production build 0.71.5 2024