public function Form()
 {
     // GridField lists categories for a specific person
     $person = GridFieldDetailFormTest_Person::get()->sort('FirstName')->First();
     $detailFields = singleton('GridFieldDetailFormTest_Category')->getCMSFields();
     $detailFields->addFieldsToTab('Root.Main', array(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());
 }
 /**
  * Builds an item edit form.  The arguments to getCMSFields() are the popupController and
  * popupFormName, however this is an experimental API and may change.
  *
  * @todo In the future, we will probably need to come up with a tigher object representing a partially
  * complete controller with gaps for extra functionality.  This, for example, would be a better way
  * of letting Security/login put its log-in form inside a UI specified elsewhere.
  *
  * @return Form
  */
 public function ItemEditForm()
 {
     $list = $this->gridField->getList();
     if (empty($this->record)) {
         $controller = $this->getToplevelController();
         $url = $controller->getRequest()->getURL();
         $noActionURL = $controller->removeAction($url);
         $controller->getResponse()->removeHeader('Location');
         //clear the existing redirect
         return $controller->redirect($noActionURL, 302);
     }
     $canView = $this->record->canView();
     $canEdit = $this->record->canEdit();
     $canDelete = $this->record->canDelete();
     $canCreate = $this->record->canCreate();
     if (!$canView) {
         $controller = $this->getToplevelController();
         // TODO More friendly error
         return $controller->httpError(403);
     }
     // Build actions
     $actions = $this->getFormActions();
     // If we are creating a new record in a has-many list, then
     // pre-populate the record's foreign key.
     if ($list instanceof HasManyList && !$this->record->isInDB()) {
         $key = $list->getForeignKey();
         $id = $list->getForeignID();
         $this->record->{$key} = $id;
     }
     $fields = $this->component->getFields();
     if (!$fields) {
         $fields = $this->record->getCMSFields();
     }
     // If we are creating a new record in a has-many list, then
     // Disable the form field as it has no effect.
     if ($list instanceof HasManyList) {
         $key = $list->getForeignKey();
         if ($field = $fields->dataFieldByName($key)) {
             $fields->makeFieldReadonly($field);
         }
     }
     // Caution: API violation. Form expects a Controller, but we are giving it a RequestHandler instead.
     // Thanks to this however, we are able to nest GridFields, and also access the initial Controller by
     // dereferencing GridFieldDetailForm_ItemRequest->getController() multiple times. See getToplevelController
     // below.
     $form = new Form($this, 'ItemEditForm', $fields, $actions, $this->component->getValidator());
     $form->loadDataFrom($this->record, $this->record->ID == 0 ? Form::MERGE_IGNORE_FALSEISH : Form::MERGE_DEFAULT);
     if ($this->record->ID && !$canEdit) {
         // Restrict editing of existing records
         $form->makeReadonly();
         // Hack to re-enable delete button if user can delete
         if ($canDelete) {
             $form->Actions()->fieldByName('action_doDelete')->setReadonly(false);
         }
     } elseif (!$this->record->ID && !$canCreate) {
         // Restrict creation of new records
         $form->makeReadonly();
     }
     // Load many_many extraData for record.
     // Fields with the correct 'ManyMany' namespace need to be added manually through getCMSFields().
     if ($list instanceof ManyManyList) {
         $extraData = $list->getExtraData('', $this->record->ID);
         $form->loadDataFrom(array('ManyMany' => $extraData));
     }
     // TODO Coupling with CMS
     $toplevelController = $this->getToplevelController();
     if ($toplevelController && $toplevelController instanceof LeftAndMain) {
         // Always show with base template (full width, no other panels),
         // regardless of overloaded CMS controller templates.
         // TODO Allow customization, e.g. to display an edit form alongside a search form from the CMS controller
         $form->setTemplate(['type' => 'Includes', 'SilverStripe\\Admin\\LeftAndMain_EditForm']);
         $form->addExtraClass('cms-content cms-edit-form center fill-height flexbox-area-grow');
         $form->setAttribute('data-pjax-fragment', 'CurrentForm Content');
         if ($form->Fields()->hasTabSet()) {
             $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
             $form->addExtraClass('cms-tabset');
         }
         $form->Backlink = $this->getBackLink();
     }
     $cb = $this->component->getItemEditFormCallback();
     if ($cb) {
         $cb($form, $this);
     }
     $this->extend("updateItemEditForm", $form);
     return $form;
 }