Custom Nav-Links: Find TableGroup-index by table name

You probably know the possibility of adding your own links to the menus. A small disadvantage is that you have to give the target menu as a number (index of the menu). However, the number varies depending on user rights. Wouldn't it be nice to have the index determined automatically? Here I show a small PHP function for this.

Custom Menu Items

According to the specs you can add custom nav-links by adding array items to the $navLinks array in hooks/links-navmenu.php.

$navLinks[] = [
		'url' => 'tasks_by_status_and_category.php',
		'title' => 'Tasks by Status & Category',
		'groups' => ['*'],
		'icon' => '...', // path to image
		'table_group' => 1
	];

The setting 'table_group' => 1 causes the new menu entry to be inserted in the second menu, which is the menu having index = 1.

Caution:
index starts at 0.

  • 1. Protokoll Index = 0
  • 2. Aufgaben Index = 1 RIGHT!
  • 3. Abonnenten Index = 2
  • 4. Konfiguration Index = 3

The problem

But what if the current user does not have permissions on any of the tables in TableGroup[0]?

In these cases the first menu "Protokoll" will not be there, so indexes shift by one.

  • Protokoll
  • 1. Aufgaben Index = 0
  • ...

As long as we keep our setting 'table_group' => 1 your custom menu item will be inserted in the wrong menu:

That's not what we want.

Idea

So I came up with the idea of finding out the menu index by code. This is not so difficult if you know the functions that can read out table and group information.

I have decided that the new menu entry should always be in the TableGroup in which the tasks table is located. I use a function-call to find the TableGroup index for this table.

This has several advantages: The code reacts automatically to shifts in the indexes. And if I move the table tasks to another group one day, my custom nav links will automatically "move" with it.

If you like my contributions, I would appreciate a small one-time donation or even a small monthly contribution! With this you can give back some appreciation and support my work on this blog as well as on the AppGini Forum.

It also always helps me a little bit when you share posts on your social media.

Many thanks for your support.





PHP-Code

Here is my code:

<?php
// file: hooks/links-navmenu.php
function getTableGroupIndexByTableName($tn, $default = null)
{
	$i = 0;
	foreach (get_table_groups(false) as $tgc => $tns) {
		if (in_array($tn, $tns)) return $i;
		$i++;
	}
	return $default;
}

$tgi =  getTableGroupIndexByTableName("tasks");

if ($tgi !== null) {
	$navLinks[] = [
		'url' => 'tasks_by_status_and_category.php',
		'title' => 'Tasks by Status & Category',
		'groups' => ['*'],
		'icon' => 'data:image/png;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ImHV3ragSlWG0/Z//MAPZC/a7aRzeB7NAG4N0nTheJYzvQz7PWKkIE2/7dxVSn3Cftm21skIktZ3QlqXHTXT0RCbzGWD8f+z//8d/ANd6hKlHjF1zAAAAAElFTkSuQmCC',
		'table_group' => $tgi
	];
}

See the function in lines 3 to 11 and the function call, passing tablename tasks, in line 13.

Result

I have created a new usergroup which restricted access. A user of that group cannot see other tables, so the menu and menu-indexes have shifted.

Anyway, due to my code above the custom nav link has been inserted in the correct menu:

The three other menus are missing due to group-permissions, but the custom menu item is still in the correct place under tasks table. That feels much better.

I hope you like it.

Do you like it?