From 7e4582c779c358253442b086728ff66dce35dd16 Mon Sep 17 00:00:00 2001 From: benjamin Date: Thu, 19 Dec 2024 13:50:34 +0000 Subject: [PATCH] use common helper to set expected keys for tabs in Summary.tpl and TabHeader.tpl --- CRM/Campaign/Form/Survey.php | 1 + CRM/Campaign/Form/Survey/TabHeader.php | 1 + CRM/Campaign/Page/Vote.php | 3 +- CRM/Contact/Page/View/Summary.php | 7 ++-- .../Form/ContributionPage/TabHeader.php | 1 + CRM/Core/Form.php | 1 - CRM/Core/Page.php | 2 -- CRM/Core/Smarty.php | 32 +++++++++++++------ CRM/Event/Form/ManageEvent.php | 1 + CRM/Event/Form/ManageEvent/TabHeader.php | 1 + 10 files changed, 33 insertions(+), 17 deletions(-) diff --git a/CRM/Campaign/Form/Survey.php b/CRM/Campaign/Form/Survey.php index e57f18591c99..0d972e98d5b2 100644 --- a/CRM/Campaign/Form/Survey.php +++ b/CRM/Campaign/Form/Survey.php @@ -133,6 +133,7 @@ private function build() { $tabs = $this->processSurveyForm(); $form->set('tabHeader', $tabs); } + $tabs = \CRM_Core_Smarty::setRequiredTabTemplateKeys($tabs); $form->assign('tabHeader', $tabs); CRM_Core_Resources::singleton() ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header') diff --git a/CRM/Campaign/Form/Survey/TabHeader.php b/CRM/Campaign/Form/Survey/TabHeader.php index c8b7ceae1651..4be3f26feee5 100644 --- a/CRM/Campaign/Form/Survey/TabHeader.php +++ b/CRM/Campaign/Form/Survey/TabHeader.php @@ -36,6 +36,7 @@ public static function build(&$form) { $tabs = self::process($form); $form->set('tabHeader', $tabs); } + $tabs = \CRM_Core_Smarty::setRequiredTabTemplateKeys($tabs); $form->assign('tabHeader', $tabs); CRM_Core_Resources::singleton() ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header') diff --git a/CRM/Campaign/Page/Vote.php b/CRM/Campaign/Page/Vote.php index 955b23de3206..be1e2fb92fc6 100644 --- a/CRM/Campaign/Page/Vote.php +++ b/CRM/Campaign/Page/Vote.php @@ -122,7 +122,8 @@ public function buildTabs() { ]; } - $this->assign('tabHeader', empty($allTabs) ? FALSE : $allTabs); + $tabs = empty($allTabs) ? [] : \CRM_Core_Smarty::setRequiredTabTemplateKeys($allTabs); + $this->assign('tabHeader', $tabs); } } diff --git a/CRM/Contact/Page/View/Summary.php b/CRM/Contact/Page/View/Summary.php index ade42beb79f2..732bd34b8489 100644 --- a/CRM/Contact/Page/View/Summary.php +++ b/CRM/Contact/Page/View/Summary.php @@ -456,11 +456,7 @@ public function getTabs(array $contact) { } } - $expectedKeys = ['count', 'class', 'template', 'hideCount', 'icon']; - foreach ($allTabs as &$tab) { - // Ensure tab has all expected keys - $tab += array_fill_keys($expectedKeys, NULL); // Get tab counts last to avoid wasting time; if a tab was removed by hook, the count isn't needed. if (!isset($tab['count']) && isset($getCountParams[$tab['id']])) { $tab['count'] = call_user_func_array([ @@ -470,6 +466,9 @@ public function getTabs(array $contact) { } } + // ensure all keys used in the template are set, to avoid notices + $allTabs = \CRM_Core_Smarty::setRequiredTabTemplateKeys($allTabs); + // now sort the tabs based on weight usort($allTabs, ['CRM_Utils_Sort', 'cmpFunc']); return $allTabs; diff --git a/CRM/Contribute/Form/ContributionPage/TabHeader.php b/CRM/Contribute/Form/ContributionPage/TabHeader.php index 56aa9629fa48..057b1ff671db 100644 --- a/CRM/Contribute/Form/ContributionPage/TabHeader.php +++ b/CRM/Contribute/Form/ContributionPage/TabHeader.php @@ -31,6 +31,7 @@ public static function build(&$form) { $tabs = self::process($form); $form->set('tabHeader', $tabs); } + $tabs = \CRM_Core_Smarty::setRequiredTabTemplateKeys($tabs); $form->assign('tabHeader', $tabs); CRM_Core_Resources::singleton() ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header') diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index ee41275ecc3b..5b1b5f2ab9fa 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -806,7 +806,6 @@ public function buildForm() { // our ensured variables get blown away, so we need to set them even if // it's already been initialized. self::$_template->ensureVariablesAreAssigned($this->expectedSmartyVariables); - self::$_template->addExpectedTabHeaderKeys(); $this->_formBuilt = TRUE; } diff --git a/CRM/Core/Page.php b/CRM/Core/Page.php index 89965a18a608..21e9f718a910 100644 --- a/CRM/Core/Page.php +++ b/CRM/Core/Page.php @@ -218,8 +218,6 @@ public function run() { $pageTemplateFile = $this->getHookedTemplateFileName(); self::$_template->assign('tplFile', $pageTemplateFile); - self::$_template->addExpectedTabHeaderKeys(); - // invoke the pagRun hook, CRM-3906 CRM_Utils_Hook::pageRun($this); diff --git a/CRM/Core/Smarty.php b/CRM/Core/Smarty.php index 35602e7d9311..a0cc739ae9fa 100644 --- a/CRM/Core/Smarty.php +++ b/CRM/Core/Smarty.php @@ -277,23 +277,37 @@ public function ensureVariablesAreAssigned(array $variables): void { } /** - * Avoid e-notices on pages with tabs, - * by ensuring tabHeader items contain the necessary keys + * @deprecated + * Directly apply self::setRequiredTemplateTabKeys to the tabHeader + * variable */ public function addExpectedTabHeaderKeys(): void { + $tabs = $this->getTemplateVars('tabHeader'); + $tabs = self::setRequiredTabTemplateKeys($tabs); + $this->assign('tabHeader', $tabs); + } + + /** + * Ensure an array of tabs has the required keys to be passed + * to our Smarty tabs templates (TabHeader.tpl or Summary.tpl) + */ + public static function setRequiredTabTemplateKeys(array $tabs): array { $defaults = [ 'class' => '', 'extra' => '', - 'icon' => FALSE, - 'count' => FALSE, - 'template' => FALSE, + 'icon' => NULL, + 'count' => NULL, + 'hideCount' => FALSE, + 'template' => NULL, + // Afform tabs set the afform module and directive - NULL for non-afform tabs + 'module' => NULL, + 'directive' => NULL, ]; - $tabs = $this->getTemplateVars('tabHeader'); - foreach ((array) $tabs as $i => $tab) { - $tabs[$i] = array_merge($defaults, $tab); + foreach ($tabs as $i => $tab) { + $tabs[$i] = array_merge($defaults, (array) $tab); } - $this->assign('tabHeader', $tabs); + return $tabs; } /** diff --git a/CRM/Event/Form/ManageEvent.php b/CRM/Event/Form/ManageEvent.php index cabaa5b04eee..a7ce2340cf5f 100644 --- a/CRM/Event/Form/ManageEvent.php +++ b/CRM/Event/Form/ManageEvent.php @@ -399,6 +399,7 @@ private function build() { $tabs = $this->processTab(); $this->set('tabHeader', $tabs); } + $tabs = \CRM_Core_Smarty::setRequiredTabTemplateKeys($tabs); $this->assign('tabHeader', $tabs); CRM_Core_Resources::singleton() ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header') diff --git a/CRM/Event/Form/ManageEvent/TabHeader.php b/CRM/Event/Form/ManageEvent/TabHeader.php index 6625a7b35bfe..d5361d041994 100644 --- a/CRM/Event/Form/ManageEvent/TabHeader.php +++ b/CRM/Event/Form/ManageEvent/TabHeader.php @@ -37,6 +37,7 @@ public static function build(&$form) { $tabs = self::process($form); $form->set('tabHeader', $tabs); } + $tabs = \CRM_Core_Smarty::setRequiredTabTemplateKeys($tabs); $form->assign('tabHeader', $tabs); CRM_Core_Resources::singleton() ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')