From c0a7a4986236d838babfc1449934a550634fd657 Mon Sep 17 00:00:00 2001 From: colemanw Date: Sat, 23 Dec 2023 10:50:09 -0500 Subject: [PATCH] dev/core#4674 - Add Oauth links for AdminUi MailSettings This fixes https://lab.civicrm.org/dev/core/-/issues/4674 by adding a LinksProvider for the new GetLinks action from #27973. It uses the CRM_Core_BAO_MailSettings::getSetupActions() function to generate the extra links, but there is a mismatch between the two functions: - getSetupActions() returns a fully-formed url - API::getLinks() returns a path However, I noticed that while in theory hook_civicrm_mailSetupActions could return various urls, in practice they are all the same: a generic civicrm/ajax/setupMailAccount redirect. So this returns that as the path and it works. Perhaps we should deprecate the option to return a url from hook_civicrm_mailSetupActions (or just remove it, since it appears to be unused & YAGNI). --- .../Links/MailSettingsLinksProvider.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 ext/civi_mail/Civi/Api4/Service/Links/MailSettingsLinksProvider.php diff --git a/ext/civi_mail/Civi/Api4/Service/Links/MailSettingsLinksProvider.php b/ext/civi_mail/Civi/Api4/Service/Links/MailSettingsLinksProvider.php new file mode 100644 index 0000000000..585236ac1e --- /dev/null +++ b/ext/civi_mail/Civi/Api4/Service/Links/MailSettingsLinksProvider.php @@ -0,0 +1,60 @@ + 'alterLinksResult', + ]; + } + + public static function alterLinksResult(RespondEvent $e): void { + $request = $e->getApiRequest(); + if ($request['version'] == 4 && is_a($request, '\Civi\Api4\Action\GetLinks') && $request->getEntityName() === 'MailSettings') { + $links = (array) $e->getResponse(); + $addLinkIndex = self::getActionIndex($links, 'add'); + // Unset the generic "add" link and replace it with links to each mailSetup account + // @see \CRM_Utils_Hook::mailSetupActionsx + if (isset($addLinkIndex) && $request->getExpandMultiple()) { + // Use the single add link from the schema as a template + $addTemplate = $links[$addLinkIndex]; + $newLinks = []; + foreach (\CRM_Core_BAO_MailSettings::getSetupActions() as $key => $action) { + $link = $addTemplate; + $link['text'] = $action['title']; + // The standard link is fine as-is. Others use a redirect: + if ($key !== 'standard') { + $link['path'] = "civicrm/ajax/setupMailAccount?type=$key"; + $link['target'] = '_blank'; + } + $newLinks[] = $link; + if (isset($addTemplate['weight'])) { + $addTemplate['weight']++; + } + } + array_splice($links, $addLinkIndex, 1, $newLinks); + } + $e->getResponse()->exchangeArray(array_values($links)); + } + } + +} -- 2.25.1