0

I'm having an issue with an undefined index I really could use some help with.

This is the error I get:

Notice: Undefined index: und in include() (line 15)

This is what my line 15 looks like:

<div class="House_Header_coupon_link">
  <a href="
      <?php print $node->field_house_coupon_link['und'][0]['url']; ?>
    " style="color:#
      <?php print $node->field_house_coupon_link_hex_code['und'][0]['safe_value']; ?>
    ">
      <?php print $node->field_house_coupon_link['und'][0]['title']; ?>
  </a>
</div>

I know the error is happening because sometime the field is not given a value and allowed to be blank. I'm using Drupal 7.23 and I've tried using the isset method mentioned elsewhere on this site but it's not working and I'm out of ideas

Thanks!

  • 2
    Could you please format your code? Add some line breaks? For readability, you know. – Mołot Oct 21 '13 at 18:58
  • 2
    If you reformat your code to break your print statements onto separate lines, it will tell you which print statement is failing. – Scott Joudry Oct 21 '13 at 19:00
  • I edited for the OP just for fun. – Kari Kääriäinen Oct 21 '13 at 20:23
3

Before assigning the values to the field directly you must check if it contains any value or not otherwise you will get the notice like above, try to check the value something like below (the below code is for reference only, you can modify it as per your requirement):

<div class="House_Header_coupon_link"><a href="<?php
print isset($node->field_house_coupon_link[LANGUAGE_NONE]) ? $node->field_house_coupon_link[LANGUAGE_NONE][0]['url'] : ""; ?>"
style="color:#<?php print isset($node->field_house_coupon_link_hex_code[LANGUAGE_NONE]) ? $node->field_house_coupon_link_hex_code['und'][0]['safe_value'] : ""; ?>">
<?php print isset($node->field_house_coupon_link[LANGUAGE_NONE]) ? $node->field_house_coupon_link['und'][0]['title'] : ""; ?></a></div>

Also it is recommended to use LANGUAGE_NONE instead of und: The language code used when no language is explicitly assigned. Defined by ISO639-2 for "Undetermined".

  • Thank you so much for the help and explanation, it was a big help :) – StephanieF Oct 23 '13 at 15:53
  • @Stephanie I am glad it helped you :) – Ankit Agrawal Oct 23 '13 at 15:55
0

While the 'error' is being logged, a notice is not really an error, just a 'hey look at this' kind of message ... But yes they are annoying and should be corrected.

'und' may not always be the correct language code. While LANGUAGE_NONE is what you should use, you should also check for the corresponding node language type:

if ( isset($node->field_house_coupon_link_hex_code[$node->language]) )
  print $node->field_house_coupon_link_hex_code[$node->language][0]['safe_value']
else
  print $node->field_house_coupon_link_hex_code[LANGUAGE_NONE][0]['safe_value']
0

Assuming that this is a node template page, you can simply use the field names as variables in renderable array form, and render them like:

<div class="House_Header_coupon_link">
  <a href="<?php print render($field_house_coupon_link); ?>" style="color:#<?php print render($field_house_coupon_link_hex_code); ?>">
    <?php print render($field_house_coupon_link); ?>
  </a>
</div>

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.