/**
  * (non-PHPdoc)
  *
  * @see SiteTree::getCMSFields()
  */
 public function getCMSFields()
 {
     // Get the fields from the parent implementation
     $fields = parent::getCMSFields();
     // List of available fields in the page
     $groupFields = $this->EditableFieldGroup()->Fields();
     $list = $this->Fields()->addMany($groupFields)->sort('Sort', 'ASC');
     // Add tab to edit fields values
     $this->buildPageFieldsTab($list, $fields);
     // GridField for managing page specific fields
     $config = GridFieldConfig_RelationEditor::create();
     $config->getComponentByType('GridFieldPaginator')->setItemsPerPage(10);
     $config->removeComponentsByType('GridFieldAddNewButton');
     $config->removeComponentsByType('GridFieldEditButton');
     $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(['Name' => _t('ConfigurablePage.NAME', 'Name'), 'Title' => _t('ConfigurablePage.TITLE', 'Title'), 'Sort' => _t('ConfigurablePage.SORT', 'Sort'), 'Group' => _t('ConfigurablePage.GROUP', 'Group')]);
     $config->addComponent(new GridFieldEditableManyManyExtraColumns(['Sort' => 'Int']), 'GridFieldEditButton');
     $config->getComponentByType('GridFieldDataColumns')->setFieldFormatting(['Group' => function ($value) {
         return !$value ? '' : $this->EditableFieldGroup()->Title;
     }]);
     $fieldsField = new GridField('Fields', 'Fields', $list, $config);
     // Drop-down list of editable field groups
     $groups = EditableFieldGroup::get()->map();
     $groups->unshift('', '');
     $groupsField = new DropdownField("EditableFieldGroupID", _t('ConfigurablePage.FIELDGROUP', 'Editable field group'), $groups);
     $groupsField->setDescription(_t('ConfigurablePage.FIELDGROUP_HELP', 'Select a group to load its collection of fields in the current page. ' . 'You need to click save to update the page fields.'));
     // Add fields to manage page fields tab
     $fields->addFieldToTab('Root.ManagePageFields', $groupsField);
     $fields->addFieldToTab('Root.ManagePageFields', $fieldsField);
     // JS & CSS for the gridfield sort column
     Requirements::javascript('configurablepage/javascript/ConfigurablePage.js');
     Requirements::css('configurablepage/css/ConfigurablePage.css');
     return $fields;
 }
 public function onAfterWrite()
 {
     parent::onAfterWrite();
     // If this is not attached to a group, find the first group prior to this
     // with no end attached
     $group = $this->Group();
     if (!($group && $group->exists()) && $this->ParentID) {
         $group = EditableFieldGroup::get()->filter(array('ParentID' => $this->ParentID, 'Sort:LessThanOrEqual' => $this->Sort))->where('"EditableFieldGroup"."EndID" IS NULL OR "EditableFieldGroup"."EndID" = 0')->sort('"Sort" DESC')->first();
         // When a group is found, attach it to this end
         if ($group) {
             $group->EndID = $this->ID;
             $group->write();
         }
     }
 }
 /**
  * The content of the main section of the page
  * List current form fields and allow user to modify their data
  *
  * @see LeftAndMain::getEditForm()
  */
 public function getEditForm($id = null, $fields = null)
 {
     // Save button
     $actions = new FieldList(FormAction::create('save', _t('CMSMain.SAVE', 'Save'))->setUseButtonTag(true)->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept'));
     // Form tabs
     $fieldsTab = new Tab('Fields', _t('EditableFieldAdmin.Fields', 'Fields'));
     $groupsTab = new Tab('Groups', singleton('Group')->i18n_plural_name());
     // Form tab container
     $tabSet = new TabSet('Root', $fieldsTab, $groupsTab);
     $tabSet->setTemplate('CMSTabSet');
     // Activate tab based on request param
     $actionParam = $this->request->param('Action');
     if ($actionParam == 'fields') {
         $fieldsTab->addExtraClass('ui-state-active');
     } elseif ($actionParam == 'groups') {
         $groupsTab->addExtraClass('ui-state-active');
     }
     // Form field list
     $fields = new FieldList([$tabSet]);
     // Add field to first tab
     $fields->addFieldToTab('Root.Fields', new EditableFieldEditor("Fields", 'Fields', ""));
     // Add field to second tab
     $groupsConfig = GridFieldConfig_RecordEditor::create();
     $groupsField = GridField::create('EditableFieldGroup', singleton('Group')->i18n_plural_name(), EditableFieldGroup::get(), $groupsConfig);
     $component = $groupsConfig->getComponentByType('GridFieldAddNewButton');
     $component->setButtonName(_t('EditableFieldAdmin.AddGroup', 'Add Group'));
     $fields->addFieldToTab('Root.Groups', $groupsField);
     // The edit form
     $form = CMSForm::create($this, 'EditForm', $fields, $actions)->setHTMLID('Form_EditForm');
     $form->loadDataFrom($this);
     // Render correct responses on validation errors
     $form->setResponseNegotiator($this->getResponseNegotiator());
     $form->disableDefaultAction();
     // Form layout
     $form->addExtraClass('cms-content cms-edit-form center ss-tabset cms-tabset ' . $this->BaseCSSClasses());
     $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
     $form->setAttribute('data-pjax-fragment', 'CurrentForm');
     // Allow decorators to modify the form
     $this->extend('updateEditForm', $form);
     return $form;
 }