You are here

function profile2_page_menu_local_tasks_alter in Profile 2 7

Same name and namespace in other branches
  1. 7.2 contrib/profile2_page.module \profile2_page_menu_local_tasks_alter()

Implements hook_menu_local_tasks_alter().

If viewing or editing a profile sub-tab, change the URL of the edit or view tab (if present) so it edits or views the profile, not the main account page. Subject to permissions.

File

contrib/profile2_page.module, line 140
Adds separate pages for viewing and editing profiles.

Code

function profile2_page_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  if (strpos($root_path, 'user/%/') === 0 && isset($router_item['original_map'][3])) {
    $ptype = $router_item['original_map'][3];
    $profile = profile2_type_load($ptype);

    // Only do this if the current profile is in use-tab mode.
    if (!empty($profile->data['use_tab'])) {
      switch ($router_item['original_map'][2]) {
        case 'view':
          $href_suff = '/edit';
          $ptype_pref = '';
          $access = 'edit';
          break;
        case 'edit':
          $href_suff = '';
          $ptype_pref = '/view';
          $access = 'view';
          break;
        default:
          return;
      }

      // Get the uid & profile type name.
      $uid = $router_item['original_map'][1];
      if (profile2_access($access, profile2_by_uid_load($uid, $ptype))) {

        // Get the part of the link URL containing "user/<uid>".
        $href = "{$router_item['tab_root_href']}{$href_suff}";

        //Walk the menu tree to find the account link, and append the profile type.
        array_walk_recursive($data, function (&$item) use ($href, $ptype_pref, $ptype) {
          if ($item == $href) {
            $item .= "{$ptype_pref}/{$ptype}";
          }
        });
      }
    }
  }
}