Skip to content

[5.x]: Apply changes via EVENT_BEFORE_SAVE does not propagate on first save #17811

@Wiejeben

Description

@Wiejeben

What happened?

Description

When applying changes to my entry via EVENT_BEFORE_SAVE it does not propagate these changes on the first save (vendor/craftcms/cms/src/services/Drafts.php:402). This is unexpected behaviour since saving the second time does propagate.

Steps to reproduce

  1. Create a section that is applied to at least two sites.
  2. Create two fields that are both non-translatable (in this case productPriceWebshop and productPriceRetail).
  3. Use this little sample script, it applies the value of productPriceRetail to productPriceWebshop only when productPriceWebshop has a falsely value.
Event::on(Entry::class, Element::EVENT_BEFORE_SAVE, function ($event) {
    $entry = $event->sender;

    if (! $entry instanceof Entry) {
        return;
    }

    if (ElementHelper::isDraftOrRevision($entry)) {
        return;
    }

    $section = $entry->section;
    if (! $section) {
        return;
    }

    if ($section->handle === 'products') {
        if (! $entry->productPriceWebshop) {
            $entry->productPriceWebshop = $entry->productPriceRetail;
        }
    }
});
  1. Create an entry within that section, enter a value into productPriceRetail and save the entry
  2. You'll see that productPriceWebshop now has a copy of that value on the site that it was being saved from, however this value is not applied to the other site.
  3. If you now re-save the entry the value would be propegated.

Expected behavior

I expect the value of any non-translatable fields to be consistent across all of my sites. However since that is probably not how this works instead I'd like to have the ability to at least catch the $entry->firstSave and somehow manually propagate the changes?

Craft CMS version

5.8.16

PHP version

8.3.20

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions