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

GridField and GridState_component bug #6886

Closed
t3hn0 opened this issue May 8, 2017 · 5 comments
Closed

GridField and GridState_component bug #6886

t3hn0 opened this issue May 8, 2017 · 5 comments

Comments

@t3hn0
Copy link
Contributor

t3hn0 commented May 8, 2017

Hi!

I'm not sure if it's intentional but if you construct GridField through injector like in example bellow:

GridField::create('MyRelation')
	->setTitle('My Title')
	->setList($this->MyRelation())
	->setConfig( GridFieldConfig_Base::create() )

then GridfieldState_Component is not set on GridField config. GridState_Component is added to current GridFieldConfig in GridField constructor, but if we redefine GridFieldConfig with GridField::setConfig, GridState_Component is never set. That can cause JavaScript error while using GridFieldSortableRows and probably others.

Shouldn't you merge these lines

public function __construct($name, $title = null, SS_List $dataList = null, GridFieldConfig $config = null) {
	...
	$this->setConfig($config ?: GridFieldConfig_Base::create());

	$this->config->addComponent(new GridState_Component());
	$this->state = new GridState($this);		
	...
}

and put it all in GridField::setConfig method?

@robbieaverill
Copy link
Contributor

robbieaverill commented May 8, 2017

Hey @t3hn0 - which version of SilverStripe are you running? Looks like 3.x, just want to confirm.

Quick investigation shows that you can create a GridField with a _Base config by either omitting the config from the constructor arguments and allowing it to be created by default, by providing it to the constructor or by setting it after the construct has run. The first two of those scenarios will also set a GridState_Component into the config, regardless of whether you've passed it in or allowed a default config to be created.

I think the point of this issue is that these two scenarios produce a different config - one with a GridState_Component and one without:

// Config (Base) contains a grid state component
$gridField = GridField::create('Grid');

// Config (Base) does not contain a grid state component
$gridField = GridField::create('Grid')
    ->setConfig(GridFieldConfig_Base::create());

My impression of the purpose of the code below (from the GridField constructor) is to add a GridState_Component to all GridFieldConfig's:

$this->setConfig($config);

$this->config->addComponent(new GridState_Component());
$this->state = new GridState($this);

If this is causing trouble, then perhaps the bottom two lines could be moved into GridField::setConfig to ensure they're also called whenever you set a new config class via the public API?

@t3hn0
Copy link
Contributor Author

t3hn0 commented May 8, 2017

Hey @robbieaverill it was version 3.5.

It's not only GridFieldConfig_Base that's affected but as you guessed, every config which is set with GridField::setConfig is missing GridState_Component.

Suggested update should be ok.

@robbieaverill
Copy link
Contributor

Hey @t3hn0 - cool. Would you be willing to submit a patch for this?

@tractorcow
Copy link
Contributor

tractorcow commented May 9, 2017

I agree with @t3hn0. The original behaviour (assign state on constructor) is flawed, and as this issue demonstrates, isn't reliable.

I would also agree with moving to setConfig(), and having the constructor call this. Maybe have this code check to see if one such config already exists, and add it only conditionally.

@t3hn0
Copy link
Contributor Author

t3hn0 commented May 9, 2017

@robbieaverill ok this is my first try, so please don't kill me if I got it wrong :)
I created pull request #6896

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