0

When adding a block to the site:

Home » Administration » Structure » Blocks

the default setting for visibility is set to "All pages except those listed." is there a way to set the default setting to "Only the listed pages"?

I'd guess that this possible w/ a custom module but I'd rather avoid that.

1

Yes, you can add this to you admin theme's (default is Seven) template.php file (so no custom module):

function seven_form_block_add_block_form_alter(&$form, &$form_state, $form_id){
  $form['visibility']['path']['visibility']['#default_value'] = BLOCK_VISIBILITY_LISTED;
}

or if your admin theme is something else:

function ADMINTHEME_form_block_add_block_form_alter(&$form, &$form_state, $form_id){
  $form['visibility']['path']['visibility']['#default_value'] = BLOCK_VISIBILITY_LISTED;
}

Refer to hook_form_FORM_ID_alter for more info.

| improve this answer | |
  • The exact answer I needed. Also helps to reinforce the fundamental paradigm of Drupal: "There a hook/module for that". – Mike Apr 20 '16 at 17:50
1

Nope. The block add admin form is hard-coded to use BLOCK_VISIBILITY_NOTLISTED (i.e. "All pages.." option) as the default option; there's no configuration setting there. The only way around this is to form alter it with a module.

| improve this answer | |
  • hook_form_alter works in theme template files also. – J. Reynolds Apr 20 '16 at 7:21
0

You may want to use the Rules block visibility module. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless:

  • Need to show a block only for users registered more than a month ago?

  • Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

  • What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

With that, and as per the "if you can do it with Rules, you can use it to control block visibility" above, you've reduced your question to making Rules "check the path of the current page" (so that the block is only shown for the listed pages).

For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?".

| improve this answer | |

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.