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.

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

pmate created an issue. See original summary.

mvogel’s picture

I got the same problem.

https://git.drupalcode.org/project/views_autocomplete_filters/-/commit/b... introduced the "bug"

with $entity->$field_name->value

          $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
          $entity = $view->result[$index]->_entity->getTranslation($langcode);
          $raw_field = $entity->$field_name->value;

The 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.

pmate’s picture

StatusFileSize
new911 bytes

@mvogel I was suspecting the patch will work only for my case.

Could you give attached patch a try?

mvogel’s picture

Thanks 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 $entity will 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->value which will always be null, because the user object hasn't a field node_title

pmate’s picture

Thanks, 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.

maxilein’s picture

1.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.

crutch’s picture

Getting same error. I tried the diffs and it isn't honoring the entity reference relationship using title.

kostask made their first commit to this issue’s fork.

kostask’s picture

The 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.

joro78’s picture

I 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.

dunebl’s picture

I confirm #10 is solving my issue

pmate’s picture

Issue summary: View changes
joseph.olstad’s picture

keshavv’s picture

Status: Active » Needs review
joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

The MR resolves repeated PHP errors that occur when the search is used, we had so many that it was contributing to a server crash.

joseph.olstad’s picture

Status: Reviewed & tested by the community » Needs work

Ok, 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.

  • vasike committed f382a810 on 8.x-1.x
    Issue #3345680 by pmate: Attempt to read property "value" on null in src...
vasike’s picture

Version: 8.x-1.4 » 8.x-1.x-dev
Status: Needs work » Needs review
Related issues: +#3353406: Check entity field really is translatable

Unfortunately, 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_2 as "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

mvogel’s picture

Unfortunately, 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:

$entity is an instance of User and the $real_field_name is a field from a NodeType, that results in Attempt to read property "value" on null in Drupal\views_autocomplete_filters\Controller\ViewsAutocompleteFiltersController->autocomplete() Line 231

Steps to reproduce:

  1. Create an entity reference field for user that points for example on node article
  2. create a view for users that has an exposed filter for the entity reference field from 1 for example the title field (activate views autocomplete filters)
  3. Type something in the input field and get the error message from above
vasike’s picture

@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

mvogel’s picture

You are right, my problem is the issue you posted. I tested your commit and it fixes the problem with different field identifiers field_something_2

sorry for the unnecessary noise.

vasike’s picture

@mvogel it's not noise, it's voice ... and it's important for issues and solutions.
thanks a lot

vasike’s picture

@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

  • vasike committed f2521394 on 8.x-1.x
    Issue #3345680: Attempt to read property "value" on null in src/...
vasike’s picture

it 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

  • vasike committed 0ca4b464 on 8.x-1.x
    Issue #3345680: Attempt to read property "value" on null in src/...
vasike’s picture

thanks 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.

mvogel’s picture

+1 from me. Now all works 100%

vasike’s picture

Status: Needs review » Fixed

i think we can close this.

please, feel free to re-open, if not done.

Status: Fixed » Closed (fixed)

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