I searched this topic and found lots of people want a block of taxonomy related nodes on a node page. But what I want is a block of taxonomy related nodes to display on the taxonomy view page.

My goal here is to build something like an "arts section" as if I were a "newspaper".

I have overridden the core taxonomy pages with a custom view, so it's pretty. Now I would like to use the item offset of the view to show me results #2 through #20. I would like a custom block containing the same view logic to appear at the top of the content showing results #1 only, then I can theme it or tweak the view so that block resembles a "lead article".

Is this making sense so far?

Basically, the question is, how do I get a block to return nodes that are related to the term of the taxonomy page on display?

I'm looking for an argument here, but I think it's difficult to get an argument for this block because its displayed on a mysite.com/taxonomy/term page rather than one with a URL that looks like mysite.com/somenumber.

Any help and you will be my bosses best friend. Thanks!

Comments

seaneffel’s picture

It's heartbreaking when you search Drupal.org for your problem and then the only result you find is your original cry for help...

WorldFallz’s picture

I'm not sure I fully understand what you're after-- let me see if i understand correctly.

You want a view at site.com/taxonomy/term that shows the first item one way and the rest of the items a different way? For example, the first item might be a full node view and the rest of the items could be simply node titles for items 2 through whatever?

Is that it?

seaneffel’s picture

I'm looking for a method of showing a block of taxonomy-related nodes on a mysite.com/taxonomy/term/id page.

That means when users navigate to a taxonomy display page for the term "kittens" that they can see a list of nodes that are tagged with the term kittens as well as a block containing nodes tagged as kittens.

I've already got a view that overrides the core taxonomy display pages. I also have a view that turns out a block of nodes by term. I can't seem to display the block on the taxonomy page and call up the same taxonomy term filtering in each. I've tried various arguments for the block view but it doesn't pick up the term ID from the URL when on the taxonomy/term/id pages. Take a look at this example, notice that the block on the side doesn't display nodes with the same terms: http://www.cctvcambridge.org/taxonomy/term/799.

Once I can get that trick done, I know how to work with the views offset function to turn up different results in each region. Imagine the page view turning up results #1 through 10 and the sidebar block turning up results #11-15, for example.

WorldFallz’s picture

ah ok. Is it always from a specific vocabulary? Is is always a single term?

EDIT: nm, i don't think it matters.

Ok what I did was enable the core taxonomy_term view and cloned it to have something to work with.

Try this:

  1. add a block display to the view
  2. configure the first argument (in this case it's "Taxonomy: Term ID (with depth)")
  3. select "provide default argument" then "php code"
  4. Then add the following to the code text box:
    if (arg(0) == 'taxonomy' && is_numeric(arg(2))){
      return arg(2);
    } else {
      return FALSE;
    }
    
  5. then set the block visibility to "taxonomy/*".

Try that and see if that's what you're looking for-- works on my dev site.

seaneffel’s picture

Looking pretty good here, thanks so much for the work. This turns up results like I had in mind:

1. Taxonomy term page with fix number of tagged nodes.
2. A block with similarly tagged nodes pulled from the same URL via argument.

I worked the offset functions of the page and block view so that the page shows results #1-8 and the block shows results #9-13. I even threw in a block to show results #14-20. See on this link below how that the nodes on page two of the view are reflected in the blocks on the side in the correct order:

Check it out now: http://www.cctvcambridge.org/taxonomy/term/793?page=1

I'm actually planning to make a new block that would show in the content-top region that displays node #1 only, then the page to display nodes #2-8, and sidebar region blocks that show nodes #8-14 and then again for #15-20. It's getting a little cerebral.

Let me bang on this for a few hours and see if I run into anything funky.

WorldFallz’s picture

Excellent-- the page is shaping up great! I'd be interested in hearing how you handled the offsets... did you have to theme it in?

seaneffel’s picture

No no, the offsets are all handled in Views 2. Easy peasy.

When choosing the number of nodes to display in the view, there is a second field for selecting the offset. Let's say I'm making blocks that display 5 nodes.

Offset of 0 turns up nodes #1-5.
Offset of 3 turns up nodes #3-8.

The order of the results all depends on your choice of filtering within the view. I'm doing it chronologically, I could do it alphabetically and get nodes a-e or nodes c-g, if you catch my drift.

WorldFallz’s picture

ah yes-- i got ya. I was playing around with it a little and I thought it would be cool to list the nodes in the main content area by post date desc and in the block by total views desc (so the popular related nodes float to the top) but then the offset gets tricky. I haven't found an easy way to exclude the nodes in the main content from appearing in the block with this configuration.

seaneffel’s picture

I'm handling all the exclusion of duplicate nodes through the offset for now, but its not 100% tight.

I'm having trouble with page 2+ on any view on this model because of the redundant nodes appearing in all this blocks. The blocks don't refresh on page two, so I get dupes. If I leave the pager enabled (and then hide it with CSS) I think I get blocks that show certain results on page 1, but are smart on page 2 as well. Still experimenting.

As for popular nodes blocks, how do you feel about them? I've always thought it would just perpetuate certain nodes to always be highly viewed rather than drive users to other new nodes intstead.

WorldFallz’s picture

As for popular nodes blocks, how do you feel about them? I've always thought it would just perpetuate certain nodes to always be highly viewed rather than drive users to other new nodes intstead.

I tend to agree-- it's like a self reinforcing prophecy. It just struck me as an I idea when i was working out the argument code-- I don't think i'd actually recommend it or use it on a prod site.

But I could see a case for using a random sort in the blocks-- which again leaves the problem of duplicate nodes. You can set an argument to exclude the argument values so I've got a block which excludes the nid's I send it, but I haven't figured out how to get a list of the nids being listed in the main content area yet.

seaneffel’s picture

Can you get a list of random nodes that are offset by a number of nodes? That would be nodes that sort later than the number of ordered nodes on the current page. It would be cheap since it wouldn't be totally random, only partly random.

seaneffel’s picture

When the block of related nodes appears on a taxonomy/term/id page, there are two different behaviors when the pager is switched on or off.

1. Without pagers, the block will always show the same results. On page one of the view where the block appears, it will turn up results #1-5. On page 2 of the same view, it turns up #1-5 again.

2. With the pagers turned on, the block displays nodes #1-5 on the first page but shows #6-10 on the second page. But then the pagers are turned on, and they are ugly.

I can hide the pagers from the blocks with CSS, but is there a way to get the behavior from number two without turning on the pagers?

Check out the example now and notice that the first item on the top block shows the first result found on the following page. Go that? Hard to think it through without drawing a picture, look at this:

http://www.cctvcambridge.org/taxonomy/term/858

WorldFallz’s picture

My brain is smoking and i had to play around on the page a bit but I get what you're explaining. I can't think of any way to do this off the top of my head but my instinct is telling me there must be a way-- basically you need to do what I'm trying to figure out in my comment above (how to provide the current nodes listed on the page to the block view as an argument of nids configured to be excluded).

Maybe instead of displaying the view block directly, using views_embed_view. hmmm... i'll play around with it some but this might be a question for mighty merlin the wonder coder!

seaneffel’s picture

Working this argument to the max today. I'm finding some wonky things about the offset and I'm still experimenting, but this is looking pretty good so far. I've even added some blockage by type to pull "video" content types into the pages based on the taxonomy/term/id of the page.

http://www.cctvcambridge.org/taxonomy/term/155?page=1

It's just some remaining tweaking and then some CSS work until this looks perfect.

I'll blog about your method on Drupaltherapy, I think that argument and tactic has got some serious legs.

WorldFallz’s picture

Nice work! That page is really shaping up. I only took a quick look at the source but I couldn't see anything obvious-- what did you decide to do about the pager issue-- hide it with css?

seaneffel’s picture

I opted to hide the pagers with CSS. I wish there were a smarter way that didn't draw an extra half dozen queries per block.

WorldFallz’s picture

yeah-- i think there probably has to be a better way that we're not thinking of. Maybe an issue in the views issue queue or check with merlin on IRC.

adam_b’s picture

Would http://drupal.org/project/panels not do what you want, without all the custom coding?

seaneffel’s picture

I looked at Panels and I've even taught that module in classes, but after two years there has never been a proper release that wasn't alpha, beta or even rc. This particular site doesn't have a great threshold for maintenance and pain so I decided against it. Plus the site admins here have chops with view and don't have to learn anything new to micromanage these new layouts.

To be honest there isn't that much custom work here, apart from the one awesome snippet you gave me. The real work is being handled by Views and coming out great so far.

WorldFallz’s picture

I was browsing the views issue queue and found #191241: Offset without limit returns all results -- maybe this is related? When the blocks with offsets are returning all values do they have a limit or default/unlimited? Maybe try using a limit?

51ucars’s picture

great, this code works, but I got a warning warning: Invalid argument supplied for foreach() in ...l\includes\common.inc on line 1489.

kazah’s picture

Thank you! Thank you! Thank you! Thank you! Very much!!!

summit’s picture

This code is working great! Thanks.
greetings, Martijn

seaneffel’s picture

Maybe you know how to do this...

I'm looking a node that has been tagged with a taxonomy term. I've got a view in a block using an argument to return similar nodes based on the taxonomy term. I've got some PHP argument code I snorked off the forums to do the job of detecting the term from the parent node:


// Pick up the node in focus (if any)
$node = menu_get_object();

// Get all the taxonomy terms for this node (if any)
$terms = taxonomy_node_get_terms($node);

// If taxonomy terms were found, return them to the view
if (! empty($terms)) {
  return implode('+', array_keys($terms)); // Return all terms found separated by +
}

How can I then add an element to this argument to only return results for specific taxonomy vocabularies? We have a vocabulary available to all users, and then an admin-only vocab that I don't want to return related nodes for.

WorldFallz’s picture

Try using taxonomy_node_get_terms_by_vocabulary instead of taxonomy_node_get_terms.

seaneffel’s picture

Yeah, this is greek to me, but I'll take my best stab. I'm trying to call related nodes from vid=9. If I've got this right, its the vocabulary ID of the vocab tree I want to pull related nodes from. I basically made this up, I'm sure it's wrong.


// Help me out - manually enter the vocabulary id
$vid = '9';

// Pick up the node in focus (if any)
$node = menu_get_object();

// Get terms from the specified vocabulary on this node (if any)
$terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);

// If taxonomy terms were found, return them to the view
if (! empty($terms)) {
  return implode('+', array_keys($terms)); // Return all terms found separated by +
}

WorldFallz’s picture

Yep-- that looks right to me. Let me know if it works.

seaneffel’s picture

Yes, this is working great. Thanks for checking my work.

WorldFallz’s picture

excellent ;-)

baim78’s picture

Thank you,, it works,,

seaneffel’s picture

I was just about to ask you another views arguments question when a last ditch search showed me that you had already answered it!

http://drupal.org/node/398786

So, i guess, thanks for that.

WorldFallz’s picture

welcome ;-)

HS’s picture

WorldFallz and seaneffel,

Thank you so much for this thread.