1

I have a boolean single check-box field. and inside my node--content-type.tpl.php Idothe following check:

  <?php if($content['field_hide_1st_line_of_the_addre']['und'][0]['value'] == 0): { ?>
    <?php print render($content['field_images']['#object']->field_location['und'][0]['street']) . t(', '); ?>
    <?php } else: { ?>
    <?php print t(''); ?>
    <?php } endif; ?>

And it returns loads of "Notice: Undefined index: und in include() (line 80 of node--content-type.tpl.php"

Any idea what is going on?

UPDATE

  <?php if (isset($content['field_hide_1st_line_of_the_addre']['und'][0]['value'])): { ?>
    <?php print render($content['field_images']['#object']->field_location['und'][0]['street']) . t(', '); ?>
    <?php } else: { ?>
    <?php print t(''); ?>
    <?php } endif; ?>

is not the doing the check correctly: the Street field disappears forallteasers,while it should only disappear for those that have the boolean check-box ticked.

UPDATE 2 This code didn't work either. Pave me a Parse error: syntax error, unexpected $end

<?php 
  if (isset($content['field_hide_1st_line_of_the_addre'][LANGUAGE_NONE][0]['value'])) :
  if($content['field_hide_1st_line_of_the_addre']['und'][0]['value'] == 0): { ;
    print render($content['field_images']['#object']->field_location['und'][0]['street']) . t(', ');
  }  endif; 
?>

UPDATE 3

I deleted that old field and created a new identical one. Tried many things,but it turns out that for some unknown reason it just didn't like the 0 and preferred 1 instead.So I used 1,and just changed the wording of the field from "Hide full address" to "Display full address". This is the code that worked for me. If you seeany syntax mistakes,pleaselet me know.

<?php 
    if (isset($content['field_hide_address']['#items'][0]['value'])) :
    if($content['field_hide_address']['#items']['0']['value'] == '1'):
    print render($content['field_images']['#object']->field_location['und'][0]['street']) . t(', '); 
    endif;
     endif; ?> 
1

The error you're getting simply means that

$content['field_hide_1st_line_of_the_addre']['und']

hasn't been set. It might be that the 'und' key is missing, or it might be that $content['field_hide_1st_line_of_the_addre'] simply hasn't been set at all. This can happen if you've added a new field to a content type that already has existing nodes (and probably in other situations too).

To remove the notice, you can just wrap your code in another condition that ensures the array element you're accessing exists. For example:

<?php 
  if (isset($content['field_hide_1st_line_of_the_addre'][LANGUAGE_NONE][0]['value'])) :
    if ($content['field_hide_1st_line_of_the_addre'][LANGUAGE_NONE][0]['value'] == 0) : 
      print render($content['field_images']['#object']->field_location['und'][0]['street']) . t(', '); 
    endif;
  endif; 
?>

I've taken the liberty of replacing 'und' with the constant equivalent (and cleared up a couple of other very minor issues), it's better practice. Even better practice would be to extract this logic out of the template file completely, and instead implement it in a preprocess function.

  • See update 2...It didn't work – drupalina Oct 17 '13 at 9:38
  • You seem to have added some extra brackets: Use either if ($foo == 'bar') { or if ($foo == 'bar'): , never try to combine them (see Alternative syntax for control structures for more info) – Clive Oct 17 '13 at 9:40
  • I updated the answer, I missed out the 2nd conditional. Try that exactly as it is, no changes – Clive Oct 17 '13 at 9:41
  • I tried your updated code (exactly). Still doesn't work. In teaser "Manage display" section it is set to "Default". – drupalina Oct 17 '13 at 9:45
  • I'm afraid I'm out of ideas - check that the field is named correctly, and that $content['field_images']['#object'] contains what you expect. That's all I can advise really – Clive Oct 17 '13 at 10:09

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.