From e3508d41d520983262e2fba28f7b6db0785e6d72 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Mon, 26 Aug 2024 10:56:15 +1200 Subject: [PATCH] NEW Provide a standardised CMSEditLink method (#11338) --- src/Forms/GridField/GridFieldDetailForm.php | 8 +++----- src/ORM/CMSPreviewable.php | 4 ++-- src/ORM/DataObject.php | 20 +++++++++++++++++++ .../GridFieldDetailFormTest/Person.php | 2 +- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Forms/GridField/GridFieldDetailForm.php b/src/Forms/GridField/GridFieldDetailForm.php index e776f333daa..624276eb42e 100644 --- a/src/Forms/GridField/GridFieldDetailForm.php +++ b/src/Forms/GridField/GridFieldDetailForm.php @@ -175,7 +175,7 @@ protected function getRecordFromRequest(GridField $gridField, HTTPRequest $reque /** * Try and find another URL at which the given record can be edited. - * If redirectMissingRecords is true and the record has a CMSEditLink method, that value will be returned. + * If redirectMissingRecords is true and the record has a getCMSEditLink method, that value will be returned. * This only works when the list passed to the GridField is a {@link DataList}. * * @param $gridField The current GridField @@ -203,9 +203,7 @@ public function getLostRecordRedirection(GridField $gridField, HTTPRequest $requ } $existing = DataObject::get($list->dataClass())->byID($id); - if ($existing && $existing->hasMethod('CMSEditLink')) { - $link = $existing->CMSEditLink(); - } + $link = $existing?->getCMSEditLink(); if ($link && $link == $request->getURL()) { throw new \LogicException(sprintf( @@ -282,7 +280,7 @@ public function getName() * Enable redirection to missing records. * * If a GridField shows a filtered list, and the record is not in the list but exists in the - * database, and the record has a CMSEditLink method, then the system will redirect to the + * database, and the record has a getCMSEditLink method, then the system will redirect to the * URL returned by that method. */ public function setRedirectMissingRecords(bool $redirectMissingRecords): GridFieldDetailForm diff --git a/src/ORM/CMSPreviewable.php b/src/ORM/CMSPreviewable.php index 8f5a6d82369..a4bbdbb13b5 100644 --- a/src/ORM/CMSPreviewable.php +++ b/src/ORM/CMSPreviewable.php @@ -32,9 +32,9 @@ public function PreviewLink($action = null); public function getMimeType(); /** - * @return string Link to the CMS-author view. Should point to a + * @return string|null Link to the CMS-author view. Should point to a * controller subclassing {@link LeftAndMain}. Example: * http://mysite.com/admin/edit/6 */ - public function CMSEditLink(); + public function getCMSEditLink(): ?string; } diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index 3852d640c0c..43414fc301a 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -2692,6 +2692,25 @@ public function getViewerTemplates($suffix = '') return SSViewer::get_templates_by_class(static::class, $suffix, $this->baseClass()); } + /** + * Get the link for editing this record in the CMS. + */ + public function getCMSEditLink(): ?string + { + $link = null; + $this->extend('updateCMSEditLink', $link); + return $link; + } + + /** + * @deprecated 6.0.0 Use getCMSEditLink() instead + */ + public function CMSEditLink() + { + Deprecation::notice('6.0.0', 'Use getCMSEditLink() instead'); + return $this->getCMSEditLink(); + } + /** * Gets the value of a field. * Called by {@link __get()} and any getFieldName() methods you might create. @@ -4143,6 +4162,7 @@ public static function enable_subclass_access() */ private static $casting = [ "Title" => 'Text', + 'CMSEditLink' => 'Text', ]; /** diff --git a/tests/php/Forms/GridField/GridFieldDetailFormTest/Person.php b/tests/php/Forms/GridField/GridFieldDetailFormTest/Person.php index c51876527e7..c5f33364065 100644 --- a/tests/php/Forms/GridField/GridFieldDetailFormTest/Person.php +++ b/tests/php/Forms/GridField/GridFieldDetailFormTest/Person.php @@ -75,7 +75,7 @@ public function getCMSValidator() ); } - public function CMSEditLink() + public function getCMSEditLink(): ?string { return sprintf('my-admin/%d', $this->ID); }