languageicons.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @file
  4. * Icons for language links.
  5. *
  6. * This is a spin off from the Internationalization (i18n) package.
  7. */
  8. /**
  9. * Implements hook_theme().
  10. */
  11. function languageicons_theme() {
  12. return array(
  13. 'languageicons_icon' => array(
  14. 'variables' => array('language' => NULL, 'title' => NULL),
  15. ),
  16. 'languageicons_place' => array(
  17. 'variables' => array('text' => NULL, 'icon' => NULL, 'separator' => ' '),
  18. ),
  19. );
  20. }
  21. /**
  22. * Implements hook_help().
  23. *
  24. * @todo The @handbook link needs to change to a module specific one.
  25. */
  26. function languageicons_help($path, $arg) {
  27. switch ($path) {
  28. case 'admin/help#languageicons':
  29. $output = '<p>' . t('This module manages language icons for multilingual sites:') . '</p>';
  30. $output .= '<ul>';
  31. $output .= '<li>' . t('Automatically adds icons to language links.') . '</li>';
  32. $output .= '<li>' . t('Provides related theme functions.') . '</li>';
  33. $output .= '</ul>';
  34. $output .= '<p>' . t('For more information, please see <a href="@handbook">the online handbook section</a>.', array('@handbook' => 'http://drupal.org/node/133977')) . '</p>';
  35. return $output;
  36. case 'admin/config/regional/language/icons':
  37. $output = '<p>' . t('To enable multilingual support for specific content types go to <a href="@configure_content_types">configure content types</a>.', array('@configure_content_types' => url('admin/structure/types'))) . '</p>';
  38. return $output;
  39. }
  40. }
  41. /**
  42. * Implements hook_menu().
  43. */
  44. function languageicons_menu() {
  45. $items['admin/config/regional/language/icons'] = array(
  46. 'title' => 'Icons',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('languageicons_admin_settings'),
  49. 'weight' => 10,
  50. 'type' => MENU_LOCAL_TASK,
  51. 'access arguments' => array('administer languages'),
  52. 'file' => 'languageicons.admin.inc',
  53. );
  54. return $items;
  55. }
  56. /**
  57. * Implements hook_language_switch_links_alter().
  58. *
  59. * Adds language icons to language switcher block links.
  60. *
  61. * @todo Figure out a way to either ignore node links or specifically target
  62. * them here. See http://drupal.org/node/1005144 for more info.
  63. */
  64. function languageicons_language_switch_links_alter(array &$links, $type, $path) {
  65. if (variable_get('languageicons_show_block', 1) || variable_get('languageicons_show_node', 1)) {
  66. foreach (array_keys($links) as $langcode) {
  67. if (!isset($links[$langcode]['language'])) {
  68. $lang_obj['language'] = $langcode;
  69. $links[$langcode]['language'] = (object) $lang_obj;
  70. }
  71. languageicons_link_add($links[$langcode]);
  72. }
  73. }
  74. }
  75. /**
  76. * Add language icon to link.
  77. *
  78. * The language icon may be a different language as the destination page, can be passed in 'language_icon'.
  79. */
  80. function languageicons_link_add(&$link, $title = NULL) {
  81. $language = isset($link['language_icon']) ? $link['language_icon'] : $link['language'];
  82. $title = $title ? $title : $link['title'];
  83. $icon = theme('languageicons_icon', array(
  84. 'language' => $language,
  85. 'title' => check_plain($title),
  86. ));
  87. if ($icon) {
  88. $link['title'] = theme('languageicons_place', array(
  89. 'text' => check_plain($link['title']),
  90. 'icon' => $icon,
  91. ));
  92. $link['html'] = TRUE;
  93. }
  94. }
  95. /**
  96. * Theme language icon.
  97. *
  98. * This function can be overridden for no language icons.
  99. *
  100. * @seealso theme_image()
  101. */
  102. function theme_languageicons_icon($variables) {
  103. $language = $variables['language'];
  104. $title = $variables['title'];
  105. if ($path = variable_get('languageicons_path', drupal_get_path('module', 'languageicons') . '/flags/*.png')) {
  106. $title = $title ? $title : $language->native;
  107. // Build up $image for theme_image() consumption.
  108. $image = array(
  109. 'path' => str_replace('*', $language->language, check_plain($path)),
  110. 'alt' => $title,
  111. 'title' => $title,
  112. 'attributes' => array(
  113. 'class' => array('language-icon'),
  114. ),
  115. );
  116. if ($size = check_plain(variable_get('languageicons_size', '16x12'))) {
  117. list($width, $height) = explode('x', $size);
  118. $image += array('width' => $width, 'height' => $height);
  119. }
  120. return theme('image', $image);
  121. }
  122. }
  123. /**
  124. * Theme language icon and text.
  125. *
  126. * @ingroup themeable
  127. */
  128. function theme_languageicons_place($variables) {
  129. $text = $variables['text'];
  130. $icon = $variables['icon'];
  131. $separator = $variables['separator'];
  132. switch (variable_get('languageicons_placement', 'before')) {
  133. case 'after':
  134. return $text . $separator . $icon;
  135. case 'replace':
  136. return $icon;
  137. case 'before':
  138. default:
  139. return $icon . $separator . $text;
  140. }
  141. }