Skip to content

Instantly share code, notes, and snippets.

@zanderwar
Created June 9, 2017 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zanderwar/9df38a5ab7584a02254a656bdfc7a86b to your computer and use it in GitHub Desktop.
Save zanderwar/9df38a5ab7584a02254a656bdfc7a86b to your computer and use it in GitHub Desktop.
An example of swapping the GridFieldConfig of a particular GridField in a ModelAdmin
<?php
class Example extends ModelAdmin
{
private static $url_segment = 'Example';
private static $menu_title = 'Example';
private static $managed_models = [
Site::class
];
/**
* @param null $id
* @param null $fields
*
* @return Form
*/
public function getEditForm($id = null, $fields = null)
{
/** @var Form $form */
$form = parent::getEditForm($id, $fields);
$fields = $form->Fields();
foreach ($fields as $field) {
if (!$field instanceof GridField) {
continue;
}
/** @var GridField $field */
if ($field->getName() === TestScraperResult::class) {
$field->setConfig(GridFieldConfig_RecordViewer::create());
}
}
return $form;
}
}
@zanderwar
Copy link
Author

SS4 equivalent:

    public function getEditForm($id = null, $fields = null)
    {
        /** @var Form $form */
        $form = parent::getEditForm($id, $fields);
        $fields = $form->Fields();
        foreach ($fields as $field) {
            if (!$field instanceof GridField) {
                continue;
            }

            /** @var GridField $field */
            if ($field->getName() === str_replace('\\', '-', VideoCategory::class)) {
                $config = $field->getConfig();
                
                $field->setConfig($config);
            }
        }
        return $form;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment