You are here

bootstrap_languages.module in Bootstrap Languages 7

Same filename and directory in other branches
  1. 8 bootstrap_languages.module

Provides a Bootstrap dropdown button to switch between available languages.

File

bootstrap_languages.module
View source
<?php

/**
 * @file
 * Provides a Bootstrap dropdown button to switch between available languages.
 */

/**
 * Implements hook_libraries_info().
 */
function bootstrap_languages_libraries_info() {
  $libraries['bootstrap-languages'] = array(
    'name' => 'Languages for Bootstrap 3',
    'vendor url' => 'http://usrz.github.io/bootstrap-languages/',
    'download url' => 'https://github.com/usrz/bootstrap-languages/archive/master.zip',
    'version' => '2.0',
    'files' => array(
      'css' => array(
        'languages.min.css',
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_theme().
 */
function bootstrap_languages_theme($existing, $type, $theme, $path) {
  $templates_path = drupal_get_path('module', 'bootstrap_languages') . '/templates';
  return array(
    'bootstrap_languages_switcher_block' => array(
      'template' => 'bootstrap-languages-switcher-block',
      'variables' => array(
        'default_option' => NULL,
        'btn_type' => NULL,
        'languages' => NULL,
      ),
      'path' => $templates_path,
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function bootstrap_languages_block_info() {
  include_once DRUPAL_ROOT . '/includes/language.inc';
  $block = array();
  $info = language_types_info();
  foreach (language_types_configurable(FALSE) as $type) {
    $block[$type] = array(
      'info' => t('Bootstrap Languages (@type)', array(
        '@type' => $info[$type]['name'],
      )),
      'cache' => DRUPAL_NO_CACHE,
    );
  }
  return $block;
}

/**
 * Implements hook_block_configure().
 */
function bootstrap_languages_block_configure($delta = '') {
  $settings = variable_get('bootstrap_languages_block_settings');
  $form = array();

  // Check if the Languages for Bootstrap 3 library is present.
  $library = libraries_load('bootstrap-languages');
  if ($library['loaded'] == FALSE) {
    drupal_set_message($library['error message'], 'error');
  }
  $form['bootstrap_languages'] = array(
    '#type' => 'fieldset',
    '#title' => t('Bootstrap Languages settings'),
    '#weight' => 0,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['bootstrap_languages']['show_all'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show all enabled languages'),
    '#description' => t('Show all languages in the switcher no matter if there is a translation for the node or not. For languages without translation the switcher will redirect to homepage.'),
    '#default_value' => $settings['show_all'],
  );
  $form['bootstrap_languages']['btn_type'] = array(
    '#type' => 'select',
    '#title' => t('Bootstrap button type'),
    '#options' => array(
      'btn-default' => t('Default'),
      'btn-primary' => t('Primary'),
      'btn-info' => t('Info'),
    ),
    '#default_value' => $settings['btn_type'],
  );
  return $form;
}

/**
 * Implements hook_block_save().
 */
function bootstrap_languages_block_save($delta = '', $edit = array()) {
  $bootstrap_languages_settings = array(
    'show_all' => $edit['show_all'],
    'btn_type' => $edit['btn_type'],
  );
  variable_set('bootstrap_languages_block_settings', $bootstrap_languages_settings);
}

/**
 * Implements hook_block_view().
 */
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;
    }
  }
}