Enable namespace support for GridFieldAddNewMultiClass

This commit is contained in:
Nik Rolls 2017-04-13 12:12:52 +12:00 committed by Nathan Glasl
parent 5c1f9cca01
commit 755cafa754
2 changed files with 84 additions and 2 deletions

View File

@ -122,7 +122,12 @@ class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URL
} }
} }
return $result; $sanitised = array();
foreach($result as $class=>$title) {
$sanitised[$this->sanitiseClassName($class)] = $title;
}
return $sanitised;
} }
/** /**
@ -168,8 +173,9 @@ class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URL
throw new SS_HTTPResponse_Exception(400); throw new SS_HTTPResponse_Exception(400);
} }
$unsanitisedClass = $this->unsanitiseClassName($class);
$handler = Object::create($this->itemRequestClass, $handler = Object::create($this->itemRequestClass,
$grid, $component, new $class(), $grid->getForm()->getController(), 'add-multi-class' $grid, $component, new $unsanitisedClass(), $grid->getForm()->getController(), 'add-multi-class'
); );
$handler->setTemplate($component->getTemplate()); $handler->setTemplate($component->getTemplate());
@ -219,4 +225,20 @@ class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URL
$this->itemRequestClass = $class; $this->itemRequestClass = $class;
return $this; return $this;
} }
/**
* Sanitise a model class' name for inclusion in a link
* @return string
*/
protected function sanitiseClassName($class) {
return str_replace('\\', '-', $class);
}
/**
* Unsanitise a model class' name from a URL param
* @return string
*/
protected function unsanitiseClassName($class) {
return str_replace('-', '\\', $class);
}
} }

View File

@ -0,0 +1,60 @@
<?php
namespace SilverStripeAustralia\Test;
use SapphireTest;
use SS_HTTPRequest;
use Form, Controller, FieldList;
use GridField, GridFieldDetailForm, GridFieldAddNewMultiClass;
class GridFieldAddNewMultiClassWithNamespacesTest extends SapphireTest {
public function testGetClassesWithNamespaces() {
$grid = new GridField('TestGridField');
$grid->setModelClass('SilverStripeAustralia\\Test\\NamespacedClass');
$component = new GridFieldAddNewMultiClass();
$this->assertEquals(
array(
'SilverStripeAustralia-Test-NamespacedClass' => 'NamespacedClass'
),
$component->getClasses($grid),
'Namespaced classes are sanitised'
);
}
public function testHandleAddWithNamespaces() {
$grid = new GridField('TestGridField');
$grid->getConfig()->addComponent(new GridFieldDetailForm());
$grid->setModelClass('SilverStripeAustralia\\Test\\NamespacedClass');
$grid->setForm(Form::create('test', Controller::create(), FieldList::create(), FieldList::create()));
$request = new SS_HTTPRequest('POST', 'test');
$request->setRouteParams(array('ClassName' => 'SilverStripeAustralia-Test-NamespacedClass'));
$component = new GridFieldAddNewMultiClass();
$response = $component->handleAdd($grid, $request);
$record = new \ReflectionProperty('GridFieldAddNewMultiClassHandler', 'record');
$record->setAccessible(true);
$this->assertInstanceOf('SilverStripeAustralia\\Test\\NamespacedClass', $record->getValue($response));
}
}
/**#@+
* @ignore
*/
class NamespacedClass {
public function i18n_singular_name() {
return 'NamespacedClass';
}
public function canCreate() {
return true;
}
}
/**#@-*/