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
Comment #1
druplicate commentedWhoops - 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/');Comment #2
dawehnerThats 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.
Comment #3
druplicate commentedThanks for the tip.
But it still won't work:
No matter what I put for xxx it just redirects to the home page or I get a parsing error.
I tried
$node->nidfor 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...
Comment #4
dawehnerWhere did you tryed to write this code? In which theme file? There you will see at the top, the list of availible variables.
Comment #5
druplicate commentedThe 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.Weird.
This code works properly:
Comment #6
dawehnerYou can access the result variables via $view->result
Comment #7
druplicate commentedFound it!
dsm($view) shows: $view->result[0]['nid']
But this code results in a parsing error:
Comment #8
dawehnerIts
$view->result[0]->nid
Comment #9
druplicate commentedOk, 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.
Comment #10
dawehnerSo is this fixed now?
Comment #11
druplicate commentedNo, 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.
Comment #12
dawehnerCome 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.
Comment #13
druplicate commentedNot 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.
Comment #14
druplicate commentedChanged version to 1.0 from dev.
Comment #15
druplicate commentedSolved 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: