diff --git a/code/extensions/UserFormFieldEditorExtension.php b/code/extensions/UserFormFieldEditorExtension.php index 505c305..ec8fec1 100644 --- a/code/extensions/UserFormFieldEditorExtension.php +++ b/code/extensions/UserFormFieldEditorExtension.php @@ -57,7 +57,7 @@ class UserFormFieldEditorExtension extends DataExtension { ->addComponents( $editableColumns, new GridFieldButtonRow(), - GridFieldAddClassesButton::create('EditableFormField') + GridFieldAddClassesButton::create('EditableTextField') ->setButtonName(_t('UserFormFieldEditorExtension.ADD_FIELD', 'Add Field')) ->setButtonClass('ss-ui-action-constructive'), GridFieldAddClassesButton::create('EditableFormStep') diff --git a/code/forms/GridFieldAddClassesButton.php b/code/forms/GridFieldAddClassesButton.php index 5810fe0..bd955e8 100644 --- a/code/forms/GridFieldAddClassesButton.php +++ b/code/forms/GridFieldAddClassesButton.php @@ -3,15 +3,11 @@ /** * A button which allows objects to be created with a specified classname(s) */ -class GridFieldAddClassesButton extends Object implements GridField_HTMLProvider, GridField_URLHandler { +class GridFieldAddClassesButton extends Object implements GridField_HTMLProvider, GridField_ActionProvider { - private static $allowed_actions = array( - 'handleAdd' - ); - /** * Name of fragment to insert into - * + * * @var string */ protected $targetFragment; @@ -127,7 +123,7 @@ class GridFieldAddClassesButton extends Object implements GridField_HTMLProvider */ public function getClassesCreate($grid) { // Get explicit or fallback class list - $classes = $this->modelClasses; + $classes = $this->getClasses(); if(empty($classes) && $grid) { $classes = array($grid->getModelClass()); } @@ -149,7 +145,7 @@ class GridFieldAddClassesButton extends Object implements GridField_HTMLProvider } $this->modelClasses = $classes; } - + public function getHTMLFragments($grid) { // Check create permission $singleton = singleton($grid->getModelClass()); @@ -165,45 +161,59 @@ class GridFieldAddClassesButton extends Object implements GridField_HTMLProvider $buttonName = _t('GridField.Add', 'Add {name}', array('name' => $objectName)); } - $data = new ArrayData(array( - 'Title' => $this->getButtonName(), - 'ButtonClass' => $this->getButtonClass(), - 'Link' => Controller::join_links($grid->Link(), $this->getAction()) - )); + $addAction = new GridField_FormAction( + $grid, + $this->getAction(), + $buttonName, + $this->getAction(), + array() + ); + $addAction->setAttribute('data-icon', 'add'); + + if($this->getButtonClass()) { + $addAction->addExtraClass($this->getButtonClass()); + } return array( - $this->getFragment() => $data->renderWith(__CLASS__) + $this->targetFragment => $addAction->forTemplate() + ); + } + + /** + * {@inheritDoc} + */ + public function getActions($gridField) { + return array( + $this->getAction() ); } /** * Get the action suburl for this component * - * @return type + * @return string */ protected function getAction() { - $classes = implode('-', $this->getClasses()); - return Controller::join_links('add-classes', $classes); + return 'add-classes-' . strtolower(implode('-', $this->getClasses())); } - /** - * {@inheritDoc} - */ - public function getURLHandlers($grid) { - return array( - $this->getAction() => 'handleAdd' - ); + public function handleAction(GridField $gridField, $actionName, $arguments, $data) { + switch(strtolower($actionName)) { + case $this->getAction(): + return $this->handleAdd($gridField); + default: + return null; + } } /** * Handles adding a new instance of a selected class. * * @param GridField $grid - * @param SS_HTTPRequest $request + * @return null */ - public function handleAdd($grid, $request) { + public function handleAdd($grid) { $classes = $this->getClassesCreate($grid); - if(empty($classes)) { throw new SS_HTTPResponse_Exception(400); } @@ -216,10 +226,7 @@ class GridFieldAddClassesButton extends Object implements GridField_HTMLProvider $list->add($item); } - // Return directly to the gridfield again - return $grid - ->getForm() - ->getController() - ->redirectBack(); + // Should trigger a simple reload + return null; } } diff --git a/code/model/UserDefinedForm.php b/code/model/UserDefinedForm.php index 8f79f54..9f8e675 100755 --- a/code/model/UserDefinedForm.php +++ b/code/model/UserDefinedForm.php @@ -782,10 +782,10 @@ JS * Allows the use of field values in email body. * * @param ArrayList fields - * @return ViewableData + * @return ArrayData */ private function getMergeFieldsMap($fields = array()) { - $data = new ViewableData(); + $data = new ArrayData(array()); foreach ($fields as $field) { $data->setField($field->Name, DBField::create_field('Text', $field->Value)); diff --git a/code/model/editableformfields/EditableFormField.php b/code/model/editableformfields/EditableFormField.php index b02906c..db2f438 100755 --- a/code/model/editableformfields/EditableFormField.php +++ b/code/model/editableformfields/EditableFormField.php @@ -164,7 +164,7 @@ class EditableFormField extends DataObject { TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')), TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title')), SegmentField::create('Name')->setModifiers(array( - UnderscoreSegmentFieldModifier::create()->setDefault('Field'), + UnderscoreSegmentFieldModifier::create()->setDefault('FieldName'), DisambiguationSegmentFieldModifier::create(), ))->setPreview($this->Name) ) @@ -277,6 +277,10 @@ class EditableFormField extends DataObject { public function onBeforeWrite() { parent::onBeforeWrite(); + if($this->Name === 'Field') { + throw new ValidationException('Field name cannot be "Field"'); + } + if(!$this->Sort && $this->ParentID) { $parentID = $this->ParentID; $this->Sort = EditableFormField::get() @@ -772,6 +776,7 @@ class EditableFormField extends DataObject { $editableFieldClasses[$class] = $singleton->i18n_singular_name(); } + asort($editableFieldClasses); return $editableFieldClasses; } } diff --git a/css/UserForm_cms.css b/css/UserForm_cms.css index 8c2ff75..00a77a0 100644 --- a/css/UserForm_cms.css +++ b/css/UserForm_cms.css @@ -1,10 +1,6 @@ /** * Styles for cms */ -.cms .uf-field-editor > div.ss-gridfield-buttonrow-before { - margin-bottom: 10px; - overflow: auto; -} .cms .uf-field-editor table.ss-gridfield-table .ss-gridfield-item, .cms .uf-field-editor table.ss-gridfield-table .ss-gridfield-item:hover { background: white; } diff --git a/scss/UserForm_cms.scss b/scss/UserForm_cms.scss index 233314c..f780fc9 100644 --- a/scss/UserForm_cms.scss +++ b/scss/UserForm_cms.scss @@ -6,11 +6,6 @@ .uf-field-editor { - > div.ss-gridfield-buttonrow-before { - margin-bottom: 10px; - overflow: auto; - } - // Row styles table.ss-gridfield-table { diff --git a/templates/gridfield/GridFieldAddClassesButton.ss b/templates/gridfield/GridFieldAddClassesButton.ss deleted file mode 100644 index aabfe88..0000000 --- a/templates/gridfield/GridFieldAddClassesButton.ss +++ /dev/null @@ -1,3 +0,0 @@ - - $Title.XML - \ No newline at end of file