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

From aMember Pro Documentation
Jump to: navigation, search
(Remove a sub item)
 
(7 intermediate revisions by one user not shown)
Line 2: Line 2:
  
 
Add this code to [[site.php file]]:
 
Add this code to [[site.php file]]:
 +
 +
== Add single tab ==
 
<source>
 
<source>
<?php
+
Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
Am_Di::getInstance()->hook->add('userMenu', 'siteUserMenu');
+
    $user = $event->getUser();
function siteUserMenu(Am_Event $event)
+
    $menu = $event->getMenu();
{
+
      // $user = $event->getUser(); // if required
+
      $menu = $event->getMenu();
+
     
+
      // Add a single tab
+
      $menu->addPage(array(
+
              'id' => 'mypage',
+
              'label' => ___('Do The Thing'),
+
              'uri' => '/the.thing',
+
              'order' => 2,
+
      ));
+
  
      // Add a tab with subitems
+
    $menu->addPage(array(
      $menu->addPage(
+
        'id' => 'mypage',
          array(
+
        'label' => ___('Do The Thing'),
              'id' => 'with-subitem',
+
        'uri' => '/the.thing',
              'label' => 'With Subitems',
+
        'order' => 2,
              'uri' => '#',
+
    ));
              'pages' => array(
+
});
                    array(
+
</source>
                        'id' => 'subitem1',
+
                        'label' => 'Subitem 1',
+
                        'uri' => '/subitem-2'
+
                    ),
+
                    array(
+
                        'id' => 'subitem2',
+
                        'label' => 'Subitem 2',
+
                        'uri' => '/subitem-2'
+
                    )
+
              )
+
          )
+
      );
+
  
      // Remove a tab
+
== Add a tab with subitems ==
      $page = $menu->findOneBy('id', 'add-renew');
+
<source>
      $menu->removePage($page);
+
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'
 +
                )
 +
            )
 +
        )
 +
    );
 +
});
 +
</source>
 +
 
 +
== Remove a tab ==
 +
<source>
 +
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);
 +
    }
 +
});
 +
</source>
 +
 
 +
== Remove a sub item from tab ==
 +
<source>
 +
Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
 +
    $menu = $event->getMenu();
 
   
 
   
      // Add sub item to exiting tab
+
    if (($item = $menu->findOneById('aff')) && ($subitem = $menu->findOneById('aff-payout'))) {
      $page = $menu->findOneBy('id', 'aff'); // get affiliate page
+
        $item->removePage($subitem);
      $page->addPage(array(
+
    }
              'id' => 'my-subitem',
+
});
              'label' => ___('Subitem'),
+
</source>
              '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
+
== Add sub item to exiting 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'),
+
      $page = $menu->findOneById('aff'); // get affiliate page
              'uri' => '/the.thing',
+
      $page->addPage(array(
              'order' => 2,
+
          '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>

Latest revision as of 03:33, 26 July 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:

Contents

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);
    }
});

Remove a sub item from tab

Am_Di::getInstance()->hook->add(Am_Event::USER_MENU, function (Am_Event $event) {
    $menu = $event->getMenu();
 
    if (($item = $menu->findOneById('aff')) && ($subitem = $menu->findOneById('aff-payout'))) {
        $item->removePage($subitem);
    }
});

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,
        ));
    }
});