Reputation: 614
I'm using the tutorial found here to display the directions based on Google Maps Directions API in Drupal. I have two fields in my node - originfield and destinationfield. Each of them contains the name of a city.
The problem is that I can't print the field value inside of the $params array. Here is the whole tpl.php code:
<?php
/**
* @file
* Returns the HTML for a node.
*
* Complete documentation for this file is available online.
* @see https://drupal.org/node/1728164
*/
?>
<?php
// This print works:
print $node->field_originfield['und'][0]['value'];
// Parameters used for the request
$params = array(
'origin' => 'Tokyo, Japan', // I can't find a way to output the originfield's value here
'destination' => 'Kyoto, Japan' // I can't find a way to output the destinationfield's value here
);
// Join parameters into URL string
foreach($params as $var => $val){
$params_string .= '&' . $var . '=' . urlencode($val);
}
// Request URL
$url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');
// Make our API request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);
// Parse the JSON response
$directions = json_decode($return);
// Show the total distance
print('<p><strong>Total distance:</strong> ' . $directions->routes[0]->legs[0]->distance->text . '</p>');
?>
// This print works:
<?php
print $node->field_originfield['und'][0]['value'];
print $node->field_destinationfield['und'][0]['value'];
?>
If I manually add origin and destination everything works fine. But if I use any of the following combinations, the whole page gets either messed up and nothing is displayed or nothing happens at all.
'origin' => "<?php print $node->field_originfield['und'][0]['value']; ?>"
'origin' => "print $node->field_originfield['und'][0]['value'];"
'origin' => "$node->field_originfield['und'][0]['value'];"
'origin' => "field_originfield['und'][0]['value'];"
'origin' => "field_originfield;"
'origin' => "<?php print render($content['originfield']); ?>"
I have also tried this, without luck:
$departure = print $node->field_originfield['und'][0]['value'];
$arrival = print $node->field_destinationfield['und'][0]['value'];
'origin' => 'departure',
'destination' => 'arrival'
What am I doing wrong here?
Upvotes: 2
Views: 353
Reputation: 542
Try this
$departure = $node->field_originfield['und'][0]['value'];
$arrival = $node->field_destinationfield['und'][0]['value'];
'origin' => $departure,
'destination' => $arrival
Upvotes: 2
Reputation: 41737
Please try this:
$params = array(
'origin' => $node->field_originfield['und'][0]['value'],
'departure' => $node->field_destinationfield['und'][0]['value']
);
or
$departure = $node->field_originfield['und'][0]['value'];
$arrival = $node->field_destinationfield['und'][0]['value'];
$params = array(
'origin' => $departure,
'destination' => $arrival
);
Upvotes: 2