silverstripe-admin icon indicating copy to clipboard operation
silverstripe-admin copied to clipboard

Can't search against `many_many` relations in `GridFieldFilterHeader`

Open sabina-talipova opened this issue 1 year ago • 0 comments

Description

This problem was identified during the work on task https://github.com/silverstripe/silverstripe-admin/issues/1733

Since this task is complex in nature and requires changes to some main files, which may affect the operation of some other elements and functionality of the CMS. So, when working with SearchableDropdownField, we process and use the received data differently, so we should analyse how this field is used in CMS.

It is also worth noting that this problem was identified when creating this field using FormBuilder (tested by Search Options and Asset Admin).

This field works great if the data is processed on the client side before being sent to the server, for example when saving new values. Therefore, at the moment, this problem has been identified only when using SearchableDropdownField with search, but it is possible that this problem may appear when using SearchableDropdownField in other cases when, after sending data to the server, the server returns this value and updates state and then these values are displayed in another CMS field , for example when displaying tags with values.

I did some research on this issue and identified some areas that need to be updated for SearchableMultiDropdownField to work correctly with multi values. First of all, we need to use a unified approach when storing SearchableField data in state. This is due to the fact that react-select as a value object / array of objects. This in turn will break the approach used for searching. Since the search occurs by a specific value, string or number. Therefore, we should also make changes to the following function (See). It is also possible that changes should be made to the SearchContext since in the current implementation the search by many-many relationships does not occur. The next item that might need to be updated is "GridFieldState" to support looking up many-many relationships.

tl;dr

There's no way to search against a many_many relation in the GridFieldFilterHeader right now. Other fields and relations including many_many do work as expected.

Searching with a SearchableMultiDropdownField with multi values doesn't work, which is a related concern.

See discussion here for more details:

  • https://github.com/silverstripe/silverstripe-admin/pull/1736

Step to reproduce

  • Add the following code to the test project:
<?php

namespace App\Extension;

use SilverStripe\Core\Extension;
use SilverStripe\Forms\SearchableMultiDropdownField;
use SilverStripe\Security\Member;

class GroupSearchableFieldsExtension extends Extension
{
    public function updateSearchableFields(array &$fields)
    {
        $fields['Members'] = [
            'title' => 'Users',
            'field' => SearchableMultiDropdownField::create('MemberID', 'Users', Member::get()),
        ];
    }
}
SilverStripe\Security\Group:
  extensions:
    - App\Extension\GroupSearchableFieldsExtension
  • Create a group and make it the child of a group
  • Add members to the new group (at least 2)
  • In /admin/security/groups click the search magnifying glass, then click the "search options" dropdown.
  • In the "Users" dropdown, select new members and click "Search"
  • Server doesn't return any results and selected values will be removed from SearchableDropdownField.

Related issue

  • https://github.com/silverstripe/silverstripe-admin/issues/1733

sabina-talipova avatar May 12 '24 21:05 sabina-talipova