From cfd3308af3541ce643b05431de79f2dcfbce9950 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 20 Oct 2023 20:29:31 -0700 Subject: [PATCH] MultisiteManaged - If a module has made its own domain-copies, then skip it --- Civi/Foo/MultisiteManaged.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Civi/Foo/MultisiteManaged.php b/Civi/Foo/MultisiteManaged.php index 65a8d4f566..614b6d3a46 100644 --- a/Civi/Foo/MultisiteManaged.php +++ b/Civi/Foo/MultisiteManaged.php @@ -15,12 +15,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; * 3. Package the subscriber in an extension (`multsite` or `multisitemgd`) * 4. Package the subscriber as a mixin that extensions may toggle (`mgd@2.0`) * - * The choice of package/activation determines how it interacts with the 2-3 ext's that already reproduce across domains: - * - * 1. Nobody has to opt-in. Bt would multiply #records, unless we a hard-coded exclusion list for the 2-3. - * 2. Won't activate unless the site-admin chooses to. - * 3. Won't activate unless the site-admin chooses to. - * 4. Each extension needs to opt into the new behavior. + * This variant has an extra guard so that clever extensions (which multiply entities themselves) don't get entities-squared. * * @service * @internal @@ -36,11 +31,16 @@ class MultisiteManaged extends AutoService implements EventSubscriberInterface { } public function generateDomainEntities(array &$entities): void { + $selfMultipliedModules = $this->findSelfMultipliedModules($entities); + foreach ($selfMultipliedModules as $module) { + \CRM_Core_Error::deprecatedWarning(sprintf('Module (%s) has self-multiplied some records across domains. This is deprecated.', $module)); + } + $templates = []; // Figure out which entities to copy foreach (array_keys($entities) as $entityKey) { - if ($this->isCopiable($entities[$entityKey])) { + if ($this->isCopiable($entities[$entityKey]) && !in_array($entities[$entityKey]['module'], $selfMultipliedModules)) { $templates[] = $entities[$entityKey]; unset($entities[$entityKey]); } @@ -70,4 +70,20 @@ class MultisiteManaged extends AutoService implements EventSubscriberInterface { return in_array($entity['entity'], $this->entities) && ($entity['params']['version'] ?? 3) == 4; } + protected function findSelfMultipliedModules(array $entities): array { + $moduleDomains = []; + foreach ($entities as $entity) { + if ($this->isCopiable($entity) && !empty($entity['params']['values']['domain_id'])) { + $moduleDomains[$entity['module']][] = $entity['params']['values']['domain_id']; + } + } + $results = []; + foreach ($moduleDomains as $module => $domains) { + if (count(array_unique($domains)) > 1) { + $results[] = $module; + } + } + return $results; + } + } -- 2.25.1