How to turn a view into tab for a specific node type only?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
kirilius's picture

Hello,

I have a view and the goal is to turn it into a tab that shows only for a specific node type. Here is what I do:

I have a page view called for e.g. x_pictures that is supposed to display all the pictures associated with a given node of type X. The URL I have given to it is node/$arg/pictures
In the "Menu" section of the page definition I have the following settings:

Provide Menu - checked
Provide Menu as Tab - checked
Make Default Menu Tab - NOT checked
Parent Menu Item Type - Already exists (don't create)

The view works fine and shows as a separate tab but on EVERY content type that I have. My goal is to have it displayed only for nodes of type X. How can I do that?

Thanks for the help!

Comments

Views Tabs?

mikey_p's picture

I'm not quite sure I understand you, but take a look at http://drupal.org/project/views_tabs. It may do exactly what you need. Let us now if it does or doesn't.

It is not possible. I did

gordon's picture

It is not possible. I did submit a patch a while back which allowed a snippet of php to be added to the menu assignment, but this was not really accepted.see http://drupal.org/node/64575

--
Gordon Heydon

There is an undocumented and

merlinofchaos's picture

There is an undocumented and not entirely complete feature that you can use.

In place of $arg, put $node-CONTENTTYPE

But be careful that certain parts will generate incorrect URLs (because the code has not been adjusted to understand that $arg isn't the only replacement possible now) -- this is mostly only a problem with RSS feeds.

>In place of $arg, put

yngvewb's picture

In place of $arg, put $node-CONTENTTYPE

Wow! This actually works!

In the view URL I put "$node-event/myview" and it only shows upp on content type event :-)

It also works with

DevElCuy's picture

It also works with "node/$node-event/mytab".

--
develCuy's fruits
(3 John 1:2) Dear friend, I pray that you may enjoy good health and that all may go well with you, even as your soul is getting along well.

--
[develCuy](http://steemit.com/@develcuy) on steemit

Custom menu hook

Crell's picture

What I've done in the past for that is to define the view without specifying the "Real" path I want it to live at, then in my hook_menu() implementation I add an entry for node/arg(1)/myview if the node is of the type I want. Then just specify the view_page() callback (or whatever the function is called). It's a bit clunky, but functional.

Well I was hoping for a

kirilius's picture

Well I was hoping for a solution without any coding. The problem is that although I have good experience with web technologies, I have very limited Drupal exposure and I don't have experience with PHP.

I can try doing what you suggested, but can you please give me a little more detailed instructions?

I have the view, alright. Where should I look for the "hook_menu() implementation"? How do I "add an entry for node/arg(1)/myview"?

Thanks, I appreciate your help!

I did some experimenting and

kirilius's picture

I did some experimenting and got some results but I am not sure I am doing the right thing. The reason is that I found multiple occurrences of hook_menu() in various modules.

I did the change you suggested in one of them - Node Hierarchy (the one that I am using to give me a relationship between a node and some pictures). The thing is that I am not sure this is the right place for it. Ideally where should I make the change?

Panels

RobLoach's picture

Panels 2 has the ability to do this. I suggest you try it out as a very good alternative.

I tried the Panels 2 module

kirilius's picture

I tried the Panels 2 module but still don't see how to configure a tab to be displayed for a node type with it. I created a Bonus:Panels type of view, which works, but don't see how to display it as a tab?

Any hints?

Panels Tabs

RobLoach's picture

Wim Leers' Panels Tabs will let you do some cool stuff with Javascript tabs. The Drupal 6 menu system will really help with this stuff.

Thanks again! I was looking

kirilius's picture

Thanks again! I was looking for a solution to add a regular Drupal tab to the node display. Panels Tabs will probably work, however it introduces a new type of tab.

If you are looking at a node that you just created, normally you have 2 tabs: View and Edit. What I want to do is to add a new tab on the same level for displaying node-related information. Much like in the user profile you see different tab for the different categories of fields in the profile.

Custom mini-module

raintonr's picture

I had the same problem, the $node- didn't seem to work for me (and plus, don't like using features that might come and go) so wrote a small mini-module:

function ride_story_tabs_menu($may_cache) {
  $items = array();

  if (!$may_cache) {
    $add_tab = FALSE;
    if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
      $node = node_load(arg(1));
      if ($node->type == 'ride_story') { $add_tab = TRUE; }
    } else if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'tracks') {
      $add_tab = TRUE;
    }
    if ($add_tab) {
      $items[] = array(
        'title' => 'Tracks',
        'path' => 'node/' . arg(1) . '/tracks',
        'type' => MENU_LOCAL_TASK,
        'access' => 1
      );
    }
  }

  return $items;
}

YMMV, but seemed easy enough and works fine.

D6 solution?

raintonr's picture

Re: Custom mini-module.... this only works in D5.

I didn't see a way in D6 to achieve the same thing.

At first I though we could use an access function and passes an argument, but this doesn't seem to be called on every page load (probably by design - fair enough).

Then I found the method described here. It works, but is it's a bit confusing:

http://www.akchauhan.com/display-menu-according-to-nodetype-in-drupal/

Basically, it appears that one can 'name' an argument and the menu system will call a hook that is that 'name' with '_load' appended.

So one can do this:

  $items['node/%whateveryoulike/page'] = array(
    'title' => 'Your title',
    'page arguments' => array(1),
    ...
    etc.

function whateveryoulike_load($nid) {
  /* Put stuff in here to check the node ($nid) is correct type for what you want to do. /
  /
Typically a node_load, or you could do a single SQL to get the type instead. */
}

Clearly whateveryoulike should be unique to your module. In the link above they use the module name which is why I found it confusing at first and thought there must be some hook_load being used which isn't the case. IMHO you should prefix with underscore incase such a hook ever did come along.

Ahhhh... this is documented here: http://drupal.org/node/224170

Something that isn't obvious is that the arg(x) from the URL is replaced by the value returned by the corresponding _load function.

Update, all good now

rjdempsey's picture

In case someone is still searching for an answer to this, as I was, recent Views 2 updates have made this easier. However a google search still leads you to this thread. You'll want to go here instead ---> http://drupal.org/node/354423

That's where I found my answer. Argument Validators can validate by node type.

Thanks

nally's picture

Thanks for posting that followup for D6. Just what I was looking for!

Nice, thanks for this. Saw

mattcasey's picture

Nice, thanks for this. Saw those Validators earlier and was never sure when I need them!

Still working for D7! Just

morningtime's picture

Still working for D7! Just throw in the content type validator on the contextual filter.

You might face issue with different content types

suresh_gju's picture

You might face issue showing tabs for a specific content type ..

These 2 link will solve you problem

http://tewson.com/content/creating-tabbed-views-drupal-6 (For adding views as tabs)
http://drupal.org/node/354423 (For showing tab on specific content type)

www.sureshyadav.com

Another follow-up for Drupal

geerlingguy's picture

Another follow-up for Drupal 7 - I can't get this to work with content type validation in my contextual filters: http://drupal.org/node/1187068

Hey buddy I went through the

ankitchauhan's picture

Hey buddy
I went through the link you have written above. Definitely it's a bit confusing. I have just modified this.
I have to add a tab as named Math Score just next to edit link in node page of content type student. On that tab link I have to load another node type math_score but url should be node/%node/math-score.

You can see the code here.

$items['node/%math_score/math-score'] = array(
    'title' => 'Math Score',
    'page callback' => 'node_page_view',  // the function to be called
    'page arguments' => array(1),         // arguments for the callback function
    'access callback' => 'node_access',   // permission
    'access arguments' => array('view', 1),
    'load arguments' => array('%map', '%index'),
    'type' => MENU_LOCAL_TASK,
  );

function math_score_load($nid) {
  if (is_numeric($nid)) {
    $node = node_load($nid);
      if ($node->type == 'student') {
        $score_nid = db_result(db_query("SELECT nid FROM {content_type_math_score} WHERE field_math_score_student_nid_value = %d", $node->nid));
        if ($score_nid != '') {
          $n = node_load($score_nid);           
          return $n;
        }           
      }
    }
  return FALSE;
}

For More
http://drupal.org/node/109153

For those who are struggling with this on D7

sbrow126's picture

Post #9 does work, but if the details are getting you stuck...

Try enabling the "Backlinks" view, and follow the example there exactly. It's a great built-in example of how to get this to work. Just use the page setup in the same way as backlinks does, don't forget to specify that yours is a normal menu, do NOT check the 'context' link box there, etc. etc.

Then, add in the contextual filter as specified above, and voila! I was getting stuck and nothing was showing up for me, and so I stumbled on the backlinks setup, and it turns out I had the wrong setup under the Menu page settings. Once I fixed that, it shows up on the node types i specify as desired.

Hope this helps.

Better way - Webform module

operations's picture

There is a better way to achieve this and it worked with me. I traced the webform module on how it displays its tabs only if the node type is a webform:

Hook menu:

$items['node/%mymodule_menu/mynewtab'] = array(
    'access arguments' => array('access mymodule tab'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mytab_form', 1),
    'title' => 'My new tab page',
    'weight' => 5,
    'type' => MENU_LOCAL_TASK,
  );

Add this function also:

/**
* Menu loader callback. Load a webform node if the given nid is a webform.
*/
function mymodule_menu_load($nid) {
  if (!is_numeric($nid)) {
    return FALSE;
  }
  $node = node_load($nid);
  if (!isset($node->type) || !in_array($node->type, array('node_type1', 'node_type2', .. etc))) {
    return FALSE;
  }
  return $node;
}

Clear cache and don't tell me it didn't work :)

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week