Created
February 17, 2012 13:40
-
-
Save anthonyringoet/1853495 to your computer and use it in GitHub Desktop.
Drupal language block with only prefixes + modification for no result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Override the output of the default locale language switch links. | |
* We only want prefixes. If no link available, go to frontpage. Better than nothing. | |
*/ | |
function theme_links__locale_block(&$variables) { | |
$links = $variables['links']; | |
$attributes = $variables['attributes']; | |
$heading = $variables['heading']; | |
global $language_url; | |
$output = ''; | |
if (count($links) > 0) { | |
$output = ''; | |
// Treat the heading first if it is present to prepend it to the | |
// list of links. | |
if (!empty($heading)) { | |
if (is_string($heading)) { | |
// Prepare the array that will be used when the passed heading | |
// is a string. | |
$heading = array( | |
'text' => $heading, | |
// Set the default level of the heading. | |
'level' => 'h2', | |
); | |
} | |
$output .= '<' . $heading['level']; | |
if (!empty($heading['class'])) { | |
$output .= drupal_attributes(array('class' => $heading['class'])); | |
} | |
$output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>'; | |
} | |
$output .= '<ul' . drupal_attributes($attributes) . '>'; | |
$num_links = count($links); | |
$i = 1; | |
foreach ($links as $key => $link) { | |
$class = array($key); | |
// Add first, last and active classes to the list of links to help out themers. | |
if ($i == 1) { | |
$class[] = 'first'; | |
} | |
if ($i == $num_links) { | |
$class[] = 'last'; | |
} | |
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page())) | |
&& (empty($link['language']) || $link['language']->language == $language_url->language)) { | |
$class[] = 'active'; | |
} | |
$output .= '<li' . drupal_attributes(array('class' => $class)) . '>'; | |
if (isset($link['href'])) { | |
// Pass in $link as $options, they share the same keys | |
$output .= l($link['language']->language, $link['href'], $link); | |
} | |
/* | |
* Mod. here because if no translated alternative, we still want a lang link. | |
* Alternative here is that it directs you to the homepage in the req. language. | |
*/ | |
elseif(!empty($link['language']->language)){ | |
$link['href'] = ''; | |
$output .= l($link['language']->language, $link['href'], $link); | |
} | |
$i++; | |
$output .= "</li>\n"; | |
} | |
$output .= '</ul>'; | |
} | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment