I have a script that writes the base of an article for me, it's created in Computed Field. It takes a text field ,(field_title_connstant) as a variable and puts the standard text and links together. I am now trying to integrate some geo-targeting, I have got it to the point where the cookie is set and I can retrieve it. My initial problem was that the first user to load the page set the geo-location for subsequent visitors of that page until the cache was cleared.

I fixed that by moving the code to the "display field" - my code is recalculated every visit to the page.

The problem I am having now is I cant send field_title_connstant to the code , its just an empty value. Any help ?
Is there any way to pass an argument to the display field?

"Display this field" is ticked
"Store using the database settings below" is not ticked
This is an approximation of what I have in the "Display Format: " field

$art_name = $node->field_title_connstant[0]['value'];
$cook = $_COOKIE['geoCode'];

switch ($cook){

case "AU": 
       $art = "Hello Australians we hope the article on  $art_name  is of some help."
       $display = $art ;
       break;

case "UK": 
       $art ="Hello Brits we hope the article on  $art_name  is of some help."
       $display = $art ;
       break;

case "US": 
       $art = "Hello Americans we hope the article on  $art_name  is of some help."
       $display = $art ;
       break;

default:
       $art= "Some generic welcome"
       $display = $art ;
       break;

;}

Any Ideas would be helpful, I have searched for an answer but couldn't find anything, I have only been learning PHP and Drupal recently.

Comments

jastraat’s picture

My initial reaction:
The code you have above should be in the 'Computed Code' textbox, not the display format textbox. But instead of setting the value of the $display variable, set
$node_field[0]['value'] = $art;

Then - in the Display Format field, just keep the default code:
$display = $node_field_item['value'];

LuisCypher’s picture

That's how I had it in the first place. When set up like that the code works but is not recomputed on every load which means the cookie value is cached.
So if you are in America and you visit the page it gets set to "US", then I vist later and the "codes" $cook value stays "US" instead of recomputing with my cookie value of "AU" . This behaviour will last until the server cache is cleared.

That's why I moved it to the Display Field, it gets recomputed every visit. For some reason though I can't get this line to work.

$art_name = $node->field_title_connstant[0]['value'];

I feel like I am making a very basic mistake but inexperience doesn't allow me to see it. Or maybe Computed field isn't designed to work like this.
Are there any other ideas , or can I compute the line above in the 'Computed Code' text box and somehow send it to the display field

that contains my current code? how would I do this?

jastraat’s picture

I don't think the Display Field area is intended for calculating the value of the field - however - a couple of ideas -

You might be able to get the node ID and load the node manually again in the Display Field area. Not the most efficient method, but if it works, it might be the easiest way around your problem.

if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node_id = arg(1);
  $node = node_load(arg(1));
}

Some more ideas: The 'computed code' textbox is reevaluated every time the node is LOADED (as long as the value isn't stored in the database). The display field code is evaluated on node VIEW. The node is loaded each time it is viewed, but I believe it happens before the HTML of the page is created. So - what may be going on is the node loads before the cookie is reset. You could look into where your cookie is being set and play with manually emptying the cache/setting the cookie in a different place.

LuisCypher’s picture

I haven't had much luck yet, but you have given me some things to think about.
Thanks for including some code , it always makes it easier to learn with examples.

If I find a solution I will post it here so it can be found by others . While trying to find a fix I found other people had similar problems but never posted the solution.

LuisCypher’s picture

In the "Computed Code: "

$node_field[0]['value']= $node->field_title_connstant[0]['value'];

I just use the "$node_field[0]['value']" to pass the fieild I wanted instead of using it to set the actual output of computed field, usually when computing in the display field this value of this in the Computed Code field is set to "" or an empty string

Iin the "Display Field :"
I compute the code so it is evaluated each time its viewed, this way I can grab info from the cookie without it being messed up by the cache and pass a field as input to be used in the rest of the code.

$art_name = $node_field_item['value'];
$cook = $_COOKIE['geoCode'];

I am just replying to myself incase someone else needs to do this. Maybe allowing them to fond it via search.

Thanks jastraat for giving me some ideas to try.