works I have added a new field to block configuration settings by using the function hook_form_FORM_ID_alter. I used submit handler to save the variable $form['#submit'][] = mymodule_submit (as block_save function was not called). Its all done till here

Now I am trying to alter the block list based on the condition set in the config field enable_conectivity

function mymodule_block_list_alter(&$blocks) {

    foreach ($blocks as $key => $block) {
       $enable_conectivity =??//How to get the varibale value
        if($enable_conectivity) {
            $blocks[$key]->visibility = BLOCK_CUSTOM_DISABLED;

I tried to call function hook_block_view it is not called instead hook_block_view_alter works.

Issue:Saving is affecting all the blocks

function mymodule_submit($form, &$form_state) {
     variable_set('url_to_check', $form_state['values']['url_to_check']);
     variable_set('enable_check', $form_state['values']['enable_check']);


$form['visibility']['mycustom'] = array(
  '#type' => 'fieldset',
  '#title' => t('ConnectCheck'),
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,
  '#group' => 'visibility',
 '#weight' => 20,
$form['visibility']['mycustom']['enable_check'] = array(
  '#type' => 'checkbox',
  '#title' => t('Show block only when user can connect to internet'),
  '#default_value' => variable_get('enable_check', FALSE),
$form['visibility']['mycustom']['url_to_check'] = array(
   '#type' => 'textfield',
   '#title' => t('Url to check'),
   '#maxlength' => 90,
   '#default_value' => variable_get('url_to_check', 'www.google.com'),
   '#states' => array(
     'visible' => array(
        'input[name="enable_check"]' => array('checked' => TRUE),

I am trying to disable the block which requires to connect internet. It should disable to only those user who do not have internet access.

  • unset the block in hook_block_list_alter rather than setting visibility – troseman Mar 23 '17 at 0:01

Assuming you've set the variable using the variable_set() function, you can get the value using the variable_get() function.

From the docs:

Returns a persistent variable.

Case-sensitivity of the variable_* functions depends on the database collation used. To avoid problems, always use lower case for persistent variable names.

In your case the foreach loop would become :

foreach ($blocks as $key => $block) {
  $enable_conectivity = variable_get('enable_conectivity', TRUE); // Default value of `TRUE` is returned if the variable is not set.
  if($enable_conectivity) {
    $blocks[$key]->visibility = BLOCK_CUSTOM_DISABLED;
| improve this answer | |
  • Thanks Ajit. I have one more problem saving the variables affect all the blocks not just one. Why? – B L Praveen Jan 13 '15 at 7:37
  • $blocks[$key]->visibility = BLOCK_CUSTOM_DISABLED is not disabling the block – B L Praveen Jan 13 '15 at 7: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.