-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API Provide a way to register link forms dynamically for ModalController #1860
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ public function setFormSchema(FormSchema $schema): static | |
} | ||
|
||
/** | ||
* Gets a JSON schema representing the current edit form. | ||
* Gets a JSON schema representing a form. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated change but the form isn't necessarily (or even usually) the "current edit form" (implying main record edit form which isn't in a modal most of the time). |
||
*/ | ||
public function schema(HTTPRequest $request): HTTPResponse | ||
{ | ||
|
@@ -103,16 +103,6 @@ public function schema(HTTPRequest $request): HTTPResponse | |
return $this->getSchemaResponse($schemaID, $form); | ||
} | ||
|
||
/** | ||
* Check if the current request has a X-Formschema-Request header set. | ||
* Used by conditional logic that responds to validation results | ||
*/ | ||
protected function getSchemaRequested(): bool | ||
{ | ||
$parts = $this->getRequest()->getHeader(static::SCHEMA_HEADER); | ||
return !empty($parts); | ||
} | ||
Comment on lines
-106
to
-114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has just been moved further down the file so it's not above the public method |
||
|
||
/** | ||
* Generate schema for the given form based on the X-Formschema-Request header value | ||
* | ||
|
@@ -121,7 +111,7 @@ protected function getSchemaRequested(): bool | |
* @param ValidationResult $errors Required for 'error' response | ||
* @param array $extraData Any extra data to be merged with the schema response | ||
*/ | ||
protected function getSchemaResponse(string $schemaID, ?Form $form = null, ValidationResult $errors = null, array $extraData = []): HTTPResponse | ||
public function getSchemaResponse(string $schemaID, ?Form $form = null, ValidationResult $errors = null, array $extraData = []): HTTPResponse | ||
Comment on lines
-124
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs to be public to avoid code duplication. See silverstripe/silverstripe-asset-admin#1515 |
||
{ | ||
$parts = $this->getRequest()->getHeader(static::SCHEMA_HEADER); | ||
$data = $this | ||
|
@@ -137,6 +127,16 @@ protected function getSchemaResponse(string $schemaID, ?Form $form = null, Valid | |
return $response; | ||
} | ||
|
||
/** | ||
* Check if the current request has a X-Formschema-Request header set. | ||
* Used by conditional logic that responds to validation results | ||
*/ | ||
protected function getSchemaRequested(): bool | ||
{ | ||
$parts = $this->getRequest()->getHeader(static::SCHEMA_HEADER); | ||
return !empty($parts); | ||
} | ||
|
||
private function prepareTinyMce(): void | ||
{ | ||
// Set the members html editor config | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,13 @@ | |
|
||
namespace SilverStripe\Admin; | ||
|
||
use LogicException; | ||
use SilverStripe\Admin\Forms\EditorEmailLinkFormFactory; | ||
use SilverStripe\Admin\Forms\EditorExternalLinkFormFactory; | ||
use SilverStripe\Admin\Forms\LinkFormFactory; | ||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\Control\HTTPResponse; | ||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\Forms\Form; | ||
|
||
/** | ||
|
@@ -13,48 +18,77 @@ class ModalController extends FormSchemaController | |
{ | ||
private static ?string $url_segment = 'modals'; | ||
|
||
private static $allowed_actions = [ | ||
'EditorExternalLink', | ||
'EditorEmailLink', | ||
private static string $required_permission_codes = 'CMS_ACCESS'; | ||
|
||
private static array $allowed_actions = [ | ||
'linkModalForm', | ||
'linkModalFormSchema', | ||
]; | ||
|
||
private static string $required_permission_codes = 'CMS_ACCESS'; | ||
private static array $url_handlers = [ | ||
'linkModalForm/$ModalName/$ItemID' => 'linkModalForm', | ||
'GET linkModalFormSchema/$ModalName/$ItemID' => 'linkModalFormSchema', | ||
]; | ||
|
||
/** | ||
* Associative array of modal form names to form factory classes. | ||
* Used primarily to register modal form factories for use in the WYSIWYG link plugin. | ||
* Form factories must subclass LinkFormFactory | ||
*/ | ||
private static array $link_modal_form_factories = [ | ||
'EditorExternalLink' => EditorExternalLinkFormFactory::class, | ||
'EditorEmailLink' => EditorEmailLinkFormFactory::class, | ||
]; | ||
|
||
/** | ||
* Builds and returns the external link form | ||
* | ||
* @return Form | ||
* @deprecated 2.4.0 Will be replaced with linkModalForm() | ||
* Get a link modal form built from a factory. | ||
* Intended to be used in conjunction with linkModalFormSchema() | ||
*/ | ||
public function EditorExternalLink(): Form | ||
public function linkModalForm(HTTPRequest $request): Form | ||
{ | ||
Deprecation::noticeWithNoReplacment('2.4.0', 'Will be replaced with linkModalForm()'); | ||
$modalName = $request->param('ModalName'); | ||
$itemID = $request->param('ItemID'); | ||
if ($modalName === null || $itemID === null) { | ||
$this->jsonError(400, 'Missing request params'); | ||
} | ||
$modalForms = static::config()->get('link_modal_form_factories'); | ||
if (!array_key_exists($modalName, $modalForms)) { | ||
$this->httpError(400); | ||
} | ||
Comment on lines
+51
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
// Show link text field if requested | ||
$showLinkText = $this->getRequest()->getVar('requireLinkText'); | ||
$factory = EditorExternalLinkFormFactory::singleton(); | ||
return $factory->getForm( | ||
$class = $modalForms[$modalName]; | ||
if (!is_a($class, LinkFormFactory::class, true)) { | ||
throw new LogicException("Factory for '$modalName' must be a subclass of " . LinkFormFactory::class); | ||
} | ||
|
||
// Build the form | ||
/** @var LinkFormFactory $factory */ | ||
$factory = Injector::inst()->get($class); | ||
$form = $factory->getForm( | ||
$this, | ||
__FUNCTION__, | ||
[ 'RequireLinkText' => isset($showLinkText) ] | ||
'linkModalForm/'.$modalName, | ||
[ | ||
'RequireLinkText' => isset($showLinkText), | ||
'ItemID' => $itemID, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
] | ||
); | ||
// Set url handler that handles ItemID param correctly | ||
$form->setRequestHandler( | ||
LeftAndMainFormRequestHandler::create($form, [$itemID]) | ||
); | ||
return $form; | ||
} | ||
|
||
/** | ||
* Builds and returns the external link form | ||
* | ||
* @return Form | ||
* @deprecated 2.4.0 Will be replaced with linkModalForm() | ||
* Gets a JSON schema representing a link modal form. | ||
* Links to this must include the ID of the current record or 0 | ||
* e.g ModalController::singleton()->Link('linkModalFormSchema/myModalForm/:pageid') | ||
*/ | ||
public function EditorEmailLink(): Form | ||
public function linkModalFormSchema(HTTPRequest $request): HTTPResponse | ||
{ | ||
Deprecation::noticeWithNoReplacment('2.4.0', 'Will be replaced with linkModalForm()'); | ||
// Show link text field if requested | ||
$showLinkText = $this->getRequest()->getVar('requireLinkText'); | ||
$factory = EditorEmailLinkFormFactory::singleton(); | ||
return $factory->getForm( | ||
$this, | ||
__FUNCTION__, | ||
[ 'RequireLinkText' => isset($showLinkText) ] | ||
); | ||
$form = $this->linkModalForm($request); | ||
$schemaID = $request->getURL(); | ||
return $this->getSchemaResponse($schemaID, $form); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most link forms don't actually need the current page, so the JS plugins don't pass it through. In those cases
0
is sufficient.Without this it'd be
undefined
which gets included as a string in the URL which causes errors.The alternative would be to update the other link plugins to include the actual ID but a) the way the anchor link plugin grabs it right now feels a little flakey and b) they don't need it so there's no need to add that functionality for them.