Wednesday, 4 July 2012

Themeing Drupal 7: Checking fields

Introduction

A very general thought, will add more specifics when I have done a bit more investigation.
Considering the template end of themeing (so ignoring theme functions for this thought experiment) and restricting to a node template or field templates. Assuming you have have a set of various different fields (some with multiple values perhaps) and you wish to customise the display of the content somewhat perhaps wrapping some of the fields in html to make a carousel or for something else. Is there ever a case where you need write complex PHP in the template/s?

Templates can get nasty

Themeing Drupal is quite a big learning curve, in some organisations there is a clear division between developer and themer, between front-end and back-end development. Americans seem better at maintaining a clear division (although I don't have first-hand experience of this) whilst us Brits don't like to admit we can't do something so seem more likely to be blitting from server configuration, through back-end development, and onto front-end, admitting that you can't at least sort of do something is not easy.
Add tight deadlines to the mix, freelance Front End developers who don't know Drupal but who are used as a resource and before you know it your template files can be full of funky PHP pulling things out of $node to for display (even though they are available in $content) or even getting a reference to the node via an object reference in a field .....
Online I see questions about how to check if a field is empty in the node temple, with a variety of answers provided but never (or not in my experience) a question about why someone needs to do this.

Is it this simple?

Maybe not, in other frameworks typically by the time you get to the template level any variables you are interested will be available directly or via some sort of context. In the template a simple conditional check will allow you to determine what you need to display.
The solution for Drupal surely (please correct me if wrong) is to call render() on the field. You can check what is returned, if the the field is empty or doesn't exist the output of render will be empty. If not you can print it (wrapping it with any html etc.)
Yet again for a key field you can provide a field template. Putting the extras in that template and in the node template you can simply render the field knowing that if it empty nothing will be rendered.
There are of course theme functions and various places prior to the template where you can reach in and adjust the output, these are not immediately accessible to a front end developer without much Drupal experience and the back-end developer may well be too busy or they could be working remotely to each other.
So templates should be simple, free of complex PHP perhaps there are situations where this is not possible, I am investigating.

26 comments:

Anonymous said...

xanax online xanax and alcohol yahoo answers - xanax effects recreational

Anonymous said...

buy tramadol online tramadol for dogs expire - tramadol and alcohol

Anonymous said...

generic xanax xanax overdose like - xanax 2mg sleep

Anonymous said...

cheap generic xanax xanax drug classification - xanax and alcohol cessation

Anonymous said...

order tramadol buy cheap tramadol overnight - tramadol hcl overdose

Anonymous said...

buy tramadol online tramadol withdrawal exercise - tramadol zydus 50 mg

Anonymous said...

buy tramadol online cheap tramadol cod - tramadol 50mg to get high

Anonymous said...

order tramadol overnight tramadol dosage - tramadol 50mg and itching

Anonymous said...

buy tramadol online tramadol no prescription mastercard - get rid tramadol withdrawal

Anonymous said...

buy tramadol online order tramadol online cheap - tramadol to buy with no prescription

Anonymous said...

buy carisoprodol snorting carisoprodol erowid - carisoprodol overdose

Anonymous said...

buy tramadol online tramadol 50 mg abuse - tramadol dosage osteosarcoma

Anonymous said...

carisoprodol 350 mg carisoprodol generic for soma - carisoprodol class

Anonymous said...

carisoprodol 350 mg carisoprodol order online - carisoprodol vs xanax

Anonymous said...

buy cialis no prescription canada cialis with food - online cialis reviews

Anonymous said...

cialis online cialis 5 mg online kaufen - cheap cialis 100mg

Anonymous said...

buy cialis online retail pharmacy price cialis - buy cialis online no rx

Anonymous said...

buy tramadol online buy tramadol 50mg online - tramadol high yahoo answers

Anonymous said...

buy tramadol india buy tramadol medication - buy tramadol online no prescription

Anonymous said...

http://reidmoody.com/#96318 high dose ativan considered - ativan side effects rxlist

Anonymous said...

ways to buy ativan online ativan and alcohol recreational - ativan while pregnant

Anonymous said...

buy tramadol online cod overnight tramadol 50mg tablets an 627 - tramadol 100 mg gotas

Anonymous said...

buy tramadol online tramadol addiction fda - tramadol for dogs high

Anonymous said...

Υour гeport haѕ verified hеlρful to me.
It’s rеally еducatiοnal аnd уou агe ceгtainly quite well-informed
іn this fiеlԁ. Үou get exposed our
sіght to be able to numеrous opіniοn of this specifiс tоpiс using intrіquing, notablе аnd
solid articles.
Feel free to visit my web page :: cialis

Anonymous said...

buy tramadol online with mastercard buy tramadol without rx - buy tramadol india

Anonymous said...

buy ambien pills online buy ambien overnight shipping - ambien to order online