Problem/Motivation

There is a wrong usage of the null coalescing operator in Line 147 of "src/Plugin/Block/DropdownLanguage.php":
$links = $this->languageManager->getLanguageSwitchLinks($derivative_id, Url::fromRoute($route)) ?? $this->languageManager->getLanguageSwitchLinks($derivative_id, Url::fromRoute($route))->links;

If "getLanguageSwitchLinks()" returns an object, the left-hand side of the argument is true and therefore returns the left handside of the argument as the value for $links.

But in further lines "$links" is treated as an array, which leads to the following error:

Error: Cannot use object of type stdClass as array in Drupal\dropdown_language\Plugin\Block\DropdownLanguage->build() (line 192 of modules/contrib/dropdown_language/src/Plugin/Block/DropdownLanguage.php).

Steps to reproduce

Proposed resolution

Instead, the Elvis Operator should be used here.

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Ruuds created an issue. See original summary.

skaught’s picture

can you describe the bug this fixes?

ruuds’s picture

$this->languageManager->getLanguageSwitchLinks doesn't return an array but an object in more recent drupal core versions. The is_object check handles this case for me.

I came across the issue when updating a website from Drupal 8 to 9.

grevil’s picture

Title: LanguageManagerInterface::getLanguageSwitchLinks() now returns an object, or NULL if there are no links » Wrong usage of null coealescing operator in DropdownLanguage.php
Issue summary: View changes
grevil’s picture

Issue summary: View changes
grevil’s picture

Status: Needs review » Needs work

The fix by @Ruuds is a bit overcomplicated, but should do the trick as well, I'll provide a simplified approach.

grevil’s picture

Status: Needs work » Needs review

@SKAUGHT, please review the MR provided!

anybody’s picture

Status: Needs review » Needs work

Due to the line below, $links should be an empty array, not NULL or that has to be checked in a better way.

grevil’s picture

Status: Needs work » Needs review

Done!

anybody’s picture

Status: Needs review » Reviewed & tested by the community

Fine! :)

skaught’s picture

@todo
this effects drupal 9.4+. [#3247792]

anybody’s picture

Any plan to fix & commit this soon @SKAUGHT?

skaught’s picture

@anybody
have released 3.1 today. thanks for you help

skaught’s picture

skaught’s picture

anybody’s picture

Thank you @SKAUGHT, is this FIXED then?
If possible, you should reference the issue IDs in the commits (see below textarea) so we can see the commit in the issues.

skaught’s picture

pasted from #3262334:
Deprecated function: Automatic conversion of false to array is deprecated in Drupal\dropdown_language\Plugin\Block\DropdownLanguage->build()

#3262334: Deprecated function: Automatic conversion of false to array is deprecated in Drupal\dropdown_language\Plugin\Block\DropdownLanguage->build() as been committed now and as far as i can tell does seem to handle this same error. Could you please test against you own project to verify if this works for your situation.

----
for my releases for this project, i do indeed my best to track progress
https://www.drupal.org/project/dropdown_language/releases/3.1.0

this is why i have not yet otherwise closed THIS ISSUE. (: thanks!

skaught’s picture

Status: Reviewed & tested by the community » Needs review

test against new release 3.1.0 (dec/22)

grevil’s picture

Status: Needs review » Active

OK.

So, issue #3262334: Deprecated function: Automatic conversion of false to array is deprecated in Drupal\dropdown_language\Plugin\Block\DropdownLanguage->build() doesn't actually change any code relevant to this issue. BUT the line of code, relevant to this issue, DID actually change: The fallback for getLanguageSwitchLinks([...])->links got completely removed!

After checking where this change came from, I found this issue here: #3255573: Warning: Attempt to read property "links" on null, where that issues implementation (on which this issue resides on) got removed.

This isn't good, as calling $this->languageManager->getLanguageSwitchLinks() can return NULL and NULL->link will throw the error once again.

grevil’s picture

Status: Active » Needs review

Rebased everything.

anybody’s picture

Status: Needs review » Needs work

Thanks @Grevil LGTM but I think we should use that chance to clean up the variables in that scope. The line is veryyyyyy long....

skaught’s picture

[removed] sorry, wrong time to peek-in..

grevil’s picture

Status: Needs work » Needs review

Done.

anybody’s picture

Status: Needs review » Reviewed & tested by the community

That looks a lot cleaner, thank you @Grevil! RTBC!

skaught’s picture

Assigned: Unassigned » skaught
anybody’s picture

eric_a’s picture

Looking at https://git.drupalcode.org/project/dropdown_language/-/merge_requests/3/... I see the same fix as the one I just provided in #3255573: Warning: Attempt to read property "links" on null(https://www.drupal.org/files/issues/2023-03-16/attempt-to-read-property-...)

The only difference is the local variable name: my $language_switch_links vs $languageSwitchLinksObject.
Up to the maintainer which version he wants. I'd say pick one and credit us both. :-)

eric_a’s picture

The title of this issue really is outdated, though. And not helpful. :-)

anybody’s picture

Assigned: skaught » Unassigned

Thanks @Eric_A. I agree and will do so! Just became maintainer. Let's fix all these issues around the NULL ;)

  • Anybody committed 95a1e77d on 3.0.x authored by Grevil
    Issue #3270979 by Grevil, Anybody, Ruuds: Wrong usage of null...
anybody’s picture

Status: Reviewed & tested by the community » Fixed

Fixed. I'll tag a 3.1.3 release now to get the WSODs away first!

anybody’s picture

@SKAUGHT, I'm missing the permission to administer releases. So I can't release 3.1.1 - could you add that permission please? (also sent you a PM).

  • Anybody committed 9960ebc2 on 4.0.x
    Issue #3270979 by Grevil, Anybody, Ruuds: Wrong usage of null...
anybody’s picture

Title: Wrong usage of null coealescing operator in DropdownLanguage.php » Incorporate Drupal 9.4 core changes to getLanguageSwitchLinks
Related issues: +#2940121: ConfigurableLanguageManager::getLanguageSwitchLinks() doesn't respect what LanguageManagerInterface::getLanguageSwitchLinks() says to return
skaught’s picture

Issue tags: +3.2.x
anybody’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.