From e1e6a4399d3fbca11c936e25709589c49625ed1c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 22 Sep 2023 01:19:04 -0700 Subject: [PATCH] setting-admin@1 - Initial implementation of mixin --- mixin/setting-admin@1/mixin.php | 83 +++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 mixin/setting-admin@1/mixin.php diff --git a/mixin/setting-admin@1/mixin.php b/mixin/setting-admin@1/mixin.php new file mode 100644 index 0000000000..9d7589b485 --- /dev/null +++ b/mixin/setting-admin@1/mixin.php @@ -0,0 +1,83 @@ + []`.) + * + * @mixinName setting-admin + * @mixinVersion 1.0.0 + * @since 5.67 + * + * @param \CRM_Extension_MixInfo $mixInfo + * @param \CRM_Extension_BootCache $bootCache + */ +return function ($mixInfo, $bootCache) { + + // We need to cache some metadata from 'info.xml' + $title = $bootCache->define('settingadmin_title_' . $mixInfo->longName, function() use ($mixInfo) { + $info = CRM_Extension_System::singleton()->getMapper()->keyToInfo($mixInfo->longName); + return empty($info->label) ? $mixInfo->longName : $info->label; + }); + + // Register the setting page ("civicrm/admin/setting/{myext}"). + Civi::dispatcher()->addListener('&hook_civicrm_alterMenu', function (array &$items) use ($mixInfo, $title) { + if (!$mixInfo->isActive()) { + return; + } + + $path = 'civicrm/admin/setting/' . $mixInfo->shortName; + if (!isset($items[$path])) { + $perm = 'administer ' . $mixInfo->shortName; + $items[$path] = [ + 'title' => ts('Administer %1', [1 => $title]), + 'page_callback' => 'CRM_Admin_Form_Generic', + 'access_arguments' => [['administer CiviCRM', $perm], 'or'], + ]; + } + }, -1000); + + // Define a permission "administer {myext}" + Civi::dispatcher()->addListener('&hook_civicrm_permission', function (array &$permissions) use ($mixInfo, $title) { + if (!$mixInfo->isActive()) { + return; + } + + $perm = 'administer ' . $mixInfo->shortName; + if (!isset($permissions[$perm])) { + $permissions[$perm] = ts('%1: Administer settings', [1 => $title]); + } + }, -1000); + + // Any settings with "group=={myext}" should be added to our setting page (unless overridden). + // By default, 'weight' is based on the order-of-declaration (spaced out with increments of 10). + Civi::dispatcher()->addListener('&hook_civicrm_alterSettingsMetaData', function(array &$settingsMetaData) use ($mixInfo) { + if (!$mixInfo->isActive()) { + return; + } + + $weight = 1000; + $weightInterval = 10; + + foreach ($settingsMetaData as &$setting) { + if (($setting['group'] ?? '') === $mixInfo->shortName) { + if (!array_key_exists('settings_pages', $setting)) { + $setting['settings_pages'][$mixInfo->shortName] = [ + 'weight' => $weight, + ]; + $weight += $weightInterval; + } + } + } + }, -1000); + +}; -- 2.25.1