Multilanguage issues

Hi, the code below worked as expected in earlier Kirby versions

<html<?php e($kirby->languages()->isNotEmpty(), ' lang="' . $kirby->language()->code() . '" dir="' . $kirby->language()->direction() . '" prefix="og: https://ogp.me/ns#"') ?>>

But not is dropping Call to a member function code() on null when there are no languages.

I saw there were some changes in 3.9.6 update for languages, but can’t figure out how to change code now to accomplish what I need.

What’s changed?

It shouldn’t have worked, though. This is the sort of logic that should never be used with the e() helper function. Since e() is a function and you pass it some parameters (2 in this case), these parameters are all evaluated, not depending on the condition. So each parameter you pass to this function must evaluate by itself:

Param 1: $kirby->languages()->isNotEmpty(): this is fine
Param 2: ' lang="' . $kirby->language()->code() . '" dir="' . $kirby->language()->direction() . '" prefix="og: https://ogp.me/ns#"': this will always fail if there is no language object.

So for such examples use:

<html<?= $kirby->languages()->isNotEmpty() ? ' lang="' . $kirby->language()->code() . '" dir="' . $kirby->language()->direction() . '" prefix="og: https://ogp.me/ns#"' : '' ?>>

instead.

If you want to still use the helper, you would have to use the null-safe operator:

<html<?php e($kirby->languages()->isNotEmpty(), ' lang="' . $kirby->language()?->code() . '" dir="' . $kirby->language()?->direction() . '" prefix="og: https://ogp.me/ns#"') ?>>
1 Like

Oh, dear, Sonja is to the rescue, as usual :pray:

Yes, it works now. I think I know when it worked, in PHP 7.4, but I didn’t test that scenario since.

Thank you very much!