processwire-issues icon indicating copy to clipboard operation
processwire-issues copied to clipboard

Integer field: setting a default value always triggers change detection in hook

Open hiboudev opened this issue 2 years ago • 5 comments

Short description of the issue

Default value of Integer field triggers change detection in hook.

Steps to reproduce the issue

  1. Create an Integer field with a default value (zero).
  2. Add the field to a page.
  3. Create a saveReady hook:
$this->addHookBefore('Pages::saveReady',
  function (HookEvent $event): void {
      /** @var Page $page */
      $page = $event->arguments(0);

      $changes = $page->getChanges(true);
      
      if ($changes) {
        var_dump($changes);
        exit();
      }
  }
);
  1. Save the page without any change and see that there is a change detected on the Integer field.
  2. Edit the Integer field configuration and remove the default value.
  3. Save the page without any change and see that no change is detected anymore.

Setup/Environment

  • ProcessWire version: 3.0.228

hiboudev avatar Nov 08 '23 21:11 hiboudev

@hiboudev If I understand correctly, this would be because the default value is being populated to the page because it doesn't already have a value present. If it wasn't in the list of changes, then the default value would never get saved to the page. The $pages->save() method only saves the fields in that list.

ryancramerdesign avatar Nov 09 '23 16:11 ryancramerdesign

@ryancramerdesign The default value is never saved to the page I think, because the change is detected at every save.

  1. Field is empty and configured with default value 0
  2. I save the page : change detected
  3. I save the page again : change detected

hiboudev avatar Nov 10 '23 15:11 hiboudev

@hiboudev I'm trying to duplicate here, but I can't get to your item 3 above. I only get the change detected on the first save where the default value gets populated. Saves following that don't detect the field as changed. I did try changing the "default and 0" setting of the field but I'm not seeing any difference in behavior either way. Are there any other factors you can think of, such as the default being for some field/template context, or anything like that?

ryancramerdesign avatar Nov 10 '23 16:11 ryancramerdesign

Hello @ryancramerdesign

I just did a test on a dev branch installation I use only for test purpose and I can reproduce it. I create an Integer field, set the default value to 0 (no other configuration), add it to a template, create a page, I publish the page, save it, then active the hook and save again, and the hook is always triggered.

In the database, table "field_integer" (my test field is named "integer") is empty.

hiboudev avatar Nov 11 '23 08:11 hiboudev

@ryancramerdesign Value is saved only if I check "Blank and 0 are different" and then the hook doesn't trigger anymore after first save.

So, I bet this is expected, isn't it?

But when "Blank and 0 are same", because default value is not saved, it triggers change detection forever, until user enter a value != 0.

hiboudev avatar Nov 11 '23 17:11 hiboudev