Case: I am using conditional fields module to hide some dependent fields based on the value of its dependee field. I have created a custom node--content-type.tpl.php template for my content type.



Here, Field2 is dependent on the value of Field1. Suppose in this case, Field2 is rendered only if Field1 has value 'jobs'. Otherwise, it is hidden from both node edit form and node page.

When using seven theme and its display(default or teaser) to view node, if the Field2 is hidden or the dependency was not triggered the Field2 is not rendered on the node page, which is correct.

But, when using my custom template, I am not been able to prevent the rendering of Field2 on my node. It is shown with its default value on the node.

Please help.

template code:

<?php if(isset($content['field_job_type'][0]['#markup'])):?>
                <p class=" no-margin "><strong>Job Type:</strong> <?php print $content['field_job_type'][0]['#markup'];?></p>
            <?php endif;?>
  • please show the template code
    – Geoff
    Jul 1 '15 at 3:43
  • I used devel module to get variables.
    – echo
    Jul 1 '15 at 3:51
  • well, if the default template doesn't show it, but the code you presented does, then the if statement is always true, so you either need to change the if statement, or use some other mechanism for determining it. I would wager a guess that it is only hide() ing the field not unset() ing it, meaning your if statement WILL always be true since it doesn't remove the actual data, just tags it as hidden - this can be found in the field array
    – Geoff
    Jul 1 '15 at 4:10
  • Yes true, I found something 'content['field_job_type']['#access']' which is a boolean value. It worked :) This will surely help others who will face the same situation.
    – echo
    Jul 1 '15 at 4:19

Based on the comments:

  • generic template works, but custom does not

  • custom template overrides visibility rules

I would surmise that the problem is with the visibility, since the conditional field is likely only hidden, and not actually being removed - this would cause the code to execute every time, while in a standard render() it would remain hidden

Using Devel and specifically the dpm() function, I would find the element in the field array that manages the visibility of the field and use that as the basis for the if statement.

A suite of modules containing fun for module developers and themers

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.