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.

58 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...

generic xanax xanax dosage vs lorazepam dosage - how much does generic xanax cost without insurance

Anonymous said...

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

Anonymous said...

buy cheap xanax xanax side effects dosage - xanax 2 mg snorting

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...

tramadol no rx tramadol x 225 - buy tramadol online usa

Anonymous said...

carisoprodol 350 mg carisoprodol 350 mg vs flexeril - drug carisoprodol prescribed

Anonymous said...

generic xanax does xanax show up in hair drug test - buy xanax online with a prescription

Anonymous said...

buy tramadol online tramadol dose for kids - get rid tramadol high

Anonymous said...

buy tramadol online tramadol hcl 93 58 - does generic tramadol look like

Anonymous said...

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

Anonymous said...

xanax sale xanax xr generic cost - buy xanax online no prescription needed

Anonymous said...

no prescription xanax xanax grapefruit juice effects - xanax withdrawal like

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...

generic xanax green generic xanax bars - xanax vs weed

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...

xanax online buy generic xanax online no prescription - xanax 1 mg at a time

Anonymous said...

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

Anonymous said...

xanax online xanax generic cheap - 1.5 mg xanax overdose

Anonymous said...

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

Anonymous said...

buy tramadol online tramadol 50mg for dogs safe for humans - ultram side effects headache

Anonymous said...

http://landvoicelearning.com/#21906 tramadol liver damage - tramadol overdose dog

Anonymous said...

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

Anonymous said...

learn how to buy tramdadol tramadol hcl 50 mg picture - tramadol 50mg norsk

Anonymous said...

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

Anonymous said...

http://landvoicelearning.com/#30896 is buying tramadol online illegal - tramadol ratiopharm 100mg nebenwirkungen

Anonymous said...

http://buytramadolonlinecool.com/#63102 tramadol high lasts - tramadol high doses

Anonymous said...

buy tramadol where to buy tramadol online usa - buy from online texas tramadol

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 ativan online dosage of ativan for anxiety - ativan every 8 hours

Anonymous said...

buy ativan online ativan withdrawal drug - ativan overdose seizures

Anonymous said...

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

Anonymous said...

buy xanax online saliva drug test detection times xanax - buy xanax 2mg bars

Anonymous said...

http://staam.org/#37708 tramadol for dogs usa - tramadol addiction lawsuits

Anonymous said...

buy xanax online xanax dosage green pill - long can xanax show up drug test

Anonymous said...

buy tramadol online tramadol addiction fda - tramadol for dogs high

Anonymous said...

xanax no prescription xanax and zoloft overdose - 6 bars xanax

Anonymous said...

http://ranchodelastortugas.com/#82165 buy alprazolam australia - what is xanax yahoo

Anonymous said...

buy tramadol with paypal tramadol withdrawal paws - buy tramadol online checking account

Anonymous said...

buy xanax online can you buy xanax online legally - what is xanax made of

Anonymous said...

buy xanax online 1mg xanax 5mg valium - xanax drug dogs

Anonymous said...

buy xanax online xanax pills bars - xanax vs valium

Anonymous said...

buy xanax online generic xanax online no prescription - drug facts for xanax

Anonymous said...

buy xanax online overdose of xanax - is it safe to buy xanax online

Anonymous said...

best place to buy xanax online xanax side effects pregnancy - xanax 1mg football

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