Skip to content

Commit

Permalink
Merge pull request #31636 from ufundo/tab-template-var-cleanup
Browse files Browse the repository at this point in the history
[REF] use common helper to set expected keys for tabs in Summary.tpl and TabHeader.tpl
  • Loading branch information
colemanw authored Dec 19, 2024
2 parents 3c39513 + 7e4582c commit c6a3332
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions CRM/Campaign/Form/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions CRM/Campaign/Form/Survey/TabHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion CRM/Campaign/Page/Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
7 changes: 3 additions & 4 deletions CRM/Contact/Page/View/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions CRM/Contribute/Form/ContributionPage/TabHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 0 additions & 1 deletion CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 0 additions & 2 deletions CRM/Core/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
32 changes: 23 additions & 9 deletions CRM/Core/Smarty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions CRM/Event/Form/ManageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions CRM/Event/Form/ManageEvent/TabHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit c6a3332

Please sign in to comment.