Hi,
After the recent update to version 1.4 the autocomplete stopped working for me on one of the views. There was the following warning in the ajax response:
Warning: Attempt to read property "value" on null in /home/customer/www/xxxxxxxxx/source/web/modules/contrib/views_autocomplete_filters/src/Controller/ViewsAutocompleteFiltersController.php on line 220
I did a bit of tracking and it looks like the field selected in "Field with autocomplete results" of the exposed filter has to have the exact same name as the field in the entity object. In my case I have 2 "Name" fields (one visible, one hidden) in my view and use the hidden one for autocomplete. The entity field name is 'name' but the view field name that I use in "Field with autocomplete results" is 'name_1'.
I'm not entirely sure if this is supported case but I managed to fix it on my own, see attached diff.
I have also attached a simple node based view for reproducing the issue.
If necessary, I can provide more specific reproduce steps.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | patch2.diff | 911 bytes | pmate |
| patch-view-and-entity-field-name-mismatch-warning.diff | 813 bytes | pmate | |
| views.view_.repro_autocomplete.yml | 9.19 KB | pmate |
Issue fork views_autocomplete_filters-3345680
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:
- 3345680-attempt-to-read
compare
- 8.x-1.x
changes, plain diff MR !5
Comments
Comment #2
mvogel commentedI got the same problem.
https://git.drupalcode.org/project/views_autocomplete_filters/-/commit/b... introduced the "bug"
with
$entity->$field_name->valueThe patch does not fix it for us, because in our case we have a user view and join with a node table and the autocomplete exposed form filters some field value of the node table, and the user entity can not have the node field in this case.
Proposed solution:
The code has to check to which entity the field in the exposed form belongs to get the right field with the translation.
Comment #3
pmate commented@mvogel I was suspecting the patch will work only for my case.
Could you give attached patch a try?
Comment #4
mvogel commentedThanks for your patch. I tested the patch but it does not work.
It will only work when the field belongs to the entity type.
In my case, I got a user view, so the
$entitywill be a user object. My user object has an entity reference field to a node object. In my view, I can filter for a field in the node object (like for example field_node_title). The code will then try$entity->field_node_title->valuewhich will always be null, because the user object hasn't a field node_titleComment #5
pmate commentedThanks, it makes sense.
So my case is different from yours and I guess that the real solution would be to revert the https://git.drupalcode.org/project/views_autocomplete_filters/-/commit/b... and look for a different solution for translations but I'm not sure.
Comment #6
maxilein commented1.4 killed it for me using the autocomplete on the name (not ID) of a term reference field in the filter.
My case, which I think is not rare, because you do not select manually be entering a term ID:
The term name (for the autocomplete) is not part of the filtered entity. The entity only has the term reference field. But I filter on the term name which has to be pulled in via a relationship.
entity with term reference field -> relationship to taxonomy -> retrieve the term name for the autocomplete.
Comment #7
crutch commentedGetting same error. I tried the diffs and it isn't honoring the entity reference relationship using title.
Comment #10
kostask commentedThe above MR partially reverts https://git.drupalcode.org/project/views_autocomplete_filters/-/commit/b.... Not a fix as "Autocomplete is not working for languages other than the default language" is reverted, but I added as a hotfix for anyone experiencing this issue and not having translations.
Comment #11
joro78 commentedI still have problems with the views autocomplete filters. I think that the field type, we are requesting as an exposed filter, causes the issue. Autocomplete filters work when Title field is exposed on a multilingual site. However when using it to get values from an address field, such as locality for instance, the error above appears in the log and data is not visualized into the autocomplete field.
Comment #12
duneblI confirm #10 is solving my issue
Comment #13
pmate commentedComment #14
joseph.olstadgoing with the MR for now, using patch
https://git.drupalcode.org/project/views_autocomplete_filters/-/merge_re...
Comment #15
keshavv commentedComment #16
joseph.olstadThe MR resolves repeated PHP errors that occur when the search is used, we had so many that it was contributing to a server crash.
Comment #17
joseph.olstadOk, so reverting this causes another regression.
Ok so a possible hint here, our default language is NOT English and we do have a second language.
Comment #19
vasikeUnfortunately, the previous attempts were more about revert the introducing change, if i didn't miss something.
So for start i merged a commit about translation (missing) https://www.drupal.org/project/views_autocomplete_filters/issues/3353406...
To be sure we are getting the value, even there is no translation.
So, also please check this issue, maybe we could also close this one, too.
This issue looks quite simple, we're trying to get a value from entity - RAW value, but for "another" field name.
Which is usual in Views, to get with some views, that have
field_something_2as "identifier".So, i pushed a commit, that get the real field name. before trying to use the entity field data.
I hope it's the solution, but for now, i'll put (only) on "Needs Review".
thanks all for your efforts - attempts and testing.
p.s. if someone thinks, credit is needed for the efforts on solution, please, comment about it. thanks
Comment #21
mvogel commentedUnfortunately, your commit does not fix my problem. For example, I got a "user entity" view that has an entity reference field to a node entity. In my exposed form I can filter this entity reference field. So I got the problem:
$entityis an instance of User and the$real_field_nameis a field from a NodeType, that results inAttempt to read property "value" on null in Drupal\views_autocomplete_filters\Controller\ViewsAutocompleteFiltersController->autocomplete() Line 231Steps to reproduce:
Comment #22
vasike@mvogel thanks for the feedback, could this be related with
https://www.drupal.org/project/views_autocomplete_filters/issues/3348802 ?
also, please, check the patches from https://www.drupal.org/project/views_autocomplete_filters/issues/3374142
they adds extra checks about entity and values to be used.
thanks
i know it worked ... years ago
Comment #23
mvogel commentedYou are right, my problem is the issue you posted. I tested your commit and it fixes the problem with different field identifiers
field_something_2sorry for the unnecessary noise.
Comment #24
vasike@mvogel it's not noise, it's voice ... and it's important for issues and solutions.
thanks a lot
Comment #25
vasike@mvogel could you, please, check again you issue.
A feedback is welcomed also on the related issue https://www.drupal.org/project/views_autocomplete_filters/issues/3348802...
thanks a lot
Comment #27
vasikeit seems there was similar issue for Combine filters and maybe other cases.
There could filters for "non fields of an entity".
So i pushed a commit with extra checks and fallback for no entity for the given filter.
i hope we're close to a solution here. please test and feedback
thanks
Comment #29
vasikethanks to @mvogel feedback
on related issue https://www.drupal.org/project/views_autocomplete_filters/issues/3348802...
i pushed another fix, about field name value.
And it was the same error on some cases.
let's see the verdicts.
Comment #30
mvogel commented+1 from me. Now all works 100%
Comment #31
vasikei think we can close this.
please, feel free to re-open, if not done.