I want a block with a text input field and a submit button to load a node that has a corresponding field number entered in that field. The field is just a CCK field that stores an ID number with a one-to-one correspondence with its node.

So rather than try to figure out how to use PHP to do a MySQL query in the block itself, I tried to use Views to do it.

I created a view with the (hidden) NID field first, then used a customfield and entered the following PHP in the 'value' section, and enabled PHP as the input type. I also created the filter for the ID number and exposed it in a block.

<?php
header('Location: http://192.168.1.12/node/5432/');
?>

This works - when the user types the ID number (4063) in the input field and clicks 'submit' Views looks up the ID number and spits out the NID, 5432 in this case, and redirects to the node display. But for the life of me I can't figure out how to replace the number with a token, [nid] or even a PHP print statement like: print $node->nid.

I replaced 5432 in the above code with:

'[nid]'
"[nid]"
"<?php print $node->nid ?>"

and some other combinations - obviously I'm no PHP expert. Also tried putting the same PHP code with HTML tags in the "rewrite the output of this field" section but apparently that field only allows html and not PHP as it didn't work either.

How can I make this work? Is there another easy way using Views or not, to do this - send the user to the node that corresponds to the ID field number?

Comments

druplicate’s picture

Whoops - for some reason the code display is messed up - it added a bunch of stuff.

Here's what is looks like after I stripped the PHP tags.

header('Location: http://192.168.1.12/node/5432/');

dawehner’s picture

 Also tried putting the same PHP code with HTML tags in the "rewrite the output of this field" section but apparently that field only allows html and not PHP as it didn't work either.

Thats good that there is no php allowed. The thing is that you can crash your site very easy with php input. Additional you can achieve the same stuff with php files, put you need php knowledge for this.

I would suggest to use drupal_goto and using theming of a field for example.

druplicate’s picture

Thanks for the tip.

But it still won't work:

<?php
 drupal_goto('node/'. xxx);
?>

No matter what I put for xxx it just redirects to the home page or I get a parsing error.

I tried $node->nid for example but that apparently is not the NID from the views filter. And I can't use tokens in PHP either.

Tried putting dsm($fields) in various tpl files like views-view--page.tpl.php and page.tpl.php but it shows nothing.

Since Views requires a path I just put a dummy in: node/id-redirect

Confused...

dawehner’s picture

Where did you tryed to write this code? In which theme file? There you will see at the top, the list of availible variables.

druplicate’s picture

The PHP redirect code is placed in the "value" text area in a custom field in a view. Customfield is a plugin module to Views. Then I turned on the input type PHP code for that text area.

I put: <?php dsm($rows); ?> in views-view--page.tpl.php (in the Acquia Marina theme directory) and it spit out some code instead of variables when I look at the dummy page, /node/id-redirect.

<div class="views-row views-row-1 views-row-odd views-row-first views-row-last">
      
  <div class="views-field-markup">
                <span class="field-content"></span>
  </div>
  </div>

</span>

Weird.

This code works properly:

<?php
drupal_goto('node/'. '5432');
?>
dawehner’s picture

You can access the result variables via $view->result

druplicate’s picture

Found it!

dsm($view) shows: $view->result[0]['nid']

But this code results in a parsing error:

<?php
drupal_goto('node/'. $view->result[0]['nid']);
?>
dawehner’s picture

Its
$view->result[0]->nid

druplicate’s picture

Ok, that fixed the parsing error but the redirect still doesn't happen and I get a pop-up box saying there was an error when Views ran the preview:

"An error occurred at /en/admin/build/views/ajax/preview/property_id_search." Followed by a whole bunch of statements like this: @import "/modules/acquia/cck/theme/content-module.css";

"property_id_search" is the name of the view.

So then I just put this code in:<?php print $view->result[0]->nid;?> and although there are no errors, the nid is not output - the page is blank.

The nid isn't available until the view is run so apparently it's not available to the customfield plugin in the process though it should be since the nid field is above the customfield in the fields table.

I'll bet I could just use the rules module to accomplish this - it's worth a shot.

EDIT:Then I deleted everything in the "value" section and just added the [nid] token to the "Rewrite the output of this field" section and it printed the correct nid.

Strange.

dawehner’s picture

So is this fixed now?

druplicate’s picture

No, the variable $view->result[0]->nid is empty as I proved when I tried to print it as an experiment.

For some reason that variable isn't populated at the time customfield needs it.

I also tried using the nid token as an experiment, but I can't do a redirect using a token. Can't use tokens in PHP.

dawehner’s picture

Project: Views (for Drupal 7) » Views Custom Field
Version: 6.x-2.10 » 6.x-1.x-dev
Component: node data » Code

Come one, this is a custom field support question.

There are reasons why the maintainer of views, doesn't like customfield: You don't have the power of real php in eval code.

druplicate’s picture

Not sure exactly what you mean about eval code, but now I think you are right - thanks for moving it to their issue queue. I'll pick it up over there.

druplicate’s picture

Version: 6.x-1.x-dev » 6.x-1.0

Changed version to 1.0 from dev.

druplicate’s picture

Status: Active » Fixed

Solved it!

Just noticed when I add a customfield to the view, there's a little note that says I should be using $data->nid.

This code now works:

drupal_goto('node/'. $data->nid);

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.