1

I am trying to add a menu tab on the user profile page with the # of blog posts someone has made.

Ex: Blogs (3)

/**
* Implementation of hook_menu
*/
function menutab_followers_menu() {
$view = views_get_view( 'blogs_written' ); 
$view->execute(); 
$count = count( $view->result ); 
    $items['user/%user/blogs'] = array(
        'title' => t('Blogs @count', array('@count' => $count)),
        'page callback' => 'menutab_blogs_linkspage',
        'page arguments' => array(1),
        'access callback' => TRUE,
        'type' => MENU_LOCAL_TASK,
        );        
    return $items;
}

For some reason it won't use the contextual filter. It displays Blogs (0) instead of Blogs (3) I have it set the views contextual filter to raw value, path 2, and use url alias. Ex: mywebsite.com/users/admin

If I insert the view in the profile page using Display Suite, it works fine, it displays all 3 items on the page. Also, if I set the contextual filter to a fix value and type "Admin" it displays Blogs (3) on the menu tab. But I don't want that, since it would display that to every user.

2

Use the "title callback" menu property instead of "title", and then use a function to generate the title. Before executing the view, set the argument as shown in the example below.

function menutab_followers_menu() {
    $items = array();
    $items['user/%user/blogs'] = array(
        'title callback' => 'menutab_followers_blogs_title_callback',
        'title arguments' => array(1),
        'page callback' => 'menutab_blogs_linkspage',
        'page arguments' => array(1),
        'access callback' => TRUE,
        'type' => MENU_LOCAL_TASK,
    );        
    return $items;
}

function menutab_followers_blogs_title_callback($account) {
    $view = views_get_view('blogs_written');
    $view->set_arguments(array($account->uid));
    $view->execute(); 
    $count = count($view->result);
    return t('Blogs @count', array('@count' => $count));
}

Also, instead of executing the view to get the count, a better approach would be to use a simple database query. An example would be:

$count = db_query("SELECT COUNT(*) FROM node WHERE uid = :uid AND type = :type", array(':uid' => $account->uid, ':type' => 'blog'))->fetchField();
  • Do you know how to add html inside the t function? I want to add a <br> tag before the @count. I tried adding an echo"<br>" before the count, but it doesn't work it just outputs it as Blogs echo"<br>" 3 on the tab tittle. – No Sssweat Jun 8 '14 at 6:20
  • t() should allow <br> in the text without using echo. It's likely that Drupal menu tabs can't have new lines by default. There might be a theme hook you can use to add the new line, but it seems like it'll be a lot of work for a small change. – Bryan Jiencke Jun 8 '14 at 15:01
  • yeah, even without the echo it doesn't work, it just outputs it as <br>3. I'll look into the theme hook. – No Sssweat Jun 9 '14 at 1:24
  • not sure if you can help me with this one drupal.stackexchange.com/questions/118141/quick-tab-with-count – No Sssweat Jun 13 '14 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.