Hello,
I'm having issues on multilingual sites (EN/FR) with the address field when it is displayed on the views.
I have an event content type, and it has an address field, this field is an Address field type, and it is available for translation as well.
I have some english and french events, and I have a small view page displaying these events.
This view seems to work fine on the english page, but for the french page it breaks sometimes, giving us this problem:
Error: Call to a member function getLocality() on null in /srv/bindings/8c39fdd1734d4352a9bf1b12a072b801/code/modules/contrib/address/src/Plugin/views/field/Subdivision.php on line 101 #0 /srv/bindings/8c39fdd1734d4352a9bf1b12a072b801/code/core/modules/views/src/Plugin/views/field/FieldPluginBase.php(1146): Drupal\address\Plugin\views\field\Subdivision->render(Object(Drupal\views\ResultRow))
..
..
Do you guys have any idea why this could be happening?
I was having a look on line 101 at src/Plugin/views/field/Subdivision.php:
case 'locality':
$code = $address->getLocality();
Looks like the $address object is null for some french nodes
is it possible to put a condition that checks if this $address object is null?
Thanks in advance
| Comment | File | Size | Author |
|---|
Issue fork address-2995680
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:
- 2995680-2.1.x
changes, plain diff MR !89
- 2995680-2.0.x
changes, plain diff MR !75
- 2995680-error-on-call
compare
Comments
Comment #2
bojanz commentedThe odd thing is, we already have a check:
If the subdivision value is empty, we stop right there.
The question is, how can the subdivision value be non-empty, but the parent address be empty.
Could you var_dump $value, to check what's there?
Comment #3
bojanz commentedNo further information provided.
Comment #4
amagdy commentedI'm having the same issue on 8.x-1.8
attached patch fixed the issue for me
Comment #5
igoragatti commentedI have come across this error on a client's website.
Steps to reproduce:
Comment #6
igoragatti commentedComment #7
danrodInteresting, I reported this a while ago and forgot about it, I'll look into this again just for fun.
Comment #8
igoragatti commentedPatch on #4 worked for me.
Comment #9
danrodHey @igoragatti I tried your steps in in the 2.0.x branch and I couldn't see the issue again, I see that you tested on the release 8.x-1.8, could you upgrade to the latest 2.0.x release? If not it would be nice if the patch goes in the next 8.x-1.x release (if it's still maintained)
Comment #10
kenowax commentedI'm having this issue on my project using version 2.0.2.
Not sure exactly which steps could be taken to reproduce it yet.
Here's the exact error message :
Comment #11
kenowax commentedAfter checking branch 2.1.x, I see that no null checks are made on the ItemList->first() function which, contrary to documentation, can return null. This is found in /src/Plugin/views/field/Subdivision.php
The error occurs when trying to access a view page having an address field.
Most probably, the data I have came from some Migrate import, which could explain why not all fields were filled in.
I suggest adding null-handling in the code.
Comment #12
nitesh pawar commentedI encountered the same error when attempting to add address subfields like "Content: Address:locality" in Drupal Views. The error message is: Error: Call to a member function getLocality() on null in Drupal\address\Plugin\views\field\Subdivision->render() (line 103 of /var/www/html/web/modules/contrib/address/src/Plugin/views/field/Subdivision.php).
I have re-roll the patch with 2.0.x branch.
Comment #13
barry75 commentedI encountered the same issue, after migrating.
Manually implementing patch as described in #12 solved the issue. (automatic patch application with composer did not work).
Will this get integrated in the next release of the address module?
Comment #14
pfrenssen#11 is right, I received a bug report with the following error logged:
According to the bug report it happened during a very long CSV export of 40000 records. I did not have the patience to replicate it, but the solution seems clear, we have to check the return value as proposed by @kenowax.
I will update the MR.
Comment #16
pfrenssenCreated new MR against 2.0.x. It looks like 2.1.x has not been worked on for a long time, and 2.0.x is more recent. I can rebase against 2.1.x if needed.
Comment #18
pfrenssenComment #19
pfrenssenHiding patches, drupal.org is moving to merge requests. See Using Gitlab to contribute to Drupal.
Comment #20
apurva.patki commentedComment #22
tbkot commentedI was able to reproduce it thanks to comment #5. However, the solution in the MR didn't cover the case when the translation has a value for "address" while the original language doesn't. With this type of scenario, the condition
if (empty($value) || empty($this->options['display_name'])) {will not pass and code will proceed. But,$entity = $this->getEntity($values);will return the original entity even if the interface language is different. This leads to an empty result in the views, while it really exists.I've modified the MR to return the 'value' instead of an empty string, so we don't lose it.
Comment #24
benstallings commentedLooks good to me!
Comment #25
dwwThanks for working on this.
Side note: we don't need separate MRs for 2.1.x and 2.0.x unless there are conflicts or something. Would have been happy to continue in !75 instead of opening an identical !89 that targets a different branch.
That said, I'm not terribly psyched about committing "fixes" like this. This code is a views plugin for an address field. Why wouldn't we have an address field?
This "fix" seems like it's only further hiding some deeper bug that's going to be even harder to find and fix if we commit this. I'd rather we spent the effort to figure out what "some reason" actually is and correct that, not just wrap it in
?->and further confuse people who are expecting a subdivision in their view and getting nothing instead. I'm not going to go so far as to say we should throw an Exception here and completely blow up in someone's face if they've gotten into whatever situation causes this. But I'm not really in favor of silently ignoring uncomfortable situations where reality doesn't match the code's expectations.Comment #26
tbkot commented@dww, the main issue here, at least in a way that I was able to reproduce it based on comment #5, is that the
getEntitymethod loads the entity with the default language. The default views field plugin (EntityField) loads the entity translation in thegetValueand gets the value from it instead of the original entity, which is used inSubdivision. I'm unsure if we can extend EntityField instead of FieldPluginBase, or if it's better to duplicate the logic and try to get the translated entity first.