Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By no2e on
I use translation, locale and i18n. I want to change the language switcher block (id="block-locale-language"
).
The language links look like this at default:
<div class="content">
<ul class="language-switcher-locale-url">
<li class="en first active"><a href="/en" class="language-link active">English</a></li>
<li class="fr"><a href="/fr" class="language-link">Français</a></li>
<li class="de last"><a href="/de" class="language-link">Deutsch</a></li>
</ul>
</div>
I want to change the links to this (example for the French link):
<li class="fr last"><a href="/fr" class="language-link"><abbr title="Français">fr</abbr></a></li>
(I added a <abbr>
element; the language's label is the title
value, the language shortcode is the content of the abbr
)
I have no idea how to accomplish that (never worked with coding/theming much).
I can overwrite the block with block--locale.tpl.php
, but how do I access the HTML markup of the links?
Comments
=-=
may be worth checking the module to see if there is a theme function
copy it to template.php of your theme
override it and change what you need to change
may also want to investigate preprocess functions
Sorry, I have no experience
Sorry, I have no experience with this topic …
Hm, how can I spot theme functions? And in which files (inside of the module's folder) should I look for it?
=-=
theme functions start with the word theme
I suggest the devel.module and learning how to work with it and the docs for themeing http://drupal.org/theme-guide/6-7
There is a section in the above link which discusses overrding/modifying theme output.
In template.php
It's a bit late, but it may be useful for others.
In your theme template.php, add this function and then clear the cache. (replace yourtheme with the name of your theme)
David Mignot
www.idflood.com
gittip
This is quite good, but I
This is quite good, but I think that is better to set the title of the abbr to the native name of the language:
Full customized list
Great job! but with theme_links() there is no possibility to theme li elements. If someone will need to custom all list elements (ul, li, links) then I suggest to use theme_item_list() function:
Just a small addition
Thanks Jack-pl that put me 99% there, just was not getting an active state for the current language, so added that in in the version below:
Changes:
Yes, you're right. Don't know
Yes, you're right. Don't know how I forgot about it. Now the example is complete. Thanks.
Remove <div> wrapper?
Hi all,
?
this snippet works like a charm but: there is a way to remove the theme_item_list() wrapper
I think I have some problems rewriting theme_item_list()
Thank you for possible solutions :)
I've got two
I've got two solutions:
Solution #1:
You can copy theme_item_list() and place into the template.php file (replace 'theme' for your_theme_name) but in this way you will remove the div wherever theme_item_list() will be called.
Solution #2:
Create a module and put the language switcher stuff there.
Module files:
file: custom_language_switcher.info
file: custom_language_switcher.module
Remove <?php
Only to notice that "custom_language_switcher.info" goes without PHP tags.
Doesn't work anymore
Hi, it seems that this doesn't work anymore, as the language isn't any more in the $info! So $info['language'] can't be used. As for just displaying the language name, I can use the 'title', but for generating the link with l(), I have to have the language in order for it to work. So if anyone can bring this solution up-to-date.
The following makes the title
The following makes the title of the language into capital and the first two letters, can be changed accordingly.
It needs to be placed in a custom module
Does somebody have a snippet
Does somebody have a snippet how to get it done in Drupal 8?
Just changing the name of the language is strangely not working here.
I can't seem to find a snippet anywhere :-).
In case anybody is looking
In case anybody is looking for a snippet:
Create a module called "language_switcher_alter"
Implement the following code in your language_switcher_alter.module file.
Or this:
Or this:
Thanks for the D8 examples ..
Thanks for the D8 examples ...
Theming the language switcher markup in Drupal8
After a while of D8-naive digging around I've found this line:
core/modules/language/src/Plugin/Block/LanguageBlock.php: '#theme' => 'links__language_block',
So just copy the
core/modules/system/templates/links.html.twig
into your theme's templates directory, call it
links--language-block.html.twig
and adjust its guts as needed. Twig!
Using twig debugging to find theme suggestions
NOTE: Next time I need to find where something is coming from I'll definitely use the twig debugging feature. Just set
parameters: twig.config: debug: true
in your
services.yml
(the new sibbling ofsettings.php
)and you'll get a stream of goodness like this
in a form of comments in your html source.
There used to be that useful Theme developer module. Just something that displays these twig debug comments painlessly ? Does it already exist ?