Skip to content

Commit 913c74e

Browse files
committed
API Unify search filter functionality across the CMS
CMSMain had different search filter code paths than the grid field search filter, and there was a lot of duplication of code. This commit works alongside a commit in silverstripe/silverstripe-cms to unify those code paths
1 parent cd29f81 commit 913c74e

22 files changed

+718
-339
lines changed

src/Forms/Form.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ public function __construct(
308308
}
309309

310310
// Form error controls
311-
$this->restoreFormState();
311+
if ($controller) {
312+
$this->restoreFormState();
313+
}
312314

313315
// Check if CSRF protection is enabled, either on the parent controller or from the default setting. Note that
314316
// method_exists() is used as some controllers (e.g. GroupTest) do not always extend from Object.
@@ -1790,6 +1792,21 @@ public function removeExtraClass($class)
17901792
return $this;
17911793
}
17921794

1795+
public function __clone(): void
1796+
{
1797+
$this->fields = clone $this->fields;
1798+
$this->actions = clone $this->actions;
1799+
if ($this->validator) {
1800+
$this->setValidator(clone $this->validator);
1801+
}
1802+
foreach ($this->fields as $field) {
1803+
$field->setForm($this);
1804+
}
1805+
foreach ($this->actions as $action) {
1806+
$action->setForm($this);
1807+
}
1808+
}
1809+
17931810
public function debug(): string
17941811
{
17951812
$class = static::class;

src/Forms/FormRequestHandler.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
use BadMethodCallException;
66
use SilverStripe\Control\Controller;
7-
use SilverStripe\Control\Director;
87
use SilverStripe\Control\HTTPRequest;
98
use SilverStripe\Control\HTTPResponse;
109
use SilverStripe\Control\HTTPResponse_Exception;
1110
use SilverStripe\Control\RequestHandler;
1211
use SilverStripe\Core\ClassInfo;
13-
use SilverStripe\Core\Convert;
1412
use SilverStripe\Core\Validation\ValidationResult;
1513
use SilverStripe\Model\List\SS_List;
1614
use SilverStripe\Core\Validation\ValidationException;
15+
use SilverStripe\Forms\Schema\FormSchema;
1716

1817
class FormRequestHandler extends RequestHandler
1918
{
@@ -27,6 +26,7 @@ class FormRequestHandler extends RequestHandler
2726
* @var array
2827
*/
2928
private static $allowed_actions = [
29+
'getSchema',
3030
'handleField',
3131
'httpSubmission',
3232
'forTemplate',
@@ -37,6 +37,7 @@ class FormRequestHandler extends RequestHandler
3737
* @var array
3838
*/
3939
private static $url_handlers = [
40+
'GET schema' => 'getSchema',
4041
'field/$FieldName!' => 'handleField',
4142
'POST ' => 'httpSubmission',
4243
'GET ' => 'httpSubmission',
@@ -67,6 +68,18 @@ public function __construct(Form $form)
6768
}
6869
}
6970

71+
/**
72+
* Gets a JSON schema representing a form.
73+
*/
74+
public function getSchema(HTTPRequest $request): HTTPResponse
75+
{
76+
$schemaID = $request->getURL();
77+
$parts = $request->getHeader(FormSchema::SCHEMA_HEADER);
78+
$data = FormSchema::singleton()->getMultipartSchema($parts, $schemaID, $this->form);
79+
$response = HTTPResponse::create(json_encode($data));
80+
$response->addHeader('Content-Type', 'application/json');
81+
return $response;
82+
}
7083

7184
/**
7285
* Get link for this form

src/Forms/GridField/GridField.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ public function performReadonlyTransformation()
275275
$hadEditButton = $copyConfig->getComponentByType(GridFieldEditButton::class) !== null;
276276

277277
// get the whitelist for allowable readonly components
278-
$allowedComponents = $this->getReadonlyComponents();
279-
foreach ($this->getConfig()->getComponents() as $component) {
278+
$allowedComponents = $copy->getReadonlyComponents();
279+
foreach ($copy->getConfig()->getComponents() as $component) {
280280
// if a component doesn't exist, remove it from the readonly version.
281281
if (!in_array(get_class($component), $allowedComponents ?? [])) {
282282
$copyConfig->removeComponent($component);
@@ -297,7 +297,7 @@ public function performReadonlyTransformation()
297297
}
298298
}
299299

300-
$copy->extend('afterPerformReadonlyTransformation', $this);
300+
$copy->extend('afterPerformReadonlyTransformation', $copy);
301301

302302
return $copy;
303303
}
@@ -1346,6 +1346,11 @@ public function saveInto(DataObjectInterface $record)
13461346
}
13471347
}
13481348

1349+
public function __clone(): void
1350+
{
1351+
$this->config = clone $this->config;
1352+
}
1353+
13491354
/**
13501355
* @param array $content
13511356
*

src/Forms/GridField/GridFieldConfig.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,13 @@ public function getComponentByType($type)
155155
}
156156
return null;
157157
}
158+
159+
public function __clone(): void
160+
{
161+
$components = $this->components;
162+
$this->components = ArrayList::create();
163+
foreach ($components as $component) {
164+
$this->components->add(clone $component);
165+
}
166+
}
158167
}

0 commit comments

Comments
 (0)