You are here

function bootstrap_languages_block_view in Bootstrap Languages 7

Implements hook_block_view().

File

./bootstrap_languages.module, line 112
Provides a Bootstrap dropdown button to switch between available languages.

Code

function bootstrap_languages_block_view($type = 'language') {
  if (drupal_multilingual()) {
    $settings = variable_get('bootstrap_languages_block_settings');
    $module_path = drupal_get_path('module', 'bootstrap_languages');
    $options = array();
    $path = drupal_is_front_page() ? '<front>' : current_path();
    $languages = language_negotiation_get_switch_links($type, $path);

    // If only default language negotiation links will be empty.
    if (!isset($languages->links)) {
      return;
    }

    // Load the Languages for Bootstrap 3 library.
    $library = libraries_load('bootstrap-languages');
    if (!$library['loaded']) {
      watchdog('Bootstrap Languages', $library['error message'], NULL, WATCHDOG_ERROR);
      return;
    }

    // Check if we should support Domain access.
    if ($domain_locale_exists = module_exists('domain_locale')) {
      global $_domain;
      $domain_languages = domain_locale_lookup($_domain['domain_id']);
    }

    // Now we iterate on $languages to build needed options for dropdown.
    foreach ($languages->links as $lang_code => $lang_options) {

      // The language is not enabled on this domain.
      if ($domain_locale_exists && !array_key_exists($lang_code, $domain_languages)) {
        continue;
      }

      // There is no translation for this language and not all languages shown.
      if (!isset($lang_options['href']) && !$settings['show_all']) {
        continue;
      }

      // Build the options in an associative array (lang_code => lang_link).
      $href = isset($lang_options['href']) ? $lang_options['href'] : '<front>';
      $lang_link = url($href, array(
        'language' => $lang_options['language'],
        'query' => isset($lang_options['query']) ? $lang_options['query'] : '',
      ));
      $options[$lang_code] = $lang_link;

      // Determine selected language option.
      if (isset($lang_options['href']) && ($lang_options['href'] == current_path() || $lang_options['href'] == '<front>' && drupal_is_front_page()) && $lang_code == $GLOBALS[$type]->language) {
        $selected_option = $lang_code;
      }
    }

    // Position language switcher to the right of the navbar.
    drupal_add_css($library['library path'] . '/languages.min.css');
    drupal_add_css($module_path . '/bootstrap_languages.css');

    // Set the block subject and content.
    if (isset($languages->links)) {
      $block['subject'] = t('Languages');
      $block['content'] = theme('bootstrap_languages_switcher_block', array(
        'default_option' => isset($selected_option) ? $selected_option : reset($options),
        'btn_type' => $settings['btn_type'],
        'languages' => $options,
      ));
      return $block;
    }
  }
}