Hi,

I am working on a project for last 6 months.. everything is complete and I purchased domain and hosting plan. BUT, the famous views is showing me a lot of issues NOW.....

i have three (3) content types: shop, hospital and street. Hospital and shop can have a CCK node reference field to a street (autocomplete).

The pathauto module is helping me to make the automated paths, as following:

for street content type: [nid]/[title-raw]
for hospital content type: [field_street_reference-nid]/[title-raw]
for shop content type: [field_street_reference-nid]/[title-raw]

The pathauto is generating the paths correctly.

Now, i want views to display a block in the right side, consisting of a list of hospitals and a list of shops, WHOSE [field_street_reference-nid] is in the URL.

So, i gave argument as: Defaults: Configure Argument Content: street reference (field_street_reference) with properties: provide default argument::Node ID from URL, basic validation.

Now, when i am opening a street node ( URL: "www.mySite.com/4/street_example" ), its working correctly. i.e., if street node ID is 4, the block is showing all the hospitals and shops WHERE [field_street_reference-nid] is 4.

But, when i am trying to view a hospital ( URL: "www.mySite.com/4/hospital_one" ), its not showing the block, its not taking the argument...

WHY ?

plz explain. thanks.

Comments

nevets’s picture

I am guessing you view is set up to take Node: nid as an argument with node from URL as the default argument.

Views sees the unaliased path, so it see the path node/{nid} regardless of type. Personally I would not place the nid in the path (I doubt its very good for SEO). My first thought would be to use PHP code for the default handing and code something like this

$node = menu_get_object();
if ( !empty($node) ) {
  switch $node->type {
  case 'street':
     return $node->nid;

  case 'hospital':
  case 'shop':
     return $node->field_street_reference[0]['nid'];
  }
}

return 0;
amrit_b’s picture

thanks nevets.

how will i know that views only accept internal actual URL's....??? :-(
Ya, i knew that placing the nid's in the url alias is not good for SEO. I did those just to make the view thing happen.. Now, its a wastage....

From where did you came to know about these functions and variables? is there any reference manual? or any book?? i know php properly and the algorithm to write. But, i dont know what are the variables and functions to use.

Did u knew the solution or searched the API reference?? what did you searched?? ;-)
frankly, I want solve these types of problems on my own..

One more doubt:
I always have to set "Action to take if argument is not present :: Provide default argument :: Node ID from URL"

Why it doesn't work as "Action to take if argument is not present :: Hide view / Page not found (404)"

thanks...

nevets’s picture

Block views need to use "Action to take if argument is not present :: Provide default argument" since blocks do not have arguments (pages do). "Node ID from URL" works when displaying nodes and uses the node id (nid) of the displayed node. In your case though you need to use PHP code since you want the argument to come from other sources.

As for where the answer came from, I have been using Drupal for a while and have answered similar questions in the past.

While both Developing for Drupal and API reference are useful they will not answer all your questions. The former is good reading material, the API a good reference but in both cases it helps to know Drupal terminology and what you want to do.

For you problem I know that menu_get_object() returns a node if viewing a single node and nothing if not. I also know I can use drupal_set_message("Node:<pre>" . print_r($node, TRUE) . '</pre>'); to examine the node returned and how the values are stored. Another useful tip, if you have the PHP filter enabled you can use a node as scratch pad to test code.

amrit_b’s picture

ya you have a lot of experience with drupal. i have only 4 months.. ;-)

still not working... I tried yor code. but not working. before, at least while viewing the "street" nodes, the blocks were showing referred hospitals and shops (the actual path was node/4, and the block was showing all the referred contents with nid=4 )

Now, after the changes, its not showing anything.. I created a new node and tried

$node = menu_get_object();
drupal_set_message("Node:<pre>" . print_r($node, TRUE) . '</pre>');

its returning correct values.

like,
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . .
[field_street_reference] => Array
(
[0] => Array
(
[nid] => 4
)

)

[last_comment_timestamp] => 1269921562
. . . . . . .
. . . . . . . . . . .

but the views block is not coming anywhere, even not with the street nodes..

My custom PHP:

$node = menu_get_object();
if ( !empty($node) ) {
switch $node->type {
case 'street':
return $node->nid;

case 'hospital':
return $node->field_street_reference[0]['nid'];
case 'shop':
return $node->field_street_reference[0]['nid'];
}
}
return 0;

But, when i am manually giving arguments in the "Live Preview", its giving correct results... What next?

thansk.

nevets’s picture

In the views default PHP code, after $node = menu_get_object(); add drupal_set_message("Node Type: " . $node->type); and visit a node of type street hospital and shop and check the actual types.

amrit_b’s picture

i created a simple block separately and custom php coded:

$node = menu_get_object();
drupal_set_message("Node Type: " . $node->type);
drupal_set_message("Amrit Referred node: " . $node->field_street_reference[0]['nid']);
if ( !empty($node) ) {
  switch $node->type {
  case 'street':
     drupal_set_message($node->nid);
  case 'hospital':
     drupal_set_message($node->field_street_reference[0]['nid']);
  case 'shop':
     drupal_set_message($node->field_street_reference[0]['nid']);
  }
}

And gave me an ERROR.

Our mistake was using "$node->type" instead of "($node->type)" in the switch case statement.. After doing the correction, it worked PROPERLY

Thnaks for the help sir.. do you have your own website or blog??

thanks again!!

nevets’s picture

Like the shoe maker, I only make websites :)

amrit_b’s picture

nice...

ok then show me some of your work. just want to see how are u using this enormous amount of knowledge.. Like, i was thinking, "IF" some day i become like you, what will be my possibilities........ and capabilities............................

:-)

are you in gTalk??

Slovak’s picture

A solution that involves a view argument in the midst of a path like street/%/hospital see Node ID as View Argument from SEO-friendly URL Path