Help setting up Arguments

j0k3z - January 26, 2007 - 01:54

Im not quite sure how to setup my website. Here is what I am trying to do.

I use CCK to create a new content type called "Attractions" and also "Hotels" and also "City"

I will have many cities on my website. This city page will have 2 large text boxes where I will put detailed info about the city. Below these text boxes I want to insert 2 views. The first view will be a table view that will show "Attractions" from whatever city we are currently on. The second view will do the same for "hotels"

How can I set this up?

First problem, is how to I create the view that will filter by city properly? I will have many cities and each hotel/attraction will have a "City" taxonomy term assigned to them to designate which city they are in. How will my website know that if I am on the Chicago page that I need to pull in Chicago hotels and attractions? Do I need to add the city taxonomy to the city page as well and when I am adding a new city to the directory choose the matching term? And how do I pull this into the arguments?

Next problem, how to I display this table into my newly created city content type? I can see how to add fields but how can I add the view? Can insert_views module be used somehow?

Please help!

Id like to get this fixed

j0k3z - January 26, 2007 - 11:12

Id like to get this fixed today so any help is greatly apreciated.

Thanks

Seems my questions dont get

j0k3z - February 1, 2007 - 07:45

Seems my questions dont get answered in this forum.

Let me try and explain it again.

On my website I have a new content type called "Attractions" This content type is just a simple page. Each of these attraction pages have a taxonomy term assigned to them from my "city" vocabulary that says which city they are in. So I create a bunch of attractions and select which city they are in. Now I have created a new content type called "City" This page will have some text, and below this I want a view that shows all atractions that are in this city.

Can someone break down how I go about setting this up? Ive been playing with arguements all day but nothing is working.

The view works without

j0k3z - February 1, 2007 - 12:55

The view works without arguements. I am able to pull in attractions for cities but dont know how to add in the arguement.

My cities will be like this:

domain.com/city/Chicago
domain.com/city/Detriot
etc

These pages will have this view embded, so I need a way for the view to pull in that last part of the url "Chicago" and "detriot" and use that to find the attractions in that city

can ANYONE please offer ANY

j0k3z - February 2, 2007 - 11:42

can ANYONE please offer ANY advice to me. Im getting desperate and my questions are going unanswered

I find it odd that nobody

j0k3z - February 3, 2007 - 13:50

I find it odd that nobody knows anything about arguments

I think I did something

crandell - February 4, 2007 - 22:25

I think I did something similar to what you want. I have a community blog that allows people to specify a neighborhood for their post. I set up neighborhoods as a content types, so it would be easy to add more neighborhoods, and to allow editorial content about the neighborhoods.

I used CCK nodereference on the blog entry node type, allowing the writer to specify the neighborhood. Then I created a view with a block that goes on each neighborhood node. The argument for the view is Node Reference: Neighborhood (field_neighborhood), and I found somewhere in these forums that I had to add the following code to the Argument Code to make it work:

if ($type == 'block' && arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
//$node = node_load($nid);
$args[] = $nid;
}
return $args;

Hope that helps.

modify for use in-node (viewfield) ?

JohnG - February 22, 2007 - 21:40

This works great with blocks (drupal 5.1) but I'm struggling to convert it to work when the view is embedded in the node (courtesy of http://drupal.org/project/viewfield module).

I am, like Crandell, filtering by a Node Reference argument, but to show a table view with 6 columns which is too wide for a block.

For testing I have both block and in-node views showing at the same time.

I removed the (obvious) 'if ... block' filter so my PHP looks like this:

if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
//$node = node_load($nid);
$args[] = $nid;
}
return $args;

The Node Reference filter works perfectly on the block view, but the filter does not apply to the in-node view - it shows the unfiltered results!

I just can't understand why! Any suggestions welcome :)

Use %tokens

mfredrickson - February 22, 2007 - 22:02

Viewfield has some very rudimentary argument replacement via tokens:

%nid => gets turned into the node id of this node
%author => get turned into the uid of the author of the node
%viewer => gets turned into the uid of the user looking at the node

Using these, you shouldn't have to add any arg handling code for your view. You can just put %nid in the args textfield in the viewfield widget.

I'm probably going to hook into the token.module to get better handling in viewfield. Check back often. :-)

Cheers,
-Mark

--
http://www.advantagelabs.com

view field module

JohnG - February 23, 2007 - 13:01

Mark's method with viewfield module works a treat - and no PHP to mess up :)

Yes, viewfields was a good

j0k3z - February 23, 2007 - 13:39

Yes, viewfields was a good solution, but for me I needed to hide this from my users while they are making the nodes. Its too easy for them to screw it up so using php was the only way to go for me

ah yes I'm beginning to see waht you mean

JohnG - February 23, 2007 - 16:43

Now I see your point (I'm still tinkering ...). Any chance you could provide example PHPcode for the method you used ?

<?php// This loads all the

j0k3z - February 24, 2007 - 01:38

<?php
// This loads all the variables of the primary view,
// including arguments which are in $current_view->args
global $current_view;

// Now let's call ALL desired additional views for this (you can add more
// if desired, just make sure to give each a unique variable name.
$view_1 = views_get_view('Attractions');
$view_2 = views_get_view('Hotels');
$view_3 = views_get_view('Restaurants');
$current_view->args[0]=$node->title//send title as args

// This is some structuring HTML
print t("<br />");
print
'<h2>' . t('Attractions in ' . $node->title) . '</h2>';

// Build and print the view: first tell Views to embed the next view,
// then tell the function WHICH view you mean (you have defined
// this in the variables $view_1, $view_2 and $view_3 above.
// Next, pass into these views the Arguments from the primary view
// which you retrieved with global $current_view; and last but not
// least, "true, 10" enables paging and pages after 10 nodes are listed -
// change this number to whatever you need.
// So let's start outputting the first view:
print views_build_view('embed', $view_1, $current_view->args, true, 10);

// This is again some structuring HTML
print t("<p>&nbsp;</p>");
print
'<h2>' . t('Hotels in ' . $node->title) . '</h2>';

// Build and print the next view
print views_build_view('embed', $view_2, $current_view->args, true, 10);

// This is again some structuring HTML
print t("<p>&nbsp;</p>");
print
'<h2>' . t('Restaurants in ' . $node->title) . '</h2>';

// Build and print the next view
print views_build_view('embed', $view_3, $current_view->args, true, 10);

// Continue adding views if you have set them up and assigned a
// unique variable above
?>

I found this code somewhere in the forums and editied it just a bit.

This block of code inserts 3 views into my page (attractions/hotels/restaurants) This code also takes the title of the current page and passes it to the view as an argument. This is because when you are on the chicago page it will send chicago as the arguments and only return hotels from there.

nice one j0k3z !!

JohnG - February 24, 2007 - 16:57

That works a treat! Thanks! I especially appreciate this line $current_view->args[0]=$node->title;  //send title as args for passing the arguments to the view. I was having trouble with that one ;)

Note that this code works perfectly from the custom node-$type.tpl.php, but not fully functional if pasted into a PHP textfield in the node (different topography I guess).

Thanks for the tip j0k3z.

Natalie@drupal.ru - February 24, 2007 - 21:35

Thanks for the tip j0k3z. What arguments do you use in the actual views?

My view has an argument that

j0k3z - February 25, 2007 - 13:38

My view has an argument that looks at a text field called "city" and uses the passed arguments (page title) to grab the city

if using Views default argument handlers ...

JohnG - February 25, 2007 - 14:47

I've just figured out how to use this with the Views argument handler (supplied by Node Relativity module) 'Relativity: Node Parent ID'.

I assume it applies to all Views' default (in the dropdown list) argument handlers ?

instead of keying off node's title, as JOk3z does with

global $current_view;
$current_view->args[0]=$node->title;

you have to key off node's NID, like this:

global $current_view;
$current_view->args[0]=$node->nid;

hide form from users

Walterh - July 23, 2007 - 23:04

When you want to hide these fields from users, so that they cannot change the viewfield settinging, just add to following code in your css-stylesheet:

fieldset.group-namefieldset
{
visibility:hidden;
}

(change "namefieldset" with the name of your fieldset)

-----------------------------------------------
http://www.offstage.nl

Default setting

robwinter - September 15, 2008 - 07:19

This would be a great workaround but the Override checkbox is selected by default so each node here would strip out any values entered at the Content Type stage & replace them with an empty text field. So we're back to having no argument values passed again!

Instead of embedding a view

kdebaas - February 11, 2007 - 13:36

Instead of embedding a view into your city node, you might consider creating a block for the attractions view, and another for the hotels. You then configure those blocks to display underneath your node, in the "content" area.

Crandells code works, if you want to use the argument handling code. In his case he is using the nodereference field. Applied to your example, it would mean that your hotel and attraction nodes would contain a nodereference field that refers to the correct city node.

However, taxonomy should do the job equally well. But, as you pointed out yourself, you would indeed need to assign the city taxonomy to the city nodes as well. (by the way, if you are interested in an automated way of doing that, every time you create a city node, then look into this new module: NAT

So, the code to insert into the argument handling code field is the following:

if ($type == 'block' && arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $cities = taxonomy_node_get_terms($nid);   
  $args = $cities;
};
return $args;

Try this, and let me know if that worked. I haven't tested it completely so you might want to tweak it a little.

Disclaimer

kdebaas - February 11, 2007 - 13:42

You could seriously and irrepairably wreck your site by adding the wrong php code. So, back-up your database before doing so. Also, my way of avoiding disaster is by using the devel.module which is available for download. It lets me add a block to my pages where I can execute php code to try out. If the php code I test does not crash or break my site, I then copy and paste it into the argument handling code field. This is not failsafe though...

I ended up using view field.

j0k3z - February 23, 2007 - 03:25

I ended up using view field. But to pass the argument I had to enter the city name a second time in another field while adding the city. I didnt like the way this worked because it is easy for my members to screw it up when they add their own cities.

What I did yesterday was use php code in my 'city' template to call the view. This php also grabs the page title (always the name of the city) and uses that as the arguments. This method does exactly what I want flawlessly.

Now users can add a new city to my website and only see the city name and body fields so its simple. They when the city is created it will automatically pull in any attractions that I have added that are in that city.

I used this method to display 3 views on my city pages, hotels/attractions/restaurants. So far its been working good.

Problems with viewfield and token replacement

activelyOUT - June 10, 2008 - 17:44

I am trying to use viewfield and pass it an argument.

If i specify no argument the unfiltered output does show up in my node.

if I specify an argument, in this case [nid], then I get the empty result message from the view.

If it is not [nid], %nid, or nid, then what works.

I am confused.

Chris

My results if it helps anyone!

ndwilliams3 - October 8, 2008 - 20:48

I got Viewfield working great! I had an events node type with a node reference to a venue node type. I wanted to display a list of events for the venue in the venue node.

I created a view the following settings:

Block view: List view
Fields: Node Title, Date
Arguments: Node Reference(field_venue), Display all values
Filters: Node Published- equal to- yes, Node Type- equal to- event, Date-option-now

added a Viewfield to the venue node type passing a node id token to argument [nid]
on the "Display field" settings on node edit, set the display for Viewfield to "Use block view settings"

I now have a filtered list of events displaying in the venue node!

(Using Drupal 5, Views 1)

 
 

Drupal is a registered trademark of Dries Buytaert.