ElkArte Community

Elk Development => Bug Reports => Exterminated Bugs => Topic started by: IchBin on November 30, 2013, 12:56:49 am

Title: integrate_menu_buttons add to admin item
Post by: IchBin on November 30, 2013, 12:56:49 am
If I understand this right, should I be able to add sub item to the admin menu by simply doing the following?

Code: [Select]
		$menu->childOf('admin')->add('admin_sub_buttons')->addBulk(
array(
'myadmin' => array(
'title' => 'My Admin',
'href' => $scripturl. '?action=myadmin',
'show' => true,
)
)
);

This is not working for me. And I've tried it multiple ways to see if I'm just not understanding the code. What I did find out somehow though, is that if I have the above code executed twice (right in a row), my item actually shows up at the bottom of the admin menu.  Very weird. Not following this logic at the moment, so I'm calling bug. :D
Title: Re: integrate_menu_buttons add to admin item
Post by: emanuele on November 30, 2013, 03:58:43 am
Yes, I was playing with it yesterday and I noticed I forgot to add "a bit of code"... O:-)

At what level do you want to add the button? First level?
Title: Re: integrate_menu_buttons add to admin item
Post by: emanuele on November 30, 2013, 06:24:21 am
Here it is:
https://github.com/emanuele45/Dialogo/commit/4c1aa6f8ea1dfb6e67219fd1aa89e4ebad322c36

Example of how to add a new item to any menu:
Code: [Select]
// Grab all the menus we have
$allMenus = Standard_Menu::context();

// Find the admin menu
$admin_menu = $allMenus->get('Admin_Menu');

// Find the submenu you want (this get is a shortcut that will find menus at any nesting level and any position,
// if a sort of getElementById in javascript terms.
// Though if you know where your menu is you can do the "extended" version with childOf that should be slightly faster
$submenu = $admin_menu->get('manageboards_sub');

// Add the new menu item
$submenu->add('my_new_menu', array($txt['menu_name'], 'optional_permissions'));

This is the very basic usage.

Then you may want to add for example an entire new menu at the first level, after config, with a droppy with a submenu, then you should do something like:
Code: [Select]
// Grab all the menus we have
$allMenus = Standard_Menu::context();

// Find the admin menu
$admin_menu = $allMenus->get('Admin_Menu');

// Add the "visible" item (i.e.the label)
$admin_menu->after('config')->add('your_main_item', array(
'title' => $txt['main_item_name'],
'permission' => array('admin_forum')
));

// Now create your new sub-menu and add it to the main one:
$my_item = $admin_menu->childOf('forum')->add('your_main_item');

// Finally populate it. addBulk is a shortcut (again) for add that loops through an array of menus and adds them one by one)
$my_item->addBulk(
array(
'index' => array(
'label' => $txt['admin_center'],
'controller' => 'Admin_Controller',
'function' => 'action_home',
'icon' => 'transparent.png',
'class' => 'admin_img_administration',
),
)
);

Now that I look at the code, I think the first two lines could be "merged" into a single call... Will work on that.

There is still a distinction between a "menu" (i.e. a "collection" of entries) and an "entry" (i.e. the "text" visible in the menu/droppy), and the two behave slightly differently (actually quite a bit differently). I think this should be polished up, not sure yet how... :-[

Yes, it's not the same as an array. :P
Title: Re: integrate_menu_buttons add to admin item
Post by: emanuele on November 30, 2013, 06:35:43 am
Thinking even more about this: if in 1.1 we are going to add a menu editor (are we, isn't it? O:-)), I'm tempted to just come back to the "old" simple array.
This class (properly finished and functioning) make sense if the entries are not manipulated from the database, but if in the round of a minor version we are anyway changing it it seems pretty useless...

I have no ego problems, so speak now or live with it for 1.0. :P
Title: Re: integrate_menu_buttons add to admin item
Post by: Spuds on November 30, 2013, 10:37:41 am
Looks like I picked the wrong week to quit sniffing glue

Looks like I picked the wrong week to quit drinking.

Looks like I picked the wrong week to quit smoking.

Looks like I picked the wrong week to quit amphetamines

http://www.youtube.com/watch?v=lyhaTQseKTQ
Title: Re: integrate_menu_buttons add to admin item
Post by: IchBin on November 30, 2013, 11:32:33 am
lol...

I'd say whatever makes the most sense that will help with the next evolution. But then again, I'm not the one coding it here. lol
Why doesn't it lend itself well to pulling the menu from the database. Assuming we cached it, or threw it into it's own file after writing to the database. Couldn't this version be used to populate the menu in the same way?
Title: Re: integrate_menu_buttons add to admin item
Post by: IchBin on November 30, 2013, 12:30:31 pm
BTW, I was talking about the main menu admin items here, not the Admin menu in admin. How does one add something as a sub menu item of the main menu Admin button? I can't seem to figure out how to grab the sub items and add something to it.

I should say though, even as complicated as this had made the menu code, this really rocks. Being able to pop and item on or off etc is really easy. I just haven't figured out the sub items part. lol
Title: Re: integrate_menu_buttons add to admin item
Post by: emanuele on November 30, 2013, 06:48:54 pm
Quote from: IchBin – I'd say whatever makes the most sense that will help with the next evolution. But then again, I'm not the one coding it here. lol
Why doesn't it lend itself well to pulling the menu from the database. Assuming we cached it, or threw it into it's own file after writing to the database. Couldn't this version be used to populate the menu in the same way?
It could be used, yes, but it wouldn't make much sense.
I thought this version to be able to target better specific entries in the menu via code, but if we move the beast to the database then it is "just" a grab and populate, nothing more and any addon that want to add a menu item will just add it directly to the database, similarly to an updateSettings, I don't see any reason to use hooks or other similar things just to add a menu.

Okay, tomorrow I'll push the reverts. ;)