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
| Comment | File | Size | Author |
|---|
Issue fork dropdown_language-3270979
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
Comment #2
skaughtcan you describe the bug this fixes?
Comment #3
ruuds commented$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.
Comment #4
grevil commentedComment #5
grevil commentedComment #6
grevil commentedThe fix by @Ruuds is a bit overcomplicated, but should do the trick as well, I'll provide a simplified approach.
Comment #8
grevil commented@SKAUGHT, please review the MR provided!
Comment #9
anybodyDue to the line below,
$linksshould be an empty array, not NULL or that has to be checked in a better way.Comment #10
grevil commentedDone!
Comment #11
anybodyFine! :)
Comment #12
skaught@todo
this effects drupal 9.4+. [#3247792]
Comment #13
anybodyAny plan to fix & commit this soon @SKAUGHT?
Comment #14
skaught@anybody
have released 3.1 today. thanks for you help
Comment #15
skaughtComment #16
skaughtComment #17
anybodyThank 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.
Comment #18
skaughtpasted 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!
Comment #19
skaughttest against new release 3.1.0 (dec/22)
Comment #20
grevil commentedOK.
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([...])->linksgot 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 andNULL->linkwill throw the error once again.Comment #21
grevil commentedRebased everything.
Comment #22
anybodyThanks @Grevil LGTM but I think we should use that chance to clean up the variables in that scope. The line is veryyyyyy long....
Comment #23
skaught[removed] sorry, wrong time to peek-in..
Comment #24
grevil commentedDone.
Comment #25
anybodyThat looks a lot cleaner, thank you @Grevil! RTBC!
Comment #26
skaughtComment #27
anybodyComment #28
eric_a commentedLooking 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. :-)
Comment #29
eric_a commentedThe title of this issue really is outdated, though. And not helpful. :-)
Comment #30
anybodyThanks @Eric_A. I agree and will do so! Just became maintainer. Let's fix all these issues around the NULL ;)
Comment #32
anybodyFixed. I'll tag a 3.1.3 release now to get the WSODs away first!
Comment #33
anybody@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).
Comment #35
anybodyMeanwhile cherry-picked into 4.0.x!
Fixing the title according to #2940121: ConfigurableLanguageManager::getLanguageSwitchLinks() doesn't respect what LanguageManagerInterface::getLanguageSwitchLinks() says to return
Comment #36
skaughtComment #37
anybodyPlease see #3349246: Affected by core regression issue on no route match? for a follow-up on the regression in core pointed out in #3348592: [regression] Language switcher block throws exception when no route is matched