User login

Agaric asks itself: help. views arguments? Taxonomy, nodes, and context

The Question: How to use arguments to make a block view show content associated with pages

ok

so there are industry pages under the industry dropdown. AND there are products pages under the products dropdown, products is its own content type, industry pages are pages, the products content type has a vocabulary associated with it that basically has all the industries in it.

This view /admin/build/views/product_view/edit is supposed to output a block that displays products associated with the taxonomy term for an industry when you are on the page for that particular industry... so i am pretty sure arguments needs to come into play here, help please

Preliminary Steps: create context by associating nodes with taxonomy terms

The first thing you have to do is associate the industry pages each with their appropriate taxonomy term.

So you should convert the industry pages to a new "industry" content type - http://drupal.org/project/nodetype

Tell them they are associated with the industry vocabulary and match them up.

(You can use http://drupal.org/project/nat to automate this in the future whenever industry pages are created.)

And *then* we have the context we need to link to products from industry pages and vice versa.

This public service announcement has been brought to you by the forthcoming business book blockbuster Working From Bed: web development the Agaric way

The Intellectual Struggle: Yet another content type?

hey,

I don't want to add another content type. there are too many already, last night I was thinking about how to remove one. especially having page and then another content type that is identical to it just called industry is stupid to me.. basically it's not gonna happen. why can't I use the taxonomy to tell them what they are? otherwise I'd am just gonna make 5 separate views, one for each industry category....

Let's take this to IM.

tuesday, april 22, 2008
11:36
dan hak 11:36
yo
benjamin melançon (autoreply) 11:36
eats the budget Pesach burrito: black beans on matzah - http://mlncn.com/node/473
36:25
oy
dan hak 11:36
not trying to be difficult with that reply...
dan hak 11:36
just wanna stay away from a million content types
36:56
how do I do it using tax and Taxonomy Term Name
benjamin melançon 11:37
yeah, but taxonomy decides its rules by content type, that's the only reason for that-- you don't want every page to have to have an industry
38:05
there's no rational reason to avoid a content type here
38:15
i agree with you in principal, but not in this case
dan hak 11:38
what about url then?

and we can give those 5 pages their own path
benjamin melançon 11:38
(brain screaming ugly hack{
dan hak 11:39
U
39:04
G
39:05
H
39:06
!
benjamin melançon 11:40
(another content type you can theme not to show the taxonomy term etc. really i think it's the only elegant solution. Just not sure the nodetype switcher works without the body field )
dan hak 11:42
(11:37:56 AM) i3iviivi: yeah, but taxonomy decides its rules by content type, that's the only reason for that-- you don't want every page to have to have an industry
42:16
there could be an option for no industry
42:18
none
42:21
in other words
42:39
a selector on the form is still cool
benjamin melançon 11:42
get over here and let me talk sense into you in person
42:57
a selector on every page form is /not/ cool, in my opinion
dan hak 11:43
i hear what you are saying, but, there are only literally 4 pages that won't be using it
43:40
under company
43:49
partners has it's own content type
44:11
all other pages will be classified to industries
44:22
so in essence page IS the industry content type
benjamin melançon 11:44
hmm
45:17
well as long as the nodes you want products to be associated with have the Industry taxonomy associated with them, we have the context we need
dan hak 11:45
ok
45:33
word
benjamin melançon 11:45
we can always rename page -> industry and create a new page later should we decide we want a clean content type
dan hak 11:45
i knew that, i just dunno how to make it happen
benjamin melançon 11:46
uh, add 'page' to the industry vocabulary, if you're going to be obstinant -- if you want it *required* for products but not for "pages" we can have a custom form_alter mess with that
47:06
but really it's easier just to have another content type
dan hak 11:47

48:33
there are going to be too many already! i am thinking though.. maybe I will make industry it;s own, the have page has the one for articles and white papers....
48:39
i guess i'll do that
48:54
....grumble grumble...
52:10
grumble
benjamin melançon 11:53
lol

Actually passing in a node's taxonomy term for the views argument proves annoying

The undistilled IM transcript:

dan hak 11:21
got partners, testimonials, and all the content types straightenedout on jonas last night
benjamin melançon 11:21
nice
dan hak 11:21
so about those views now
21:44
the arguments etc

benjamin melançon 11:22
made the base views yet?

dan hak 11:22
uh, yeah i think so
benjamin melançon 11:22
there's a Boston meetup tonight btw-- 7 p.m.
dan hak 11:23
word
23:34
brb
27:06
ok
27:07
anyway
benjamin melançon 11:27
edit your view
27:29
http://jonassoftware.com.agariclabs.com/admin/build/views/product_view/edit
dan hak 11:27
yup
27:36
there already
benjamin melançon 11:27
change the argument from term name to term id, because I'm a programmer and like numbers
28:05
as long as i don't have to do math with them
dan hak 11:28
ok, so tax term id
28:40
display all values?
benjamin melançon 11:29
um, yeah, that's just the default, which we don't expect to ....
29:21
use. i don't think. anyway go with that for now, maybe we'll want it to display nothing.
dan hak 11:29
default was return page not found
benjamin melançon 11:30
i meant, that's what the view should default to if no argument is passed in
dan hak 11:30
can it not show if there's nothing instead?
benjamin melançon 11:31
empty text i think.
dan hak 11:31
ok
benjamin melançon 11:31
i think we can also obliterate the view from the argument handling code area if necessary
dan hak 11:31
so now for the tougher part
32:19
if i am on an industry page, show the products pages tagged to THAT industry
benjamin melançon 11:34
Put this in the argument handling code
34:51
<?php
if (arg(0) == 'node' && is_int(arg(1))) {
 $industry_node = node_load(arg(1));
drupal_set_message('<pre>'. var_export($node,TRUE) .'</pre>');
}
?>

35:08
actually, this:
35:10
<?php
if (arg(0) == 'node' && is_int(arg(1))) {
 $industry_node = node_load(arg(1));
drupal_set_message('<pre>'. var_export($industry_node,TRUE) .'</pre>');
}
?>

[Do not paste the angle brackets php tags into the code filter area]
35:17
and figure it out from there
35:59
you want to return the tid
36:22
returning *which* tid, I'm not so sure how to do that yet
dan hak 11:38
so i dont need the industry vocab to include industries anymore right
38:44
coz that was kinda annoying
benjamin melançon 11:39
um, could you explain?
dan hak 11:39
i added the industry pages to the industries vocab
39:29
when i thought i wanted to use tax only
benjamin melançon 11:39
yes, you need that!
dan hak 11:39
oh ok
benjamin melançon 11:39
that's the whole point-- that's the term id we're trying to grab
dan hak 11:40
from the product page tho?
40:08
not the industry page? or both?
benjamin melançon 11:40
i thought this was a page of products you wanted to show up on the industry pages?
dan hak 11:40
lets say
benjamin melançon 11:40
(a block of products)
dan hak 11:41
I goto 'electrical' under the 'industries' drop down. I want there to be a block list that shows all the products tagged to 'electrical' on the side
benjamin melançon 11:41
"if i am on an industry page, show the products pages tagged to THAT industry"
41:45
exactly
dan hak 11:42
so i need both content types to have vocabulary
benjamin melançon 11:42
so when we load a node from arg(1), we're grabbing the node id from the URL
42:05
yes
44:49
But we can experiment with how to pass in the term ID pretty easily-- just put "$args[0] = 123" in the argument handling code, where 123 is the taxonomy term for an industry
45:23
(and as mentioned yesterday you could use node auto term to make sure every industry page created using the industry content type automatically gets a matching taxonomy term)
dan hak went away
11:49
dan hak went idle
11:49
away message: i'm not here right now
11:49
dan hak came back
11:50
dan hak became active
11:50
dan hak 11:53
hold on
53:23
i dont get it
53:31
what does the code mean?
53:41
and how does it talk to the view form
53:42
?
benjamin melançon 11:54
you created an argument -- your view is waiting for a term tid
54:22
so we want to give it exactly the one we want
55:13
setting $args[0] to the tid we want -- inside the views handling code -- should do that
55:36
so for now just set it to an industry tid you know exists, and we'll see if that works
55:56
then I want to see what happens if you try to pass an array of tids (probably won't work)
56:30
and then we'll go about getting the taxonomy term tid from the industry node for its industry
56:33
sound like a plan?
dan hak 11:56
set it how?
56:50
in the wildcard box?
benjamin melançon 11:57
Please read what I write, and even what I don't write: "Argument Handling Code" box
58:16
it's collapsed by default
dan hak 11:58
yea i added
<?php
if (arg(0) == 'node' && is_int(arg(1))) {
  $industry_node = node_load(arg(1));
drupal_set_message('<pre>'. var_export($industry_node,TRUE) .'</pre>');
}
?>

there right now
58:48
should i add arg 0 = a tid here?
58:49
too?
benjamin melançon 11:59
that's cool, that can stay, but add that also as a test, yes-- it can be outside the if statement for now
59:20
(just in case i screwed the if statement up)
dan hak 12:03
nothing is happening
benjamin melançon 12:03
what was happening before you added anything to the arguments section?
dan hak 12:04
i dunno
04:53
this is what i have in there now
04:54
<?php
if (arg(0) == 'node' && is_int(arg(1))) {
  $industry_node = node_load(arg(1));
drupal_set_message('<pre>'. var_export($industry_node,TRUE) .'</pre>');
}
$args[0] = 3
?>

04:55
um
05:11
do i have to put anything in the boxes? wildcard etc
05:12
god
benjamin melançon 12:05
that's not giving me much of a baseline. Could you make sure your view works without arguments-- displays all the product nodes in the block.
dan hak 12:05
ok
06:42
http://jonassoftware.com.agariclabs.com/industries/electrical
06:47
works without ur code
benjamin melançon 12:07
very nice
07:30
now make sure you have a semicolon after that 3 (and maybe only put in that line) and try again
dan hak 12:07
ok
09:54
ok
09:59
it works witht the 3
10:01
$args[0] = 3;
10:11
for the electrical page obviously
benjamin melançon 12:10
nice
10:27
so it was probably the lack of semicolon
10:41
put in the other text, and let's see how we can find that three
dan hak 12:10
so just add your other code back, and take that line out?
benjamin melançon 12:11
that line stays -- we're going to want it to have some variable instead of three, and we're going to want to take out the drupal_set_message line
12:08
but we can't do either until we see what printing the node object offers us in terms of a variable that holds "3" for the Electrical industry
dan hak 12:12
broken again
12:23
http://jonassoftware.com.agariclabs.com/industries/electrical
13:58
i had a slash in there for some reason
benjamin melançon 12:14
those were my next words... what the heck is that slash doing in there?
dan hak 12:14
ok it works
14:27
now what
14:30
?
benjamin melançon 12:14
what's it giving you in the set message?
dan hak 12:14
nothong
14:50

14:52
nothing
14:59
no thong either
benjamin melançon 12:15
drupal_set_message is weak
15:25
print that big guy
16:22
actually, leave a drupal_set_message("Hey");
17:05
but change for around the var_export replace it with print
17:14
or print_r
dan hak 12:18
huh
18:27
i added the hey line
18:29
still nothign
benjamin melançon 12:19
maybe i've been through this before... the views argument handling code is called so deep within Drupal that the message can't escape
19:21
print's got to work though
dan hak 12:20
<?php
$args[0] = 3;
if (arg(0) == 'node' && is_int(arg(1))) {
  $industry_node = node_load(arg(1));
print("Hey");
print_r('<pre>'. var_export($industry_node,TRUE) .'</pre>');
}
?>

20:46
still nothing
benjamin melançon 12:21
put tem outside the if statement?
21:32
them
dan hak 12:22
nope
benjamin melançon 12:23
ok, look on the Lullabot 50 tips page
23:10
for a comment by me
23:14
i explain how to do this
dan hak 12:23
where?
benjamin melançon 12:25
http://www.lullabot.com/audiocast/podcast-50-drupal-tips-and-tricks#comment-2998
25:09
drupal_set_message should work
dan hak 12:27
http://jonassoftware.com.agariclabs.com/industries/electrical
benjamin melançon 12:27
ok, now change $view in that
28:00
to $industry_node
28:17
or maybe try directly for $industry_node['taxonomy']
dan hak 12:28
reload
28:56
industry_node
29:00
did nothing
29:23
i beleive i named it industy_page
29:30
if that is the same thing
benjamin melançon 12:29
it's printing "message" though-- please look at the code for whatever variable the node_load was assigned to
dan hak 12:32
huh?
35:43
?
38:32
you help now
benjamin melançon 12:38
it's the damn if statement somehow
38:51
you should have been able to identify that for me
39:20
goddamn is_int doesn't work
39:23
but is_numeric does
39:30
ugh
39:36
go in there and clean up
40:56
oh, and try
$args = array(100,99,3);
41:00
to see if that works
dan hak went away
12:50
dan hak went idle
12:50
away message: i'm not here right now
12:50
dan hak came back
12:51
dan hak became active
12:51
dan hak 12:51
now what?
benjamin melançon 12:51
where are you at
dan hak 12:51
http://jonassoftware.com.agariclabs.com/industries/electrical
benjamin melançon 12:54
clean out my test text
54:39
and try the $args with array thing
58:05
oops-- make that args[0] = array...
58:36
i meant to replace the $args[0] = 3; to see if it would still work if the 3 was one of many
dan hak 12:58
http://jonassoftware.com.agariclabs.com/industries/electrical
59:41
brb
01:47
btw (what ddoes this mean?) nat setting => Associate node body with term description.
benjamin melançon 1:02
i guess it automatically puts whatever is in the node body into the term description
dan hak 1:02
oh

benjamin melançon 1:26
<?php
if ((arg(0) == 'node') && (is_numeric(arg(1)))) {
 $industry_node = node_load(arg(1));
 $tids = array_keys($industry_node->taxonomy);
// this will only work correctly when the single term associated with an industry node is its industry term
 $args[0] = $tids[0];
}
?>

Again, the <?php tags are for formatting and indicating PHP code here-- do not use that part in the views argument handling code textbox.

That last bit of code works, albeit fragilely. What piece of Drupal, Taxonomy, Views, or maybe Panels wizardry are we missing that would make this easy and robust?

More clarification:

so what does that mena?
benjamin melançon 1:52
if you want to add other taxonomy terms to industry pages, give those vocabularies a higher weight
dan hak 1:52
and i have to clean up tax terms coz of NAT
52:41
i dont think there is gonna be any other vocabs
benjamin melançon 1:52
and if you want industry pages to be associated with multiple industry terms, shoot yourself in the head before even trying
dan hak 1:53
by their nature they should only be associated to one... thats why i was askin if it was necessary to have them there
benjamin melançon 1:54
well somehow the term has to be associated with it, right?
54:30
so-- yes, it's necessary
54:44
but given the fragility of views arguments as I understand them regarding taxonomy
dan hak 1:54
but i could just hide that on the form
benjamin melançon 1:54
with NAT, def.
55:08
... it's a good thing they only need one
dan hak 1:55
well i have added nat and am cleaning up the terms now
55:27
it made copies
benjamin melançon 1:55
very rude of it

Resolution

The views argument is set to Taxonomy term ID.

This is pasted into the Argument Handling Code textbox (without the wrapping php tags)

<?php
if ((arg(0) == 'node') && (is_numeric(arg(1)))) {
 $industry_node = node_load(arg(1));
 $tids = array_keys($industry_node->taxonomy);
// this will only work correctly when the single term associated with an industry node is its industry term
 $args[0] = $tids[0];
}
?>

Searched words: 
views arguments taxonomy terms show other content type nodes with term do not show term on node auto term content type Drupal view add taxonomy term as argument

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.