1

I have a content type with a boolean field (checkbox). Basically, what I'm trying to achieve is this:

  • If this field is checked, only show the label (when viewing the page)
  • If this field is unchecked, don't show the field at all (no label and no value)

Is there a simple way of doing this? (Without resorting to Views or custom code)

I've noticed that if I use "Check boxes/radio buttons" as widget type for this field, it comes close to what I want, since it offers three values to choose from (yes, no, and N/A), and if N/A is selected, the field isn't rendered on the display page.

However:

  • I don't want 3 values to choose, but 2. (3 might be confusing for users, and having to explain "Don't use the 'no' value" really isn't a good solution.)
  • I don't want the value to be displayed when "Yes" is selected (only the label).
  • 1
    "Without resorting to Views or custom code" - looks like a pretty harsh requirements. Almost all of templating is custom code. – Mołot Jan 8 '14 at 13:27
  • The answer is a simple "no", there's no way to do that without 'resorting' to Views or custom code. Well, you might find a module that happens to provide that tiny bit of functionality as well as whatever else it does, but I doubt it – Clive Jan 8 '14 at 13:32
  • OK, thanks for your comments. I guess I'll use Views, then (I already use it anyway). – s427 Jan 8 '14 at 15:39
  • @Clive in case you're interested, it's actually possible using just core functionality. See my answer for more details. – s427 Feb 21 '14 at 17:29
  • @s427 I stand corrected :) – Clive Feb 21 '14 at 18:03
6

OK, so it is possible and actually rather simple. Here's how I did it in the end:

Edit the field's settings:
"value if checked" -> use the text you want to see displayed when checked
"value if unchecked" -> leave empty

And then under Content type > Manage Display, hide the label for this field.

This way, only the value of this field will be shown.
Since the value when unchecked is empty, nothing will be shown when the field is not checked.

  • 1
    You still get the empty divs which can be disturbing for theming. I had to use hook_field_attach_view_alter and set #access to FALSE to get rid of the field-items / field-item divs – Hans Rossel Mar 17 '18 at 10:40
1

If I understand you correctly, I think you just need to use a Boolean field and configure it as a Single On/Off checkbox. Then in the field settings, choose Use field label instead of the "On value" as label. I suspect the last part is the missing piece in your puzzle.

Otherwise, you may need 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.

  • I did play with the Use field label instead of the "On value" as label setting, but as far as I can tell, it only applies to how the field is displayed when editing the content. It doesn't change the fact that the value is displayed when viewing the page, regardless of its state. I guess I'll just use Views. – s427 Jan 8 '14 at 15:37
  • @s427 I would play with it some more. I was looking at a site I had worked on where I did something similar. Unfortunately, the client is reviewing it, so I can't mess with the settings. – mpdonadio Jan 8 '14 at 15:48
  • I'll look again, but frankly I think I've already tried all combinations. – s427 Jan 8 '14 at 17:51
1

I am trying to solve a very similar problem. While learning how to convert a legacy system to Drupal, I realized I had several tables in which a boolean field determines whether to hide or show several other fields. In addition to whether or not those other fields get shown, under no circumstances would I need to actually show either the label or the value of this actual boolean field.

After reading the other comments and suggestions and wrestling unsuccessfully with Conditional Fields, I found that the piece I was missing was Display Suite, which seems to give you a lot more control over the placement, hiding, and arrangement of labels and fields. Since my particular use case is a little bit different from yours, I am not sure this will help you -- did you already try Display Suite (in concert with Views, of course)?

  • Thanks for the suggestion. No I didn't know this module. However I think I can do what I need using just Views (if not, I'll check out Display Suite). My question was whether or not it was achievable using Drupal core modules only. Apparently it's not. – s427 Jan 10 '14 at 9:40

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.