From 4fb0eacf1fadb128df7400e77512aa2f449210aa Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 25 Nov 2024 17:23:39 +1300 Subject: [PATCH] API Avoid reimplementing logic from ModalController --- code/Extensions/RemoteFileModalExtension.php | 50 ++------------------ 1 file changed, 3 insertions(+), 47 deletions(-) diff --git a/code/Extensions/RemoteFileModalExtension.php b/code/Extensions/RemoteFileModalExtension.php index 1c94b5969..59293d65d 100644 --- a/code/Extensions/RemoteFileModalExtension.php +++ b/code/Extensions/RemoteFileModalExtension.php @@ -4,19 +4,15 @@ use Embed\Http\NetworkException; use Embed\Http\RequestException; -use SilverStripe\Admin\FormSchemaController; -use SilverStripe\Admin\LeftAndMain; use SilverStripe\Admin\ModalController; use SilverStripe\AssetAdmin\Forms\RemoteFileFormFactory; use SilverStripe\AssetAdmin\Exceptions\InvalidRemoteUrlException; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; -use SilverStripe\Core\Convert; use SilverStripe\Core\Extension; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; -use SilverStripe\Forms\Schema\FormSchema; use SilverStripe\Core\Validation\ValidationResult; /** @@ -32,22 +28,6 @@ class RemoteFileModalExtension extends Extension 'remoteEditFormSchema', ); - /** - * @return HTTPRequest - */ - protected function getRequest() - { - return $this->getOwner()->getRequest(); - } - - /** - * @return FormSchema - */ - protected function getFormSchema() - { - return FormSchema::singleton(); - } - /** * Form for creating a new OEmbed object in the WYSIWYG, used by the InsertEmbedModal component * @@ -70,7 +50,7 @@ public function remoteCreateForm() */ public function remoteEditForm() { - $url = $this->getRequest()->requestVar('embedurl'); + $url = $this->getOwner()->getRequest()->requestVar('embedurl'); $form = null; $form = Injector::inst()->get(RemoteFileFormFactory::class) ->getForm( @@ -92,7 +72,7 @@ public function remoteEditFormSchema(HTTPRequest $request) $schemaID = $request->getURL(); try { $form = $this->remoteEditForm(); - return $this->getSchemaResponse($schemaID, $form); + return $this->getOwner()->getSchemaResponse($schemaID, $form); } catch (NetworkException | RequestException | InvalidRemoteUrlException $exception) { $errors = ValidationResult::create() ->addError($exception->getMessage()); @@ -104,33 +84,9 @@ public function remoteEditFormSchema(HTTPRequest $request) } return $this + ->getOwner() ->getSchemaResponse($schemaID, $form, $errors) ->setStatusCode($code); } } - - /** - * Generate schema for the given form based on the X-Formschema-Request header value - * - * @param string $schemaID ID for this schema. Required. - * @param Form $form Required for 'state' or 'schema' response - * @param ValidationResult $errors Required for 'error' response - * @param array $extraData Any extra data to be merged with the schema response - * @return HTTPResponse - */ - protected function getSchemaResponse($schemaID, $form = null, ValidationResult $errors = null, $extraData = []) - { - $parts = $this->getRequest()->getHeader(FormSchemaController::SCHEMA_HEADER); - $data = $this - ->getFormSchema() - ->getMultipartSchema($parts, $schemaID, $form, $errors); - - if ($extraData) { - $data = array_merge($data, $extraData); - } - - $response = new HTTPResponse(json_encode($data)); - $response->addHeader('Content-Type', 'application/json'); - return $response; - } }