I got this error on installing a custom module, that includes a custom product variation type:

PHP Fatal error:  Call to a member function getId() on null in /var/www/drupalv/drupal/modules/commerce/src/Resolver/DefaultLocaleResolver.php on line 45
Drush command terminated abnormally due to an unrecoverable error.   [error]
Error: Call to a member function getId() on null in
/var/www/drupalvm/drupal/modules/commerce/src/Resolver/DefaultLocaleResolver.php

As soon as I set the price field to "hidden" in both entity form display AND entity view display, the installation of my module suceeds. Afterwards I can enable the price field via admin UI without any problems.

The strange thing is, when you look at the source code, that the simple call to $this->languageManager->getConfigOverrideLanguage() seems to return NULL -> how is this possible? Should this ever be possible to happen? Is this a core bug? Or a configuration problem? However, I've never experienced this or a similar problem with any other configuration entity I include in any of our my modules.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

agoradesign created an issue. See original summary.

bojanz’s picture

Yeah, looks like it's the same as in #2665322: Adding Price field having many languages cause DefaultLocaleResolver issue.

Our code makes an assumption that getConfigOverrideLanguage() is never NULL, we need to investigate if/why that is incorrect.

agoradesign’s picture

Well, that's understandable. When looking into getConfigOverrideLanguage(), one should think, that this should really never happen, as internally (LanguageManager) $this->getCurrentLanguage() gets called, which itself calls getDefaultLanguage(). It's rather unlogical, that this can end up in NULL

Xano’s picture

Xano’s picture

Well, that's understandable. When looking into getConfigOverrideLanguage(), one should think, that this should really never happen, as internally (LanguageManager) $this->getCurrentLanguage() gets called, which itself calls getDefaultLanguage(). It's rather unlogical, that this can end up in NULL

On multilingual sites, ConfigurableLanguageManager is used instead.

agoradesign’s picture

Thanks for clarification - I've already guessed that, when I had a look at your patch - which seems to work for me. But I haven't tried the exactly same situation as described above, so I can't confirm this for 100%.

agoradesign’s picture

FileSize
1.63 KB

As I don't want to use the patch/workaround from #2684873: ConfigurableLanguageManager::getConfigOverrideLanguage() returns NULL anymore, I've created a patch that I currently use to add the fallback within Commerce. I can add a test to DefaultLocaleResolverTest too, but at the moment I only have the patch itself, so that others can benefit from it in the meantime and I've a publicly available patch for my build process :D

bojanz’s picture

Title: Fatal error on installing custom product variation types with enabled price fields in entity view or form display configurations » Call to a member function getId() on a non-object in DefaultLocaleResolver
bojanz’s picture

The core issue never landed, looks like we're going to need to add a workaround after all.

borisson_’s picture

Assigned: Unassigned » borisson_

I'll have a look at this.

borisson_’s picture

I didn't notice that there was already a patch provided. I did pretty much the same thing and tested that it works. Want me to open a PR for this?

borisson_’s picture

Assigned: borisson_ » Unassigned

Unassigning.

bojanz’s picture

Assigned: Unassigned » bojanz
Status: Active » Needs review

Okay, let's see.

  • bojanz committed 7d623d6 on 8.x-2.x authored by agoradesign
    Issue #2683993 by agoradesign, borisson_: Call to a member function...
bojanz’s picture

Status: Needs review » Fixed

Committed a variant of #7, with a bit of additional cleanup. Thanks, everyone.

agoradesign’s picture

Yeaaaaaah! Finally, the oldest of my applied Commerce patches is gone, an old and reliable companion is gone :D :)

Status: Fixed » Closed (fixed)

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