1

I'm using Language Icons and Language Switcher modules. The flag icons are added to the language switcher block, but the icons are stored in sites/all/modules/languageicons/flags. They are of fixed height of 12 pixels by default and they are used by every theme.

I want to use different sets of flag icons with different dimensions per theme, for example, Theme 1 will use the default icons of 12x12 pixels, but Theme 2 will use the icons of 19x19 pixels. I will have my custom flag icons in my custom module or in my custom themes.

I would do this by using hook_block_view_alter() to override the block output per theme, but I'm looking for a better and right way.

1

As mentioned in my question, I could not do this by using hook_block_view_alter() because I can only have the renderable HTML of the block content in that hook.

I found a theme function theme_languageicons_icon, which renders the icons, in the file sites/all/modules/ languageicons/languageicons.module. I copied the theme function into my custom module and implemented hook_theme_registry_alter() to override it. Then, I updated the function as my need and flushed caches. It gave me exactly what I needed.

function MYMODULE_theme_registry_alter(&$theme_registry){
    if( isset($theme_registry['languageicons_icon']) ){
        $theme_registry['languageicons_icon']['function'] = 'theme_mymodule_languageicons_icon';
    }
}

function theme_mymodule_languageicons_icon($variables) {
  global $theme;
  $language = $variables['language'];
  $title    = $variables['title'];
  # define icons path for various themes
  $default_path = variable_get('languageicons_path', drupal_get_path('module', 'languageicons') . '/flags/*.png');
  $path_by_theme = array(
    'theme1' => drupal_get_path('theme', 'theme1') . '/flags/*.png'
    'theme2' => drupal_get_path('theme', 'theme2') . '/flags/*.png'
  );
  $path = (isset($path_by_theme[$theme]) && file_exists($path_by_theme[$theme])) ? $path_by_theme[$theme] : $default_path;
  # define dimension for various themes
  $default_size = check_plain(variable_get('languageicons_size', '16x12'));
  $size_by_theme = array(
    'theme1' => '19x19',
    'theme2' => '25x24',
  );
  $size = (isset($size_by_theme[$theme])) ? $size_by_theme[$theme] : $default_size;

  if ($path) {
    $title = $title ? $title : $language->native;
    // Build up $image for theme_image() consumption.
    $image = array(
      'path' => str_replace('*', $language->language, check_plain($path)),
      'alt' => $title,
      'title' => $title,
      'attributes' => array(
        'class' => 'language-icon',
      ),
    );
    if ($size) {
      list($width, $height) = explode('x', $size);
      $image += array('width' => $width, 'height' => $height);
    }
    return theme('image', $image);
  }
}

If you have a better solution, it is welcome.

1
  • nice one, thanks (I just need to change the flag size, but this has it all) Aug 28 '13 at 23:39

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.