This topic is locked

Hide item in menu

7/22/2025 2:41:20 PM
PHPRunner General questions
G
GuilleSaa author

Hi

In the Menu Editor, I have an item that references an external page called PaginaLibre.
I need to hide this item from the menu when the user is not an administrator.
Security is dynamic; the GroupID (1 or -1) is found in the cvduy_gymugmembers table.

I have tried several options with the help of ChatGPT without success.
In Events - Edit Menu
One of the attempts was ...

function removeMenuItemByTitleOrHref(&$items, $value) {
foreach ($items as $key => &$item) {
if ((isset($item['title']) && $item['title'] == $value) ||
(isset($item['href']) && $item['href'] == $value)) {
unset($items[$key]);
} elseif (isset($item['children'])) {
removeMenuItemByTitleOrHref($item['children'], $value);
}
}
}

$username = Security::getUserName();
$sql = "SELECT GroupID FROM cvduy_gymugmembers WHERE UserName = " . db_prepare_string($username);
$groupId = DB::Lookup($sql);

if ($groupId != -1) {
// Intentamos eliminar por título y por href
removeMenuItemByTitleOrHref($menu->Items, "PaginaLibre");
removeMenuItemByTitleOrHref($menu->Items, "pagina_libre.php");
}

Thanks in advance

Guillermo

ChatGPT 7/22/2025

Hi Guillermo,

It looks like you are trying to dynamically hide a menu item in PHPRunner based on the user's group ID. Your approach is generally correct, but there are a few things to double-check and improve to ensure that it works as expected.

Here’s a refined version of your code along with some suggestions:

  1. Ensure the Event is Correct: Make sure that you are placing this code in the correct event. The "Before Display" event or the "Edit Menu" event would be appropriate.


  2. Check the Menu Item Title and Href: Ensure that the titles and hrefs you are using for comparison are exactly as they appear in your menu. Check for case sensitivity and any leading/trailing spaces.


  3. Debugging: You might want to add debugging statements (like print_r($items) or var_dump($groupId)) to see if the values are what you expect.



Here’s an updated version of your code that takes these considerations into account:

function removeMenuItemByTitleOrHref(&$items, $value) {
foreach ($items as $key => &$item) {
if ((isset($item['title']) && $item['title'] == $value) ||
(isset($item['href']) && $item['href'] == $value)) {
unset($items[$key]);
} elseif (isset($item['children'])) {
removeMenuItemByTitleOrHref($item['children'], $value);
}
}
}

// Get the current username
$username = Security::getUserName();

// Fetch the GroupID for the user from the database
$sql = "SELECT GroupID FROM cvduy_gymugmembers WHERE UserName = " . db_prepare_string($username);
$groupId = DB::Lookup($sql);

// Check if the user is not an administrator
if ($groupId != -1) {
// Remove the menu item by title and href if the user is not an admin
removeMenuItemByTitleOrHref($menu->Items, "PaginaLibre");
removeMenuItemByTitleOrHref($menu->Items, "pagina_libre.php");
}

Additional Considerations:

  • Check Database Connection: Ensure that your database connection is working and the query returns the expected GroupID.
  • Menu Structure: If your menu has nested items or complex structures, ensure that the function is correctly traversing the entire menu structure.
  • Access Control: If PHPRunner has its own access control mechanisms, ensure that your custom logic does not conflict with that.

Debugging Steps:

  • Add debugging statements to check if the GroupID is being fetched correctly.
  • Print the $menu->Items before and after the modification to see if the item is being removed.
  • Verify that "PaginaLibre" and "pagina_libre.php" are the exact titles/links used in your menu.

If you follow these guidelines, you should be able to successfully hide the specified menu item based on the user's group ID. Let me know if you need further assistance!

G
GuilleSaa author 7/22/2025

This option is already process without exit.
The three points to watch display the correct data