Skip to content

[5.x]: ApplyFieldSaveEvent has inconsistent $event->field value #16156

@lindseydiloreto

Description

@lindseydiloreto

What happened?

Sidenote... craft\services\Fields::EVENT_BEFORE_APPLY_FIELD_SAVE is not included in the Event Generator.


I believe there is a bug in the new EVENT_BEFORE_APPLY_FIELD_SAVE event. Specifically, the $event->field value is inconsistent based on whether you are:

  • (A) Changing the field type in the Control Panel (locally), or
  • (B) Applying your Project Config changes.

Here is my current code (shown below via Xdebug).

Locally, (scenario A) the $event->field registers as a Google Maps field...


But if you roll back the database and apply the modified Project Config (scenario B), the $event->field registers as an Ether field...


This is tangentially related to a very important question...

⭐️ What is the best way to determine both the before and after types of the changing field?

I noticed that ProjectConfig::EVENT_UPDATE_ITEM emits a ConfigEvent, which contains both an $oldValue and a $newValue. Is it possible to introduce something similar to the ApplyFieldSaveEvent?

Steps to reproduce

You can get the mostly-functional dev version of this feature here...

"doublesecretagency/craft-googlemaps": "dev-v5-features/address-conversion"
  1. Install Google Maps & Ether Maps.
  2. Create an Ether Map field and populate it with some data.
  3. Go to Settings > Fields and find your Map field.
  4. Change the field type to Address (Google Maps) and save.
  5. The field should convert as expected, and existing Address data should be properly ported over.

...however...

  1. Roll back the database.
  2. Apply the same change via Project Config.
  3. Notice that these scenarios are handled differently.

Expected behavior

The $event->field value should be consistent whether I am changing the field type initially, or applying that change via Project Config.

Additionally, there should be an easy way to determine what the field type is changing to and from.

Actual behavior

  • Changing the field type via CP sets the $event->field to a Google Maps field, but
  • Changing the field type via Project Config sets the $event->field to an Ether field.

Craft CMS version

5.5.2

PHP version

8.2.22

Operating system and version

Linux 6.11.6-orbstack-00279-g28c6c77332e6

Database type and version

MySQL 8.0.36

Image driver and version

Imagick 3.7.0 (ImageMagick 6.9.11-60)

Installed plugins and versions

  • Google Maps v5.0.3 (dev-v5-features/address-conversion)
  • Ether Maps v5.0.4

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions