Attempt to read property "value" on null in src/Controller/ViewsAutocompleteFiltersController.php on line 220

Created on 3 March 2023, almost 2 years ago
Updated 9 October 2023, about 1 year ago

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.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @pmate
  • 🇩🇪Germany mvogel

    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.

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

    Could you give attached patch a try?

  • 🇩🇪Germany mvogel

    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

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

  • 🇦🇹Austria maxilein

    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.

  • 🇺🇸United States crutch

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

  • First commit to issue fork.
  • @kostask opened merge request.
  • 🇬🇷Greece kostask

    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.

  • 🇧🇬Bulgaria joro78

    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.

  • 🇧🇪Belgium DuneBL

    I confirm #10 is solving my issue

  • Status changed to Needs review over 1 year ago
  • Status changed to RTBC over 1 year ago
  • 🇨🇦Canada joseph.olstad

    The MR resolves repeated PHP errors, we had so many that it was contributing to a server crash.

  • Status changed to Needs work over 1 year ago
  • 🇨🇦Canada joseph.olstad

    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...
  • Status changed to Needs review over 1 year ago
  • 🇷🇴Romania vasike Ramnicu Valcea

    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... 🐛 Check entity field really is translatable Needs review
    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

  • 🇩🇪Germany mvogel

    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
  • 🇷🇴Romania vasike Ramnicu Valcea

    @mvogel thanks for the feedback, could this be related with
    https://www.drupal.org/project/views_autocomplete_filters/issues/3348802 🐛 Autocomplete with entity reference/relationship Needs review ?

    i know it worked ... years ago

  • 🇩🇪Germany mvogel

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

    sorry for the unnecessary noise.

  • 🇷🇴Romania vasike Ramnicu Valcea

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

  • 🇷🇴Romania vasike Ramnicu Valcea

    @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... 🐛 Autocomplete with entity reference/relationship Needs review

    thanks a lot

    • vasike committed f2521394 on 8.x-1.x
      Issue #3345680: Attempt to read property "value" on null in src/...
  • 🇷🇴Romania vasike Ramnicu Valcea

    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/...
  • 🇷🇴Romania vasike Ramnicu Valcea

    thanks to @mvogel feedback
    on related issue https://www.drupal.org/project/views_autocomplete_filters/issues/3348802... 🐛 Autocomplete with entity reference/relationship Needs review
    i pushed another fix, about field name value.
    And it was the same error on some cases.

    let's see the verdicts.

  • 🇩🇪Germany mvogel

    +1 from me. Now all works 100%

  • Status changed to Fixed about 1 year ago
  • 🇷🇴Romania vasike Ramnicu Valcea

    i think we can close this.

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

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

Production build 0.71.5 2024