0

I have searched and searched for an answer to this, so hopefully I'm not asking something that is answered elsewhere.

I have a content type with a boolean field that show or hide a form. On each of that content type node pages, I want to show a block if the boolean field is set to true.

So, in essence, the boolean on the content type tells whether or not to show the block.

Right now I have the form (via the Webform Module) displaying as a block using Views. I think I can set a contextual filter to determine if the block is shown or not, but I am having a ton of trouble getting it to do that.

So, how do I set up contextual filters to show the block if a boolean field is set in the content type to true and not if it's false?

2 Answers 2

1

Forget views.

Go to the visibility settings for the block (under /admin/structure/block) and set the Page Visibility to only show on "Pages on which this PHP code returns TRUE".

Then check your boolean field with the following code:

$node  = menu_get_object();

return (isset($node->field_boolean[LANGUAGE_NONE][0]['value'])) ? $node->field_boolean[LANGUAGE_NONE][0]['value'] : FALSE;

Then place the block on the page where you want it.

3
  • Okay. Maybe this is a dumb question, but are there security implications to using PHP like this instead of something built into the system?
    – Rewdy
    Commented Mar 8, 2013 at 1:59
  • No, as long as you restrict the use to the administrator role. The whole system is php-based. All you are doing is expanding the use to a block that you are administering. Perfectly safe. If you haven't, already done it, enable the php filter module under core, and set the administrator role to 'Use the PHP code text format".
    – Triskelion
    Commented Mar 8, 2013 at 2:10
  • Welcome. Glad to help.
    – Triskelion
    Commented Mar 8, 2013 at 5:04
0

This over kill for your particular use case, but the Panels module offers one way to do this.

With Panels, you can override the display of all nodes. On this overridden display, you can then add the block with a visibility condition. You can set the visibility condition to only show if a block has a certain content type. In this case, you don't need the boolean field on the content type (although if you really wanted to use that for some reason, you could instead set the visibility rule to work based on that field instead of based on the content type).

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.