Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Need help with if(IsMobile) add link if(NoMobile) remove link ?

vrijvlindervrijvlinder Papillon-Sauvage MVP
edited April 2013 in Vanilla 2.0 - 2.8

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

  • Options
    businessdadbusinessdad Stealth contributor MVP
    edited April 2013 Answer ✓

    @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.
      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):

    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).

Answers

  • Options
    businessdadbusinessdad Stealth contributor MVP
    edited April 2013 Answer ✓

    @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.
      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):

    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).

  • Options
    vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited April 2013

    @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 !!

Sign In or Register to comment.