Created
November 26, 2014 07:31
-
-
Save briceburg/9a11858be7bc34fe128d to your computer and use it in GitHub Desktop.
overriding GridFieldAddNewMultiClass ItemRequestClass to set some default ManyMany values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class GridFieldConfig_FlexiForm extends GridFieldConfig { | |
public static function include_requirements() { | |
$moduleDir = self::get_module_dir(); | |
Requirements::css($moduleDir.'/css/flexiforms.css'); | |
} | |
public static function get_module_dir() { | |
return basename(dirname(__DIR__)); | |
} | |
public function __construct() { | |
$this->addComponent(new GridFieldButtonRow('before')); | |
$this->addComponent(new GridFieldAddNewMultiClass('buttons-before-left')); | |
$this->addComponent(new GridFieldAddExistingAutocompleter('buttons-before-right')); | |
$this->addComponent(new GridFieldToolbarHeader()); | |
$this->addComponent(new GridFieldTitleHeader()); | |
$this->addComponent(new GridFieldEditableColumns()); | |
$this->addComponent(new GridFieldEditButton()); | |
$this->addComponent(new GridFieldDeleteAction(true)); | |
$this->addComponent(new GridFieldDetailForm()); | |
$component = $this->getComponentByType('GridFieldAddNewMultiClass'); | |
$component->setItemRequestClass('GridFieldDetailForm_FlexiFormRequest'); | |
self::include_requirements(); | |
} | |
} | |
class GridFieldDetailForm_FlexiFormRequest extends GridFieldAddNewMultiClassHandler { | |
public function doSave($data, $form) { | |
$new_record = $this->record->ID == 0; | |
if($new_record) { | |
$data['ManyMany'] = array( | |
'Name' => $data['FieldName'], | |
'DefaultValue' => $data['FieldDefaultValue'] | |
); | |
} | |
parent::doSave($data,$form); | |
} | |
} | |
.... | |
class FlexiForm extends Page | |
{ | |
protected $flexiform_tab = 'Root.Form'; | |
private static $db = array(); | |
private static $has_many = array( | |
'Submissions' | |
); | |
private static $many_many = array( | |
'Fields' => 'FlexiFormField' | |
); | |
private static $many_many_extraFields = array( | |
'Fields' => array( | |
'Name' => 'Varchar', | |
'Prompt' => 'Varchar', | |
'DefaultValue' => 'Varchar', | |
'Required' => 'Boolean', | |
'SortOrder' => 'Int' | |
) | |
); | |
protected $allowed_field_types = array( | |
'FlexiFormTextField', | |
'FlexiFormEmailField', | |
'FlexiFormDropdownField', | |
'FlexiFormCheckboxField', | |
'FlexiFormRadioField', | |
'FlexiFormCheckboxSetField' | |
); | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment