1

I am trying to include a Bootstrap style dropdown in the local_tasks tabs on a section of my website. I thought I might be able to accomplish this with the 'theme callback' option of 'HOOK_MENU', but it doesn't run the custom callback. Any ideas on a good way to accomplish this?

Menu:

    $items['module/submenu/tab'] = array(
    'title' => 'Title',
    'page callback' => 'mymodule_menu_callback',
    'access arguments' => array('access content'),
    'theme callback' => 'mymodule_menu_local_task_dropdown',
    'type' => MENU_LOCAL_TASK,
);

Callback:

function mymodule_menu_local_task_dropdown($variables) {
 $link = $variables['element']['#link'];
 $link_text = $link['title'];
 $classes = array();

 if (!empty($variables['element']['#active'])) {
 // Add text to indicate active tab for non-visual users.
 $active = '<span class="element-invisible">' . t('(active tab)') . '</span>';

 // If the link does not contain HTML already, check_plain() it now.
 // After we set 'html'=TRUE the link will not be sanitized by l().
 if (empty($link['localized_options']['html'])) {
  $link['title'] = check_plain($link['title']);
 }
 $link['localized_options']['html'] = TRUE;
 $link_text = t('!local-task-title!active', array('!local-task-title' => $link['title'], '!active' => $active));

 $classes[] = 'active';
}

$classes[] = 'dropdown';

//haven't written this part yet
$dropdown = 'build dropdown';

return '<li class="' . implode(' ', $classes) . '">' . l($link_text, $link['href'], $link['localized_options']) . $dropdown . "</li>\n";
}

What I'm looking for:

tabs with dropdown

0

For the drop-down menu you need include drop-down JavaScript in the "page callback". See drupal_add_library() function.

And I recommend for you pay attention to "CTools module".

"Drop-Down Menu" Code from CTools module:

  // Show examples of ctools javascript widgets
  $output .= '<h2>' . t('CTools Javascript Widgets') . '</h2>';

  // Create a drop down menu
  $links = array();
  $links[] = array(
    'title' => t('Link 1'),
    'href' => $_GET['q'],
  );
  $links[] = array(
    'title' => t('Link 2'),
    'href' => $_GET['q'],
  );
  $links[] = array(
    'title' => t('Link 3'),
    'href' => $_GET['q'],
  );

  $output .= '<h3>' . t('Drop Down Menu') . '</h3>';
  $output .= theme('ctools_dropdown', array('title' => t('Click to Drop Down'), 'links' => $links));
  • Bootstrap loads the javascript necessary to create the dropdown, my problem is integrating it into Drupal's menu_local_task system. I don't think this is quite the solution I'm looking for. Thanks! – rocketsurgeon Jul 2 '14 at 16:25
0

I have figured out a way to do it for now, but doesn't seem like a great solution and adds two calls to every tab rendered. Since I wasn't able to get the custom callback to work, I overrode theme_menu_local_task with some ideas from https://stackoverflow.com/questions/2716787/how-to-get-all-the-menu-items-below-a-certain-parent-in-drupal. Basically I test the path for children and output the dropdown if they exist.

    function mymodule_menu_local_task($variables) {

    $link = $variables['element']['#link'];
    $link_text = $link['title'];
    $classes = array();

    if (!empty($variables['element']['#active'])) {
        // Add text to indicate active tab for non-visual users.
        $active = '<span class="element-invisible">' . t('(active tab)') . '</span>';

        // If the link does not contain HTML already, check_plain() it now.
        // After we set 'html'=TRUE the link will not be sanitized by l().
        if (empty($link['localized_options']['html'])) {
            $link['title'] = check_plain($link['title']);
        }
        $link['localized_options']['html'] = TRUE;
        $link_text = t('!local-task-title!active', array('!local-task-title' => $link['title'], '!active' => $active));

        $classes[] = 'active';
    }

    //check to see if menu item has children
    $parent = menu_link_get_preferred($variables['element']['#link']['path']);
    $parameters = array(
        'active_trail' => array($parent['plid']),
        'only_active_trail' => FALSE,
        'min_depth' => $parent['depth']+1,
        'max_depth' => $parent['depth']+1,
        'conditions' => array('plid' => $parent['mlid']),
    );
    $children = menu_build_tree($parent['menu_name'], $parameters);

    //if item has children, output as a dropdown
    if (!empty($children)) {
        $classes[] = 'download';
        $link['localized_options']['html'] = TRUE;
        $link['localized_options']['attributes']['data-toggle'] = 'dropdown';
        $link['localized_options']['attributes']['class'] = 'dropdown-toggle';
        $dropdown = '';
        foreach ($children as $item) {
            $dropdown .= '<li>'. l($item['link']['link_title'], $item['link']['link_path']) .'</li>';
        }
        $dropdown = '<ul class="dropdown-menu" role="menu">'. $dropdown .'</ul>';
        return '<li class="' . implode(' ', $classes) . '">' . l($link_text.'<span class="caret"></span>', $link['href'], $link['localized_options']) . $dropdown . "</li>\n";
    } else {
        return '<li class="' . implode(' ', $classes) . '">' . l($link_text, $link['href'], $link['localized_options']) . "</li>\n";
    } 
}

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.