Skip to content

Commit

Permalink
PCHR-3429: Add weight to menu items added using navigationMenu hook
Browse files Browse the repository at this point in the history
This is necessary to keep the items in the expected order, because since
civicrm/civicrm-core#11772 menu item added with
the hook are now ordered by weight
  • Loading branch information
davialexandre committed Jul 2, 2018
1 parent 8ef0664 commit bbe604f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion uk.co.compucorp.civicrm.hrcore/hrcore.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ function _hrcore_createHelpMenu(&$menu) {
_hrcore_civix_insert_navigation_menu($menu, '', [
'name' => ts('Help'),
'permission' => 'access CiviCRM',
'weight' => _hrcore_getMaxMenuWeight($menu) + 1
]);

_hrcore_civix_insert_navigation_menu($menu, 'Help', [
Expand Down Expand Up @@ -466,7 +467,8 @@ function _hrcore_createDeveloperMenu(&$menu) {
_hrcore_civix_insert_navigation_menu($menu, '', [
'name' => ts('Developer'),
'permission' => 'access CiviCRM,access CiviCRM developer menu and tools',
'operator' => 'AND'
'operator' => 'AND',
'weight' => _hrcore_getMaxMenuWeight($menu) + 1
]);

_hrcore_civix_insert_navigation_menu($menu, 'Developer', [
Expand Down Expand Up @@ -540,5 +542,28 @@ function _hrcore_createSelfServicePortalMenu(&$menu) {
'name' => ts('ssp'),
'label' => ts('Self Service Portal'),
'url' => 'dashboard',
'weight' => _hrcore_getMaxMenuWeight($menu) + 1
]);
}

/**
* Returns the maximum weight among all the menu items in the given
* $menu array.
*
* @param array $menu
* An array in the same format as the one used by hook_civicrm_navigationMenu
*
* @return int
*/
function _hrcore_getMaxMenuWeight($menu) {
$maxWeight = 0;

foreach ($menu as $item) {
$itemWeight = !empty($item['attributes']['weight']) ? $item['attributes']['weight'] : 0;
if ($itemWeight > $maxWeight) {
$maxWeight = $itemWeight;
}
}

return $maxWeight;
}

0 comments on commit bbe604f

Please sign in to comment.