Allow other modules to alter the autocomplete values

Created on 12 October 2023, 9 months ago
Updated 10 March 2024, 4 months ago

Problem/Motivation

Hi,
we are using the contrib module real name for the user display name. If you use the real name "field" as a filter, the real name value is a string attached to the user object.

$raw_field = $entity->$real_field_name->value;

$raw_field = $entity->realname->value;

The $entity->realname is a type of string, and we got the error message Warning: Attempt to read property "value" on string.

Steps to reproduce

  1. Install the real name module and config the real name value setting
  2. Add as a filter to a user view and expose it with views autocomplete filter
  3. Search for a real name value and get the error message above. No autocomplete dropdown will be rendered

Proposed resolution

It is a really specific problem with the contrib real name module. So I don't know if you want to change it at all. But I added a "if type check" from

$raw_field = $entity->$real_field_name->value;

to

            $real_field = $entity->$real_field_name;
            if ($real_field instanceof FieldItemListInterface) {
              $raw_field = $real_field->value;
            }
            elseif (is_string($real_field)) {
              $raw_field = $real_field;
            }

and it works for my use cases. I am unsure if FieldItemListInterface is sufficient for all fields in exposed forms.

Feature request
Status

Needs work

Version

2.0

Component

Code

Created by

🇩🇪Germany mvogel

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

Merge Requests

Comments & Activities

  • Issue created by @mvogel
  • 🇩🇪Germany mvogel

    attached patch

  • Status changed to Needs review 9 months ago
  • Status changed to RTBC 9 months ago
  • 🇮🇳India viren18febS

    #2 is working for me so moving to RTBTC.

  • Status changed to Needs work 8 months ago
  • 🇷🇴Romania vasike Ramnicu Valcea

    sorry guys, but thanks

    This can't considered as bug report, as this too specific to a module, so i would say it's a feature request.
    Also this means it should be for the next D10 version (first).

    Also there was an update https://www.drupal.org/project/views_autocomplete_filters/issues/3394657 🐛 Not working for filters for elements/columns of Compound Fields - Attempt to read property "value" on null Active which probably "messed around" your patch.

    Is there another "similar" case where this patch could "save the day"?

    Also do you have any thoughts or suggestions, about a need of hook or something that could easily fix such custom cases?
    As it is hard to solve all contrib cases.

    Just saying ....
    Thanks again for all efforts and support.

  • 🇩🇪Germany mvogel

    You are right. Inversion of control should be the approach here. I will think about it and make a proposal later.

  • 🇩🇪Germany mvogel

    I created a merge request.
    I defined an alter hook to change the value for the autocomplete result. The will run after the default code so contrib or custom modules can change it.
    I made the original code null save by adding ?->

    $raw_field = $entity->$real_field_name?->value;
    

    Because otherwise, it will generate watchdog entries for the 'realname' fields.

    I don't know if this adds too much complexity, and if it is a straightforward approach, I tried to document as much as possible and give the hook as much context as someone may need.

  • Status changed to Needs review 8 months ago
  • 🇩🇪Germany mvogel

    I forgot to change the status to "needs review"

  • Status changed to Needs work 4 months ago
  • 🇷🇴Romania vasike Ramnicu Valcea

    Thread on MR + conflicts with the latest updates.

    + update the issue title to cover the solution needed.

Production build 0.69.0 2024