Difference between revisions of "How to customize user tabs in member's area"

From aMember Pro Documentation
Jump to: navigation, search
Line 2: Line 2:
  
 
Add this code to [[site.php file]]:
 
Add this code to [[site.php file]]:
 +
 +
===Add single tab
 
<source>
 
<source>
 
Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
 
Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
      // $user = $event->getUser(); // if required
+
    $user = $event->getUser();
      $menu = $event->getMenu();
+
    $menu = $event->getMenu();
  
      // Add a single tab
+
    $menu->addPage(array(
      $menu->addPage(array(
+
        'id' => 'mypage',
              'id' => 'mypage',
+
        'label' => ___('Do The Thing'),
              'label' => ___('Do The Thing'),
+
        'uri' => '/the.thing',
              'uri' => '/the.thing',
+
        'order' => 2,
              'order' => 2,
+
    ));
      ));
+
});
 +
</source>
  
      // Add a tab with subitems
+
===Add a tab with subitems
      $menu->addPage(
+
<source>
          array(
+
Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
              'id' => 'with-subitem',
+
    $user = $event->getUser();
              'label' => 'With Subitems',
+
    $menu = $event->getMenu();
              'uri' => '#',
+
              'pages' => array(
+
                    array(
+
                        'id' => 'subitem1',
+
                        'label' => 'Subitem 1',
+
                        'uri' => '/subitem-2'
+
                    ),
+
                    array(
+
                        'id' => 'subitem2',
+
                        'label' => 'Subitem 2',
+
                        'uri' => '/subitem-2'
+
                    )
+
              )
+
          )
+
      );
+
  
      // Remove a tab
+
    $menu->addPage(
      if ($page = $menu->findOneById('add-renew')) {
+
        array(
          $menu->removePage($page)
+
            'id' => 'with-subitem',
      }
+
            'label' => 'With Subitems',
+
            'uri' => '#',
      // Add sub item to exiting tab
+
            'pages' => array(
      $page = $menu->findOneById('aff'); // get affiliate page
+
                array(
      $page->addPage(array(
+
                    'id' => 'subitem1',
              'id' => 'my-subitem',
+
                    'label' => 'Subitem 1',
              'label' => ___('Subitem'),
+
                    'uri' => '/subitem-2'
              'uri' => '/sub.item',
+
                ),
              'target' => '_blank', // you can set target attribute if necessary
+
                array(
              'order' => 10,
+
                    'id' => 'subitem2',
      ));
+
                    'label' => 'Subitem 2',
 +
                    'uri' => '/subitem-2'
 +
                )
 +
            )
 +
        )
 +
    );
 +
});
 +
</source>
  
      //Add a tab based on user access, if user has active subscription to product with # 1
+
===Remove a tab
       /* @var $user User */
+
<source>
      $user = $event->getUser();
+
Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
      if (in_array(1, $user->getActiveProductIds())) {
+
    $user = $event->getUser();
          $menu->addPage(array(
+
    $menu = $event->getMenu();
              'id' => 'mypage',
+
 
              'label' => ___('Do The Thing'),
+
    if ($page = $menu->findOneById('add-renew')) {
              'uri' => '/the.thing',
+
        $menu->removePage($page)
              'order' => 2,
+
    }
          ));
+
});
      }
+
</source>
 +
 
 +
===Add sub item to exiting tab
 +
<source>
 +
Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
 +
      $user = $event->getUser();
 +
      $menu = $event->getMenu();
 +
 
 +
      $page = $menu->findOneById('aff'); // get affiliate page
 +
      $page->addPage(array(
 +
          'id' => 'my-subitem',
 +
          'label' => ___('Subitem'),
 +
          'uri' => '/sub.item',
 +
          'target' => '_blank', // you can set target attribute if necessary
 +
          'order' => 10,
 +
      ));
 +
});
 +
</source>
 +
 
 +
===Add a tab based on user access, if user has active subscription to product with # 1
 +
<source>
 +
Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
 +
    $user = $event->getUser();
 +
    $menu = $event->getMenu();
 +
        
 +
    /* @var $user User */
 +
    $user = $event->getUser();
 +
    if (in_array(1, $user->getActiveProductIds())) {
 +
        $menu->addPage(array(
 +
            'id' => 'mypage',
 +
            'label' => ___('Do The Thing'),
 +
            'uri' => '/the.thing',
 +
            'order' => 2,
 +
        ));
 +
    }
 
});
 
});
 
</source>
 
</source>

Revision as of 11:10, 11 May 2016

Let's say you want to add a tab to the member's area and remove the Add/Renew tab.

Add this code to site.php file:

===Add single tab

Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
    $user = $event->getUser();
    $menu = $event->getMenu();
 
    $menu->addPage(array(
        'id' => 'mypage',
        'label' => ___('Do The Thing'),
        'uri' => '/the.thing',
        'order' => 2,
    ));
});

===Add a tab with subitems

Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
    $user = $event->getUser();
    $menu = $event->getMenu();
 
    $menu->addPage(
        array(
            'id' => 'with-subitem',
            'label' => 'With Subitems',
            'uri' => '#',
            'pages' => array(
                array(
                    'id' => 'subitem1',
                    'label' => 'Subitem 1',
                    'uri' => '/subitem-2'
                ),
                array(
                    'id' => 'subitem2',
                    'label' => 'Subitem 2',
                    'uri' => '/subitem-2'
                )
            )
        )
    );
});

===Remove a tab

Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
    $user = $event->getUser();
    $menu = $event->getMenu();
 
    if ($page = $menu->findOneById('add-renew')) {
        $menu->removePage($page)
    }
});

===Add sub item to exiting tab

Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
      $user = $event->getUser();
      $menu = $event->getMenu();
 
      $page = $menu->findOneById('aff'); // get affiliate page 
      $page->addPage(array(
          'id' => 'my-subitem',
          'label' => ___('Subitem'),
          'uri' => '/sub.item',
          'target' => '_blank', // you can set target attribute if necessary
          'order' => 10,
      ));
});

===Add a tab based on user access, if user has active subscription to product with # 1

Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
    $user = $event->getUser();
    $menu = $event->getMenu();
 
    /* @var $user User */
    $user = $event->getUser();
    if (in_array(1, $user->getActiveProductIds())) {
        $menu->addPage(array(
            'id' => 'mypage',
            'label' => ___('Do The Thing'),
            'uri' => '/the.thing',
            'order' => 2,
        ));
    }
});