Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] use common helper to set expected keys for tabs in Summary.tpl and TabHeader.tpl #31636

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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