Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Accessing gridfieldconfig in modeladmin


Go to End


4 Posts   3827 Views

Avatar
BlueO

Community Member, 52 Posts

5 July 2012 at 6:49pm

Hi there,

I'm trying to customise the gridfield that Model admin uses to manage dataobjects. I know i can use searchable_fields and summaray_fields etc to customise it a bit. Would anyone know how would I go about accessing the gridfieldconfig to add gridfield components?

cheers

Bernard

Avatar
martimiz

Forum Moderator, 1391 Posts

5 July 2012 at 11:48pm

The GridField is created in ModelAdmin->getEditForm(). This function does it all: create the config, create the field and then create and return the Form that holds the field. There doesn't seem to be an easy way to 'intercept' the creation of the config. The getEditForm function can be decorated - but from there you'll have to access the form already created - so you might be able to do something like this (if by chance I understand it correctly):

// suppose the DataObject involved is called 'MyDataObject' 
$form->dataFieldByName('MyDataObject')->getConfig()->addComponent($someComponent);

Or (faster, maybe less complex but just not as nice) copy the entire function to your class MyDataObject_Admin extends ModelAdmin { ... }

Avatar
BlueO

Community Member, 52 Posts

6 July 2012 at 4:22pm

Hey great, thanks I'll give it a shot and post back

Avatar
Blackdog

Community Member, 156 Posts

20 July 2012 at 1:41am

If anyone comes across the issue they can use this code.

$form->Fields()->fieldByName('YourFieldName')->getConfig()->addComponent($Component);

As dataFieldByName has been deprecated in 3.0

I suggest you use the Extension api and build a quick extension for updateEditForm rather than duplicate the function from ModelAdmin.

class YourExtension extends Extension
{
    function updateEditForm(&$form)
    {
        $form->Fields()->fieldByName('YourField')->getConfig()->addComponent($Component);
    }
}