I read some of the obvious resources on the arguments sections of the Views module, and I'm still having trouble getting it to work with my scenario.

I have a Views block that always appears at www.sample.com/{vocab name} and shows nodes categorized under taxonomy vocabulary {vocab name}. (The main sections of my site each have a vocab name associated with it.)

I need that same block to filter nodes based on terms using a URL in the form of www.sample.com/{vocab name}/{term}.

So far I have a Term Name argument in my Views block with option = 1 and wildcard = *. From what I've read I'd hoped the View would interpret it correctly, but none of my nodes show up after adding the argument.

Thanks in advance for any advice.

Comments

auxone’s picture

After sleeping on it, I'm still stuck on this. Any help would be appreciated. Thanks

auxone’s picture

To anyone interested, I believe my problem is the bug documented here: http://drupal.org/node/129638

auxone’s picture

Nope. Still an issue.

WorldFallz’s picture

I'm not sure whether or not you'll be bitten but the bug you linked above, but block views don't understand urls and arguments automatically. Blocks don't have urls-- you need to provide them the proper arguments with argument handling code

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

auxone’s picture

Thanks for your response.

I did try that. I even hardcoded it like:

$args[0] = 'term_name';
return $args;

but still it doesn't seem to work. As a debug I added a:

print_r($args);

but nothing prints.

WorldFallz’s picture

If you export your view I'll take a look. I'm no expert, but maybe I can spot something obvious.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

auxone’s picture

I really appreciate the help. To put it in more solid terms, I have a vocabulary called 'the_bride' with terms like 'aries'. No hierarchy. On my Views block I have a Filter set to show only published nodes that 'Is One of Taxomony Vocabulary: The Bride'. I also have a simple Mini-Panel wrapping the whole thing -- and I haven't tried it without the panel but it's kinda necessary to my design. The block is visible at /bride unfiltered, and /bride/aries should filter for the term 'aries' within 'The Bride' vocabulary.

Thanks!!!

PS> More specifically, the block is visiable on pages: bride and bride/* is it possible the * is interfering?

  $view = new stdClass();
  $view->name = 'the_bride';
  $view->description = '';
  $view->access = array (
);
  $view->view_args_php = 'if ($view->build_type == \'block\' && arg(0) == \'node\' && is_numeric(arg(1))) {
  $args[0] = arg(1);
}
return $args;';
  $view->page = FALSE;
  $view->page_title = '';
  $view->page_header = '';
  $view->page_header_format = '1';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'node';
  $view->url = '';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->block = TRUE;
  $view->block_title = '';
  $view->block_header = '';
  $view->block_header_format = '1';
  $view->block_footer = '';
  $view->block_footer_format = '1';
  $view->block_empty = 'No records were found!';
  $view->block_empty_format = '1';
  $view->block_type = 'node';
  $view->nodes_per_block = '1';
  $view->block_more = FALSE;
  $view->block_use_page_header = FALSE;
  $view->block_use_page_footer = FALSE;
  $view->block_use_page_empty = FALSE;
  $view->sort = array (
  );
  $view->argument = array (
    array (
      'type' => 'taxletter',
      'argdefault' => '7',
      'title' => '%1',
      'options' => '1',
      'wildcard' => '*',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array (
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array (
      'tablename' => 'term_data',
      'field' => 'vid',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => '44',
),
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(node, term_data);
  $views[$view->name] = $view;

WorldFallz’s picture

I was just rereading your op-- I think i figured out what the problem is. The arg() function is not going to see your '/vocab/term' url-- it sees the root unaliased path of 'node/nid'. You'll need to do something like this snippet to grab the vocabulary term and pass it back to views.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

auxone’s picture

You may be on to something! I implemented the following code. The id of the vocabulary 'The Bride' is 44. It didn't work. I'd really appreciate more specific guidance at this point -- I've been struggling for a few days. Is there a way to do a "print" that actually shows up on the page so I can see what the values of $args is? My only thought is perhaps I need to convert the $term->tid to the actual term name. Still doing bride/{tid} doesn't seem to work.

if ($type=='block' && arg(0) == 'node' && is_numeric(arg(1))){
$node=node_load(arg(1));
$terms = taxonomy_node_get_terms_by_vocabulary($node->nid, 44, $key = 'tid');
foreach($terms as $term){
$sign = $term->tid;
}
$args[0] = $sign;
return $args;
}
WorldFallz’s picture

Actually, I was playing around with this (because that snippet just seemed inefficient to me when the info you need is right in the url) and came up with this:

<?php
if ($type == 'block' && arg(0) == 'node' && is_numeric(arg(1))){
  $alias = explode('/', drupal_get_path_alias($_GET['q']));
  $args[0] = $alias[1];
}
return $args;
?>

Try this with an argument of "Taxonomy: Term Name".

Also, to answer your question-- if you want to see the contents of an array you can use print_r($array_variable). So to print the args array it would be print_r($args). Or, to make it easier to read:

print "<pre>";
print_r($args);
print "</pre>";

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

auxone’s picture

Sigh. This whole situation is crushing my spirit! Hah.

I used that code, and I agree that it looks good. However, I tried using the print statement as you indicated, and it seems like the View or the block over writes any prints that would show up with its "argument default", so I set my argument default to 'Summary, sorted as view' which revealed some interesting behavior. The little list it produces in place of results is a single link to the term 'aries' (the only term with an article) but the link goes to "http://aries/" -- it should be at sample.com/bride/aries.

I checked my URL aliases, and there wasn't an alias for bride/aries so I set one to "taxonomy/term/99" which is the TID of 'aries'. This didn't fix it, it says 'page not found' when I go to sample.com/taxonomy/term/99, and on sample.com/bride/aries it shows results for that term in the content area (which I don't want since I'm trying to force it into my block on the same page). Also, I forgot to mention that sample.com/bride/ is a Page View, but it set to filter for a Vocab that I know to be empty since all the structural elements of my site are blocks.

I hope this helps clear some things up. I will now experiment further, but any divine intervention on this would really make my day.

Thanks,
Tyler

WorldFallz’s picture

Regarding the printing-- i've never tried the print statements in the actual argument handling code, I usually either stick it in a custom block or php page when i'm testing out code.

As for the rest, I'm not sure I understand. The 'bride/aries' url is an alias-- and I'm not sure what behavior you'll get it you make and alias for an alias. Also, I'm not sure what behavior you'll get if you make an alias of 'taxonomy/term/99' because it's a canonical path.

I did test the code above, and it does what your OP was requesting-- put a block with nodes that have the same term as the one in the page based on the url alias of '/vocab/term'.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

auxone’s picture

Thanks again for all your help. If you got it to work, then I have hope.

To recap, my vocab term is 'The Bride' and my main Page View is 'bride'. Perhaps I should rename my vocab term to 'bride', but I didn't think that was necessary since I am using a Filter on my Views block which only considers nodes with terms in the Vocab 'The Bride'. From this group of nodes, I though I could reduce it further by just specifying 'aries' as an argument. So, its not EXACTLY a url of '/vocab/term'.

I found that after creating a taxonomy category there would automatically be a URL aliased to that term. Since I use Pathauto I immediately found this to be bothersome as many of the URL aliases automatically present for taxonomy terms are the exact URL I wanted for some of my nodes. Hence, aside from /user/admin ALL of my URL aliases point to nodes.

This is where I get confused... You say that '/bride/aries' IS an alias, however currently I don't have listing for it in my URL aliases -- and my View is aliased to /bride. Based on the code I am using it should just parse the 'aries' from the drupal path then the block View should use that as an argument when searching for nodes matching this term within the vocab 'The Bride'. So, I am not trying to alias an alias. I have the block set to be viewable at 'bride/*' so that's the only reason going to the URL /bride/aries/ looks like a legit page (presumably).

Thanks for testing the code for me. Sounds like if I just start from scratch again on this block, knowing what I know now, I should be able to get something working.
Tyler