Skip to content

[4.x]: Change propagation method programmatically #16084

@yannkost

Description

@yannkost

What happened?

Description

We would like to change the propagation method only for 1 user group (save all entries to all sites, only for this particular group). But I can't seem to write the necessary code with the EVENT_BEFORE_SAVE event.

I have following code in the event hook:

Event::on(
     Entry::class,
     Element::EVENT_BEFORE_SAVE,
     function (ModelEvent $e) {
         if (ElementHelper::isDraftOrRevision($e->sender)) {
             return;
         }

         $entry = $e->sender;
         if ($entry->getSection()->handle === 'applications') {
             $currentUser = Craft::$app->getUser()->getIdentity();
             if ($currentUser) {
                 $isInGroup = $currentUser->isInGroup('pm');
                 if ($isInGroup) {
                     if ($entry instanceof Entry) {
                         $supportedSites = $entry->getSupportedSites();
                         $siteStatuses = ElementHelper::siteStatusesForElement($entry);
                         foreach (Craft::$app->getSites()->allSites as $site) {
                             $siteStatuses[$site->id] = true;
                         }
                         $entry->setEnabledForSite($siteStatuses);
                         $entry->propagateAll = true;
                         $entry->setScenario(Element::SCENARIO_LIVE);
                     }
                 }
             }
         }
     }
 );

Is there any way to do this programmatically? Any help greatly appreciated :)

Steps to reproduce

  1. Save an entry where the propagation method is to only save the entry to the site the element has been created on.

Expected behavior

Having the hook code working to change the propagation method only for this particular group.

Actual behavior

The entry gets saved without propagation.

Craft CMS version

4.12.8

PHP version

8.2

Operating system and version

linux

Database type and version

mariadb

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