Semicolon
Semicolon

Reputation: 1914

SilverStripe extensions for GridFieldConfig

I'm trying to create a class that simplifies re-building a GridFieldConfig each time. I use the following setup for nearly every GridFieldConfig in my CMS:

$config = GridFieldConfig::create()->addComponents(
      new GridFieldToolbarHeader(),
      new GridFieldAddNewButton('toolbar-header-right'),
      new GridFieldTitleHeader(),
      ... etc
    )

Rather than repeating myself each time, I'd rather create a class that returns an instance of GridFieldConfig with the components above. So I created this class, but I'm not sure how to make it work properly and how to plug it into the framework / cms.

<?php

class CustomGridConfig extends ??? {

function __construct() {

    $config = GridFieldConfig::create()->addComponents(
      new GridFieldToolbarHeader(),
      new GridFieldAddNewButton('toolbar-header-right'),
      new GridFieldTitleHeader()
      ... etc
    );

    return $config;

  }

}

Eventually it would be implemented in a GridField as follows:

GridField::create('Foo', 'Bar', $this->Foo(), new CustomGridConfig());

I'm not sure if it's possible to create a class within a class like that, also I'm not quite sure how to get this class loaded into the CMS.

Is the concept of this setup viable? If so, how? This would help me understand how to properly extend the framework / cms.

Upvotes: 2

Views: 115

Answers (3)

Dan Hensby
Dan Hensby

Reputation: 1143

SilverStripe already comes with a set of GridFieldConfigs out of the box, that may already do what you need.

To answer your question you'd extend GridFieldConfig and do add your components in the constructor like this:

class CustomGridConfig extends GridFieldConfig {
    public function __construct() {
        $this->addComponents(
            new GridFieldToolbarHeader(),
            new GridFieldAddNewButton('toolbar-header-right'),
            new GridFieldTitleHeader()
            ... etc
        );
    }
}

This class will become available to use after you perform a "flush" (appending ?flush to any site URL) - see the documentation on caching.

See the docs for information on the built in configs.

Upvotes: 3

jjjjjjjjjjjjjjjjjjjj
jjjjjjjjjjjjjjjjjjjj

Reputation: 3118

I think you can create custom GridFieldConfigs by extending GridFieldConfig like so:

class CustomGridFieldConfig extends GridFieldConfig {

    public function __construct() {
        parent::__construct();
        $this->addComponent(new GridFieldToolbarHeader());
        $this->addComponent(new GridFieldAddNewButton('toolbar-header-right'));
        // etc...
    }

}

And then pass it to your GridField like so:

GridField::create(
    'Foo', 
    'Bar', 
    $this->Foo(), 
    CustomGridFieldConfig::create()
);

Check out the class GridFieldConfig_RelationEditor in file GridFieldConfig.php for inspiration.

Upvotes: 3

Barry
Barry

Reputation: 3318

your concept is good and setup is viable. I'd have just a plain class and then add your method... should be fine as a constructor but if not a static method should be fine...

class CustomGridConfig {
    public static function create() {
        $config = GridFieldConfig::create()->addComponents(
              GridFieldToolbarHeader::create(),
              GridFieldAddNewButton::create('toolbar-header-right'),
              GridFieldTitleHeader::create()
              ... etc
            );

        return $config;
    }
}

Upvotes: 1

Related Questions