0

I am trying to print a value of submitted for a google map field in a page.tpl.php file. I used below code to print but got a error message. I am able to print other field types using similar code but unable to do the same for map filed. Please help

<?php
  $node=node_load(41);
  print "<div id='map'>".$node->field_add_map['und'][0]['GoogleMapField']."</div>";// print styled image
?>

google map filed in a content type

3
  • What's the error message?
    – calebtr
    Jan 20 '15 at 5:08
  • Create a view as block for this map field and show on any desired region on page.tpl.php
    – Rupesh
    Jan 20 '15 at 5:12
  • Creating a view may be a valid solution depending on the requirements, however if you are just printing a single static item I would say it is not a good choice because it adds unnecessary complexity and processing overhead.
    – rooby
    Jan 20 '15 at 5:18
1

For a start don't load the node in the template file like that.

If you want to load a node do it in a template preprocess function in your theme's template.php file, like this for example:

/**
 * Variable preprocess function for the page template.
 */
function THEMENAME_preprocess_page(&$variables, $hook) {
  // Load a node and save it to the variables array.
  // This will then be available in the variable $node, in the page.tpl.php template.
  $variables['node'] = node_load(41);
}

However don't use $variables['node'] in the page template because it is already in use. If you are on a node page then that variable will already be populated with the node object of the current page, so in your template you can access it using $node.

If you are aloading a node that isn't the current page node then you can do it your way but give the variable a different name, like $variables['map_node'] for example, which would then be available in the template file as $map_node.

On to the field printing.

The problem is you are trying to print the GoogleMapField property of the field value, which as far as I know doesn't exist.

The values in this field type are:

  • lat
  • lon
  • map_width
  • map_height
  • zoom
  • name

So doing what you are doing you could print those out, however I'm thinking you probably don't want those either.

So you want to print the rendered field item. There are a few ways this can be done but a good option is using the field_view_field() function like this:

/**
 * Variable preprocess function for the page template.
 */
function THEMENAME_preprocess_page(&$variables, $hook) {
  // Load a node and save it to the variables array.
  // This will then be available in the variable $node, in the page.tpl.php template.
  $variables['map_node'] = node_load(41);
  // Get the rendered markup for the map field to print in the template.
  $variables['rendered_map'] = field_view_field('node', $map_node, 'field_add_map', 'full');
}

That example uses whatever display configuration you have set on the "full" display mode for your node type for that field. You could use "teaser" or some other view mode if you prefer. You could even add a custom view mode specifically for this if it suits your use case.

Then in your page template you could do something like this to print the map using the render() function:

if ($rendered_map) {
  print render($rendered_map);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.