silverstripe-framework/tests/php/Forms/GridField/GridFieldDetailFormTest/CategoryController.php
Sam Minnee 8883413ba7 NEW: Add GridFieldDetailForm::setRedirectMissingRecords()
This new opt-in setting will let grid field detail forms redirect to the
“Correct” URL of a GridField if it’s not found in the current list.

This works by:
 * Looking for the item in the database
 * If it exists, check for a CMSEditLink() method that returns a value
 * If so, redirect to that

This is useful if you have a number of grid fields that each show a
partial list of records, and it’s possible for the user to make changes
such the item no longer appears in the list, but does appear in another
list.

It’s an opt-in feature as I think all changes like this should be
opt-in, based on previous experiences improving GridField and in turn
breaking SecurityAdmin and slowing versioned-data-browsing down. ;-)
2021-05-21 13:16:00 +12:00

67 lines
2.5 KiB
PHP

<?php
namespace SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\TextField;
/**
* @skipUpgrade
*/
class CategoryController extends Controller implements TestOnly
{
private static $allowed_actions = ['Form'];
protected $template = 'BlankPage';
public function Link($action = null)
{
return Controller::join_links('GridFieldDetailFormTest_CategoryController', $action, '/');
}
public function Form()
{
// GridField lists categories for a specific person
$person = Person::get()->filter('FirstName', 'Jane')->First();
$detailFields = singleton(Category::class)->getCMSFields();
$detailFields->addFieldsToTab(
'Root.Main',
[
new CheckboxField('ManyMany[IsPublished]'),
new TextField('ManyMany[PublishedBy]')
]
);
$categoriesField = new GridField('testfield', 'testfield', $person->Categories());
$categoriesField->getConfig()->addComponent(
$gridFieldForm = new GridFieldDetailForm(
$this,
'SilverStripe\\Forms\\Form'
)
);
$gridFieldForm->setFields($detailFields);
$categoriesField->getConfig()->addComponent(new GridFieldToolbarHeader());
$categoriesField->getConfig()->addComponent(new GridFieldAddNewButton('toolbar-header-right'));
$categoriesField->getConfig()->addComponent(new GridFieldEditButton());
$favGroupsField = new GridField('testgroupsfield', 'testgroupsfield', $person->FavouriteGroups());
/** @skipUpgrade */
$favGroupsField->getConfig()->addComponent(new GridFieldDetailForm($this, 'Form'));
$favGroupsField->getConfig()->addComponent(new GridFieldToolbarHeader());
$favGroupsField->getConfig()->addComponent(new GridFieldAddNewButton('toolbar-header-right'));
$favGroupsField->getConfig()->addComponent(new GridFieldEditButton());
$fields = new FieldList($categoriesField, $favGroupsField);
/** @skipUpgrade */
return new Form($this, 'Form', $fields, new FieldList());
}
}