• I added a boolean field named Fee waived to the user account.
  • On the edit form, this field is represented as a single checkbox.
  • Users cannot edit the value; only admins can see and edit it (I'm using field_permissions).
  • When the checkbox is selected, Fee waived: Yes appears in the user profile. I want this.
  • When the checkbox is NOT selected, Fee waived: No appears in the user profile. I don't want this. I don't want anything to show (because I don't want users wondering how to get their fee waived).

By default, when a user account field is empty, nothing is displayed for it in the user profile. However, a non-checked checkbox is not empty; it has a value of 0 (or whatever has been defined for it). This is why I get Fee waived: No in the user profile.


Is there a simple way to keep the field from being displayed in the user profile when it is not selected?

Attempted solutions:

  • If the conditional_fields module would allow a field to be dependent upon itself, that would work beautifully to solve the problem. But it does not. I tried disabling the 'same field' check in the module's code. This works, in that the field is hidden exactly as I want it to be, but when the dependent and dependee fields are the same: (1) the dependency configuration form ceases to hide irrelevant fields due to a Javascript error and (2) a PHP error is thrown when the dependency is saved. These can be ignored and the dependency still works, but it makes the solution ugly.

  • I tried other modules that allow value-based field control (field conditional state and field formatter settings), but none seem to allow a field to depend upon itself.

  • I placed a span tag with the class conceal in the Off value for the boolean field and then injected the following Javascript into the user profile page:

    jQuery("span.conceal").closest("div.field").css("display", "none");

    This works, but when the profile is displayed, the user sees a "hiccup" as the to-be-hidden field is briefly shown and then hidden.

  • I thought the field permissions module had a check for viewing a field. If you take away the user's role from viewing, what happens? – Niall Murphy Apr 2 '17 at 1:50
  • Thanks @Niall Murphy. Apparently, my question wasn't clear. I have rewritten it to make it better. I want the field to appear on the user profile page when the checkbox is selected and to not appear when it is not selected. field_permissions makes a field either always there or never there, but doesn't allow conditions based on values, as far as I know. – Marshall Morrise Apr 3 '17 at 16:32

One way to do it is in a twig template that renders the profile. Something like:

{% if user.field_fee_waived is not empty and user.field_fee_waived.value == 1 %}
  {{ content.field_fee_waived }}
{% endif %}

Basically, twig looks at the user object to see if the field is present, and that its value is 1/true. If not, it won't render the field. This same approach can work on node or paragraphs or any entity that gets passed along to a template. content.field_fee_waived is the render array that renders that specific field to your configuration you set under Manage Display.

  • Thanks @Kevin. Your answer gave me reason to read up on twig templates. Very interesting. In the end, creating a separate template to solve this problem was more than I cared to do, but now I have a solution for more complex situations. – Marshall Morrise Apr 14 '17 at 16:46

In the end, the most straightforward solution I could find was to:

  • create a duplicate user profile field named Fee waived dup.
  • use field_permissions to always hide this field from all users.
  • create a rule to set Fee waived dup equal to Fee waived when the user account is saved.
  • use the conditional_fields module to display Fee waived only when Fee waived dup is true.

If there were multiple fields to be hidden based on their own values, this might not be such a good solution. I have only one such field, so it is OK.

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.