1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to set add-on's navtab permission based

Discussion in 'XenForo Development Discussions' started by Dadparvar, Mar 23, 2016.

  1. Dadparvar

    Dadparvar Well-Known Member

    Hi,

    My add-on has a navigation tab + child tabs.

    I can set permission for usergroups to set if they can see child tabs or not.

    But how to set Add-on's main tab in navbar permission based?

    Regards
     
  2. wang

    wang Well-Known Member

    You must set the permission in the Listeners file where you have added the code for the navigation tab.
     
    Dadparvar likes this.
  3. Dadparvar

    Dadparvar Well-Known Member

    Would you mind providing an example code for that?
     
  4. wang

    wang Well-Known Member

    You can add the permission conditional before the $extraTabs array.

    Can you please post here the code that you are using to add the tab?
     
  5. Dadparvar

    Dadparvar Well-Known Member

    I created tabs for my add-on using a route.
    Code:
    <?php
    
    class Path_To_Route_Prefix_Index implements XenForo_Route_Interface
    {
        public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
        {
            $action = $router->resolveActionWithIntegerParam($routePath, $request, 'template_id');
                    $routeMatch = $router->getRouteMatch('Path_To_ControllerPublic_Index', $action, 'tabname', $routePath);
                    return $routeMatch;
        }
    }
     
  6. wang

    wang Well-Known Member

    I was talking about the code that adds the tab in the navigation menu, not the router.
     
  7. Dadparvar

    Dadparvar Well-Known Member

    Oosp!
    the navigation.php file:
    Code:
    <?php
    
    class Path_to_Listeners_Navigation
    {
        public static function navtabs(array &$extraTabs, $selectedTabId)
        {
            $extraTabs['tabname'] = array(
                'title' => 'Name',
                'href' => 'name/',
                'selected' => ($selectedTabId == 'newsticker'),
                'linksTemplate' => 'my_temp_Navtabs',
            );
        }
    }
    and the template:
    Code:
    <ul class="secondaryContent blockLinksList">
        <li><a href="{xen:link pageofit/}">Title</a></li>
    </ul>
     
  8. wang

    wang Well-Known Member

    PHP:
    public static function navtabs(array &$extraTabs$selectedTabId)
    {
       if (
    XenForo_Visitor::getInstance()->hasPermission(permissionGroup', 'permissionName'))
       {
            $extraTabs['
    tabname'] = array(
                '
    title' => 'Name',
                '
    href' => 'name/',
                '
    selected' => ($selectedTabId == 'newsticker'),
                '
    linksTemplate' => 'my_temp_Navtabs',
            );
       }
    }
     
    katsulynx, Dadparvar and Lawrence like this.
  9. Dadparvar

    Dadparvar Well-Known Member

    Awesome.

    Worked.

    Thanks
     
    wang likes this.
  10. Chris D

    Chris D XenForo Developer Staff Member

    You should be using the XF link builder instead of hard coding the URL like this:
    PHP:
    'href' => 'name/'
    The correct way is:
    PHP:
    'href' => XenForo_Link::buildPublicLink('name')
    (It takes the same arguments as the {xen:link} template function).

    Your href won't work for people without friendly URLs enabled and if the route was ever changed with a route filter, that wouldn't work properly either.
     
    lsxforo, Thomas.B, katsulynx and 2 others like this.
  11. wang

    wang Well-Known Member

    You are welcome sir.
     
  12. Lawrence

    Lawrence Well-Known Member

    Good catch, @Chris D, and thanks for taking the time to explain why xen:link should be used.
     
    lsxforo likes this.

Share This Page