Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelAdmin SearchForm filters only on the first field #9356

Closed
7 tasks done
lekoala opened this issue Dec 17, 2019 · 12 comments
Closed
7 tasks done

ModelAdmin SearchForm filters only on the first field #9356

lekoala opened this issue Dec 17, 2019 · 12 comments

Comments

@lekoala
Copy link
Contributor

lekoala commented Dec 17, 2019

Affected Version

4.4.4

Description

So I have this model admin instance displaying members

If I type something in the search bar, it searches only on the first name. Data posted is something like

filter[SilverStripe-Security-Member][FirstName]: ***MYSEARCHQUERYHERE***

it's not searching on all searchable fields by default. which means you need to use the little dropdown to search each time by a specific field. It's very counterintuitive in a google era where people type stuff and expect the system to match records based on that.

Steps to Reproduce

  • Have a model admin managing users
  • Type something in the search box other than the firstname (the Surname for example)
  • Expect a match, but get an empty list

Am I missing something ? Also, it's very confusing at the moment to know how to properly deal with this since we are in the middle of deprecated features (searchform getting replaced by the gridfieldfilterheader).

Any idea how to "simply" match on all searchable columns by default?

Acceptance criteria

  • Gridfiled search term search across multiple fields.
  • The list of searchable fields, default to the summary fields, but can be customised.
  • Non-searchable fields (e.g.: dynamic field) are excluded.
  • The solution works for Relation fields (Category.Title)
  • When applied to a DataObject extending another DataObject, the solution can search accross tables
  • The solution doesn't crash when searched field include a none-searchable column
  • Appropriate extensions hooks are added and care is given to making the final solution extensible..

Pull requests

@lerni
Copy link
Contributor

lerni commented Dec 19, 2019

Same issue bugged me as well. I agree it feels unexpected and counter intuitive.

@Cheddam
Copy link
Member

Cheddam commented Dec 19, 2019

Hey @lekoala, thanks for raising this! My initial impression is that this would require an expansion of the $searchable_fields config structure to allow applying multiple DataObject fields to a single search field. We likely wouldn't want to make the main search UI match on every field by default, as some fields might not be useful in a search (especially on more complex projects). Definitely worth exploring options, though - I agree that the current behaviour feels unnecessarily shallow.

@lekoala
Copy link
Contributor Author

lekoala commented Dec 20, 2019

@Cheddam something like this would work for me

  private static $searchable_fields = [
       'FirstName' => [
          'field' => TextField::class,
          'filter' => 'PartialMatchFilter',
          'global' => true,
       ],
       'Surname' => [
          'field' => TextField::class,
          'filter' => 'PartialMatchFilter',
          'global' => true,
       ],
   ];

this way, you can opt in to filter on multiple fields by default by setting the "global" config to true

@Cheddam
Copy link
Member

Cheddam commented Jan 13, 2020

Related: #9341 aims to implement proper APIs for determining which (single) field to use for the search bar. When tackling this issue, we'd likely now be looking at expanding that API.

@lekoala
Copy link
Contributor Author

lekoala commented Jan 31, 2020

@Cheddam while this is being improved, any idea how to simply allow searching on multiple fields? It's really not clear how to do this properly

@Cheddam
Copy link
Member

Cheddam commented Feb 5, 2020

@lekoala You might be able to sufficiently modify the behaviour of the default search field by customising getList() on your ModelAdmin - see the ModelAdmin docs for some idea on how custom fields / filtering can be accomplished. This might become a bit easier once #9341 is merged.

@lekoala
Copy link
Contributor Author

lekoala commented Feb 5, 2020

@Cheddam i may be wrong, be as far as I can tell, gridfieldfilterheader applies its change AFTER getList is called no? calling something like

public function getList()
{
    $list = parent::getList();
    die($list->sql());

will show a sql statement without any where clause applied (even though I have a search parameter in the search bar)

@lekoala
Copy link
Contributor Author

lekoala commented Mar 10, 2020

@lerni so since there is no easy solution, I had to build one. not sure it's the best way, but it works!

the class is here
https://github.com/lekoala/silverstripe-base/blob/master/src/ORM/Search/WildcardSearchContext.php

the idea is to override the default search context in the GridFieldFilterHeader

it's not so easy to do, because there is no way to set the searchContext, so I had to use reflection to do so. then, the idea is simply if there is only one search argument to do a filterAny for all availables filters. as an added bonus, i deal with multiple words as multiple AND/OR groups.

sample usage in model admin

        $filter =  $config->getComponentByType(GridFieldFilterHeader::class);
        $wildCardHeader = WildcardSearchContext::fromContext($filter->getSearchContext($gridfield));
        $wildCardHeader->setWildcardFilters(['FirstName', 'Surname','Email']);
        $wildCardHeader->replaceInFilterHeader($filter);

@lekoala
Copy link
Contributor Author

lekoala commented Mar 10, 2020

@Cheddam using my current solution feels much better than the default behaviour. having to click on the dropdown and do a specific search should really not be the default behaviour in my opinion. for members list (and probably lots of other types of data) it makes much more sense to search on firstname, surname and email. i think it's really sad that there is no built in option for this.

@lekoala
Copy link
Contributor Author

lekoala commented May 12, 2021

For reference
#9836

@brynwhyman
Copy link
Contributor

Just making a note here as it's listed in the description but not as a comment, there's an open pull request by @maxime-rainville to address this: #9836

@emteknetnz
Copy link
Member

Linked PRs have all been merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants