You are here

function block_country_form_alter in Block Country 7

Implements hook_form_alter().

Adds Country specific visibility options to block configuration form.

See also

block_admin_configure()

File

./block_country.module, line 67
Country_Block - Module for showing the country specific blocks.

Code

function block_country_form_alter(&$form, &$form_state, $form_id) {

  // Check if user have access to configure block country.
  if (user_access('administer block country') && ($form_id == 'block_admin_configure' || $form_id == 'block_add_block_form')) {

    // Get country list for current block.
    $selected_country_query = db_select('block_country', 'bc')
      ->fields('bc', array(
      'country_code',
    ))
      ->condition('bc.delta', $form['delta']['#value'])
      ->condition('bc.module', $form['module']['#value']);
    $selected_country_list = $selected_country_query
      ->execute()
      ->fetchAllKeyed(0, 0);
    $block_visibility_country = TRUE;
    if ($form_id == 'block_admin_configure') {

      // Load statically cached block object.
      $block = block_load($form['module']['#value'], $form['delta']['#value']);
      $block_visibility_country = !empty($block->country_visiblility) ? TRUE : FALSE;
    }
    $form['visibility']['block_country'] = array(
      '#type' => 'fieldset',
      '#title' => t('Country'),
      '#weight' => 31,
      '#collapsible' => TRUE,
      '#group' => 'visibility',
      '#attached' => array(
        'js' => array(
          drupal_get_path('module', 'block_country') . '/block_country.js',
        ),
      ),
    );

    // Block visibility.
    $form['visibility']['block_country']['block_visibility_country'] = array(
      '#title' => t('Visible on all country'),
      '#type' => 'checkbox',
      '#description' => t('If checked block will display in all countries. For displaying block in specific country uncheck it.'),
      '#default_value' => $block_visibility_country,
      '#weight' => 1,
    );

    // Country Option list.
    $form['visibility']['block_country']['country_code'] = array(
      '#type' => 'select',
      '#title' => t('Select Country'),
      '#multiple' => TRUE,
      '#options' => country_get_list(),
      '#default_value' => $selected_country_list,
      '#description' => t('Block will be visible in selected countries'),
      '#weight' => 2,
      '#states' => array(
        'visible' => array(
          ':input[name="block_visibility_country"]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
    );

    // Custom validate to make country list mandatory,
    // if "Visible on all Country" checkbox is unchecked.
    $form['#validate'][] = 'block_country_form_validate';

    // Custom block admin configuration form submit.
    $form['#submit'][] = 'block_country_form_block_admin_configure_submit';
  }
}