-
Notifications
You must be signed in to change notification settings - Fork 684
Closed
Labels
Description
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
- 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
Reactions are currently unavailable