Need help with if(IsMobile) add link if(NoMobile) remove link ?
I am trying to figure out how to remove a link from a plugin from the menu if the device is not mobile. I came up with this pitiful try... Am I missing something?
I want the link to be removed if the person is not on a mobile. Help this wretched hack if you have the time...thanks
public function Base_Render_Before($Sender) { $Session = Gdn::Session(); if(IsMobile())&& if($Sender->Menu) { $Sender->Menu->AddLink('MobileHome', Img('/plugins/MobileHome/mhicon.png', array('alt' => T('MobileHome'))), '/mobilehome', array('class' => 'MobileHome')); else($Sender->Menu->RemoveLink('MobileHome', Img('/plugins/MobileHome/mhicon.png', array('alt' => T('MobileHome'))), '/mobilehome', array('class' => 'MobileHome'))); } }
Best Answer
-
businessdad MVP
@vrijvlinder said:
I am trying to figure out how to remove a link from a plugin from the menu if the device is not mobile. I came up with this pitiful try... Am I missing something?You only have to do one of two things:
If the menu entry is there by default, remove it when not on mobile.
ORIf the menu entry is not there by default, add it.
There is no need to both add and remove, as the changes you make to the menu are not persistent, i.e. the menu will be in its initial state at every page reload. This said, the following should work (in the hypothesis where you need to add the menu item):
public function MenuModule_BeforeToString_Handler($Sender) { // The event is fired by all the menus when they are rendered. Using HtmlId, // we can distinguish which menu fired it and alter it accordingly if(($Sender->HtmlId == 'TheMenuWeWantToAlter') && IsMobile()) { $Sender->AddLink('MobileHome', Img('/plugins/MobileHome/mhicon.png', array('alt' => T('MobileHome'))), '/mobilehome', array('class' => 'MobileHome')); } }
That's it. The menu item will be added to the menu when you are in mobile mode. In normal mode, it simply won't be added (no need to remove it).
2
Answers
You only have to do one of two things:
If the menu entry is there by default, remove it when not on mobile.
OR
If the menu entry is not there by default, add it.
There is no need to both add and remove, as the changes you make to the menu are not persistent, i.e. the menu will be in its initial state at every page reload. This said, the following should work (in the hypothesis where you need to add the menu item):
That's it. The menu item will be added to the menu when you are in mobile mode. In normal mode, it simply won't be added (no need to remove it).
My shop | About Me
@businessdad
Well I'll be dammed !! (shock and amazement face) , that worked for sure !!! That was the missing link to my plugin. Thank you very much daddy !!
❌ ✊ ♥. ¸. ••. ¸♥¸. ••. ¸♥ ✊ ❌