Disable active language from the language menu

Hi to everyone,

I have a multi-language website and I build a menu that display each language with this code

<?php foreach($kirby->languages() as $language): ?>
<a href="<?= $language->url() ?>">
  <?= html($language->name()) ?>
</a>
<?php endforeach ?>

Now I would like that the active language button will be disabled.
I try to do this

<a<?php e($language->isOpen(), ' class="active"') ?> href…

But it doesn’t work…
Any suggestion?

What exactly doesn’t work? Is the active class not added?

The website break down.

Schermata 2020-01-09 alle 17.41.47

Here the code

<?php foreach($kirby->languages() as $language): ?>

<a <?php e($language->isOpen(), ' class="active"') ?>href="<?= $language->url() ?>">

<?= html($language->name()) ?> <?php endforeach ?>

I’m too blind to spot the error, could you please enable debugging in your config? Doesn’t make sense to change code with debugging off… :wink:

Ah, isOpen() causes the issue…

I guess is this but I don’t know what to use…

You can filter out the current language like this:

<?php foreach($kirby->languages()->not($kirby->language()) as $language): ?>
<a href="<?= $language->url() ?>"><?= html($language->name()) ?></a>
<?php endforeach ?>

Where did you get that isOpen() code from? Is that documented anywhere?

I would like that all the languages will appear but the link button of active one became a normal text.
Filter will display only the other languages…

From the cookbook building menu

Ah, ok:

<?php foreach($kirby->languages() as $language): ?>
<a<?= $language->code() == $kirby->language()->code() ? ' class="active"'  : '' ?>href="<?= $language->url() ?>"><?= html($language->name()) ?></a>
<?php endforeach ?>

Thank you but it still give only one language… like a filter

Sorry, I copy/pasted my own code, forgetting to remove the filter… :see_no_evil: corrected above.

It works perfectly!!! Great!!! Thank you!!!

Ops no… it leaves the link on the active language and delete the link on the other language so I can’t change language

For what you want to achieve, the markup with a class on the active link doesn’t really make sense. Render a link if the language is not active, and a span element for the active language.

Maybe I make a mistake while I explain. I have two languages (French and English). When the website is in the French version: “Français” it has not work as a link and “English” yes. When I am in the English version will be the opposite.

Yes, I get that, but the code adds the active link to the current active language. I don’t know what you are doing with your active class. You would have to disable pointer events etc. like explained here:

The other alternative is to use an if statement and render a link when the language is not active and just a span with text in the opposite case.

Thank you