Skip to content

Commit

Permalink
API Avoid reimplementing logic from ModalController
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 26, 2024
1 parent d478944 commit 4fb0eac
Showing 1 changed file with 3 additions and 47 deletions.
50 changes: 3 additions & 47 deletions code/Extensions/RemoteFileModalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
*
Expand All @@ -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(
Expand All @@ -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());
Expand All @@ -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;
}
}

0 comments on commit 4fb0eac

Please sign in to comment.