I have a content type called specials
In that content type I have a field called discount code which has a machine name of field_discount_code
I have a custom node template called node--specials.tpl.php that I am using to fomate the display of this content type
If the discount code if filled in by the user I want drupal to show some text if it is left empty I want it show some other generic text
here is the code I am trying but not having any luck with
<?php if (!empty($content['field_discount_code'])): ?>
<p>Call <strong>1-800-661-2555</strong> to contact our Guest Service specialists to reserve this package or book online by using discount code "<strong><?php print render($content['field_discount_code']);?></strong>".</p>
<?php else: ?>
<p>Call <strong>1-800-661-2555</strong> to contact our Guest Service specialists to reserve this package.</p>
<?php endif; ?>
If I leave the discount code empty drupal is still showing the first text chunk but with nothing in the render($content['field_discount_code']); area.
so like this
Call 1-800-661-2555 to contact our Guest Service specialists to reserve this package or book online by using discount code "".
I have also tried
<?php if ($content['field_discount_code'] == "NONE"): ?>
text to display here
<?php else: ?>
text to display here
<?php endif; ?>
but no joy I am relatively new to drupal dev so perhaps I am doing this the wrong way any insight would be greatly appreciated!
Comments
<?phpprint
print render($content['field_discount_code']);
will render the entire field, including the label, you can hide the label in Field Display settings or only print what you actually need:
print render($content['field_discount_code']['#items'][0]['safe_value']);
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Thanks!
Thanks jeff this worked I just used
<?php if (!empty($content['field_discount_code']['#items'][0]['safe_value'])): ?>
in my conditional an was good to go