1

I have a Netrunner Card content type. (It's a card game I play.) I have a "field_card" field that holds the card image. In the node--netrunner-card--Runner_Identity_Card--full.tpl.php file, I am trying to control the output of each field. Instead, I seem to just be dumping the same fields on the screen.

I tried using the following code.

<img src="<?php print render($content['field_card'])?>" />

It scattered the HTML, because the output of render() is the HTML. What is the standard practice for doing what I am trying to do?

I'm trying to theme the output of each card to make it look like I want it to be, and to learn the theming engine for Drupal.

3

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']; ?>" >

Anyway, hope this helps!

  • Boriana... you are my heroine... – randomblink May 15 '13 at 19:21
  • I installed the Devel module a few weeks ago but it is still a massive mystery. You just unlocked like 95% of what has plagued me, and the other 5% just needs time and for me to play with it. I seriously can't thank you enough. I have played 'a little' with the template.php... blinks_responsive_field__field_subroutines__netrunner_card($variables) But I feel fairly lost on when it is appropriate to mess with template.php vs tpl.php or whether it's all preference. – randomblink May 15 '13 at 19:23
  • I'm so glad this was helpful!! – Boriana Ditcheva May 15 '13 at 19:24
  • And, by the way, we all struggled with figuring out drupal fields and theming at the beginning. It takes time and asking questions to really get into it! – Boriana Ditcheva May 15 '13 at 19:25
  • I have had a WordPress buddy of mine smack talk me every time I bring up Drupal, lol. He and his 'people' only use Wordpress because they can get it to do whatever they want. But I don't like Wordpress... I've had to learn it because it lets me throw together a website in hours versus days, but I always feel like I just inherited a marrionette and the strings are all cockeyed now... I got into programming via MS Access 95. I built DB's, learned my way around forms, then started custom building reports. Next thing I knew I had learned VBA... I am trying to do the same thing with Drupal. – randomblink May 15 '13 at 19:27

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.