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

GridFieldFilterHeader and ArrayLists #10654

Open
lekoala opened this issue Jan 20, 2023 · 4 comments
Open

GridFieldFilterHeader and ArrayLists #10654

lekoala opened this issue Jan 20, 2023 · 4 comments

Comments

@lekoala
Copy link
Contributor

lekoala commented Jan 20, 2023

Affected Version

4.*

Description

GridFieldFilterHeader parameters are typed against SS_List, but since it's using a SearchContext under the hood, it only works with a DataList

    $results = $this->getSearchContext($gridField)
        ->getQuery($filterArguments, false, false, $dataListClone);

This means you don't get the issue until someone actually tries it (for example, I only realized the issue today in an app that's been in production for two years :-) )

I'm not sure what's the best course of action here:

  • Show some kind of runtime exception ?
  • Prevent adding invalid components ?
  • Introduce a new GridFieldConfig_ArrayList config that have components that actually work with it to make it clear the _Base is only really meant for Datalist ?
  • Use the filter api for ArrayLists ?

Steps to Reproduce

  • Create a grid field with a GridFieldConfig_Base config
  • Pass an ArrayList as value
  • Try to search => it breaks
@maxime-rainville
Copy link
Contributor

@GuySartorelli is this still relevant with your recent work to make GridField work with ArrayList?

@GuySartorelli
Copy link
Member

Dont think so, but I'd have to follow the reproduction steps to be sure.

@GuySartorelli
Copy link
Member

This is mostly handled now - though the core complaint that an exception isn't thrown until someone tries to search is still a problem. The exception it throws is better now, but it still isn't thrown until someone searches, so developers could miss the fact that their config is set incorrectly.

New reproduction steps

Set up an ArrayList in a GridField.

public function getCMSFields()
{
    $fields = parent::getCMSFields();
    $list = ArrayList::create([ArrayData::create(['Title' => 'value1']),ArrayData::create(['Title' => 'value2'])]);
    $config = GridFieldConfig_Base::create();
    $fields->addFieldToTab('Root.Main', GridField::create('test', 'test', $list, $config));
    return $fields;
}

Accessing the form that owns this GridField will immediately result in this exception:

Uncaught LogicException: Cannot dynamically determine columns. Pass the column names to setDisplayFields() or implement a summaryFields() method on SilverStripe\View\ArrayData

Call setDisplayFields() as per the exception message

$config->getComponentByType(GridFieldDataColumns::class)->setDisplayFields(['Title' => 'Title']);

You can now access the form fine, but as soon as you try to search you get this exception:

Uncaught LogicException: Cannot dynamically instantiate SearchContext. Pass the SearchContext to setSearchContext() or implement a getDefaultSearchContext() method on SilverStripe\View\ArrayData

Add the search context as per the exception (the new BasicSearchContext was added in 5.2 and handles arbitrary data just fine)

$searchContext = BasicSearchContext::create(ArrayData::class);
$searchContext->setFields(FieldList::create([
    HiddenField::create(BasicSearchContext::config()->get('general_search_field_name')),
    TextField::create('Title', 'Title'),
]));
$config->getComponentByType(GridFieldFilterHeader::class)->setSearchContext($searchContext);

Searching now works without exceptions.

@GuySartorelli
Copy link
Member

@maxime-rainville Probably still worth looking at throwing that second exception when the form loads instead of waiting for someone to search.

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

4 participants