Thursday, May 7, 2015

Drupal - Custom template file field output

Typically, to control the output of your field you'd just print out all your fields in your template files by doing precisely what you have up there:
<?php print render($content['your_field_name']); ?>
You can have as much html in your template file in addition to these rendered fields and then style them with css! So you can have
<div id="some_field_grouping">
  <?php print render($content['field_1']); ?>
  <?php print render($content['field_2']); ?>
</div>
And then theme this with css using any of the ids and classes you get and whatever the fields themselves render.
If you really need to modify the field's output more precisely and the standard output from print render doesn't work for you, you could certainly look into the field structure itself and grab the individual field parts you need. I'd only do this if you absolutely need to...
To start peering into the exact structure of the fields, I'd
  • Install and enable the devel module... You can just enable the 'Devel' module that comes with it for this purpose
  • In your template file, put a
    <?php dsm($content['your_field']); ?> 
    statement somewhere. That function is only available if you have the devel module enabled, and it will print out the field array for you to inspect in detail. Here is an example of the dsm output: enter image description here
  • Once you can look into the structure of that array, you can take any pieces of the field you need, if you don't want to render it in the default way.
So, you can do something like getting only the uri of your image...
<img src="<?php print $content['field_image'][0]['#item']['uri']; ?>" >

From: http://drupal.stackexchange.com/questions/73177/custom-template-file-field-output

0 comments:

Post a Comment

Breaking News
Loading...
Quick Message
Press Esc to close
Copyright © 2013 Technology KB All Right Reserved