From fe2ea33deec0f3e4a3cb8f1e9ab6f2febec12efa Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 20 Oct 2023 20:10:49 -0700 Subject: [PATCH] (POC) Automatically copy `Navigation` mgds to all domains --- Civi/Foo/MultisiteManaged.php | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Civi/Foo/MultisiteManaged.php diff --git a/Civi/Foo/MultisiteManaged.php b/Civi/Foo/MultisiteManaged.php new file mode 100644 index 0000000000..65a8d4f566 --- /dev/null +++ b/Civi/Foo/MultisiteManaged.php @@ -0,0 +1,73 @@ +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. + * + * @service + * @internal + */ +class MultisiteManaged extends AutoService implements EventSubscriberInterface { + + protected $entities = ['Navigation', 'Dashboard']; + + public static function getSubscribedEvents() { + return [ + '&hook_civicrm_managed' => ['generateDomainEntities', -1000], + ]; + } + + public function generateDomainEntities(array &$entities): void { + $templates = []; + + // Figure out which entities to copy + foreach (array_keys($entities) as $entityKey) { + if ($this->isCopiable($entities[$entityKey])) { + $templates[] = $entities[$entityKey]; + unset($entities[$entityKey]); + } + } + + // Make the real entities, one for each domain + foreach ($templates as $template) { + foreach ($this->makeCopies($template) as $entity) { + $entities[] = $entity; + } + } + } + + protected function makeCopies(array $entity): array { + $copies = []; + $domains = \Civi\Api4\Domain::get(FALSE)->addSelect('id')->execute(); + foreach ($domains as $domain) { + $copy = $entity; + $copy['name'] = $entity['name'] . '_' . $domain['id']; + $copy['params']['values']['domain_id'] = $domain['id']; + $copies[] = $copy; + } + return $copies; + } + + protected function isCopiable(array $entity) { + return in_array($entity['entity'], $this->entities) && ($entity['params']['version'] ?? 3) == 4; + } + +} -- 2.25.1