0

I have a field named field_event_type. When creating a node, you can select one of two options for this field from a drop-down list:

  • birthday party
  • Wedding

I want to be able to display different fields in my node depending on which type of event is selected. So in node--events.tpl.php, my node template, I wrote the following:

if ($element['#object']->field_event_type['und'][0]['value'] == 'birthday party' )
   {
      print render($content['field_some_field']);
   }

else
   {
      print render($content['field_some_other_field']);
   }

However, this does not seem to be working the way I intended it to, not only is it not displaying the additional field content in either event type, but I'm also getting an error in Drupal about an undefined variable. Is my code incorrect?

6
  • 1
    how is it not working the way you intended? what is it doing, what is it not doing?
    – Geoff
    Commented Apr 30, 2015 at 16:28
  • Thank you I updated the description of the issue I'm dealing with.
    – Jane
    Commented May 1, 2015 at 14:26
  • what undefined variable error are you getting, your code uses 4 variables
    – Geoff
    Commented May 1, 2015 at 14:29
  • First "undefined variable: element" then "trying to get property of non-object". It doesn't get much more specific than that.
    – Jane
    Commented May 1, 2015 at 14:31
  • 1
    $element is not a standard variable in a node tpl file - I believe it is used in a field tpl file though. Is it a custom variable you create?
    – Geoff
    Commented May 1, 2015 at 14:48

2 Answers 2

1

Given your use of the $element variable in a page.tpl.php file, I would suspect a typo or error in its use.

You could either continue to use the page.tpl.php but use $node instead, or use a field tpl instead and continue to use $element. Which you choose really depends on the complexity of the set up and where you want the modifications to lie

2

You may find it easier to use the Conditional Fields Module:

Conditional Fields allows you to manage sets of dependencies between fields. When a field is “dependent”, it will only be available for editing and displayed if the state of the “dependee” field matches the right condition. When editing a node (or any other entity type that supports fields, like users and categories), the dependent fields are dynamically modified with the States API. You can, for example, define a custom “Article teaser" field that is shown only if a "Has teaser" checkbox is checked.

1
  • Thank you for your suggestion, but adding a module is not an option at this time given the way my client has set up his site. The only available option is therefore through the template.
    – Jane
    Commented Apr 30, 2015 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.