From a8b6f668d3e6698fef4e48f730b6e18ab1a8688e Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Mon, 26 Aug 2024 09:58:24 +1200 Subject: [PATCH] API Change public CMSEditLink to protected updateCMSEditLink (#1809) --- code/CMSEditLinkExtension.php | 19 +++++++++++++------ code/Forms/UsedOnTable.php | 4 ++-- tests/php/CMSEditLinkExtensionTest.php | 10 +++++----- tests/php/Navigator/UnversionedRecord.php | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/code/CMSEditLinkExtension.php b/code/CMSEditLinkExtension.php index ce20bce31..55d34a177 100644 --- a/code/CMSEditLinkExtension.php +++ b/code/CMSEditLinkExtension.php @@ -31,7 +31,7 @@ * Note that the cms_edit_owner must implement a getCMSEditLinkForManagedDataObject() method. * * If the cms_edit_owner is a has_one relation, the class on the other end - * of the relation must have a CMSEditLink() method. + * of the relation must have a getCMSEditLink() method. * * @template T of LeftAndMain|DataObject * @extends Extension @@ -70,14 +70,21 @@ public function getCMSEditLinkForManagedDataObject(DataObject $obj, string $reci } /** - * Get a link to edit this DataObject in the CMS. + * Provide a link to edit this DataObject in the CMS if there isn't one already. + * @throws LogicException if a link cannot be established + * e.g. if the object is not in a has_many relation or not edited inside a GridField. */ - public function CMSEditLink(): string + protected function updateCMSEditLink(?string &$link): void { + // Don't update the link if it has already been established. + if ($link) { + return; + } + /** @var DataObject|LeftAndMain|null $owner */ $owner = $this->owner->getCMSEditOwner(); if (!$owner || !$owner->exists()) { - return ''; + return; } if (!$owner->hasMethod('getCMSEditLinkForManagedDataObject')) { @@ -89,7 +96,7 @@ public function CMSEditLink(): string } else { $relativeLink = $owner->getCMSEditLinkForManagedDataObject($this->owner); } - return Director::absoluteURL((string) $relativeLink); + $link = Director::absoluteURL($relativeLink); } private function getCMSEditLinkForRelation(array $componentConfig, DataObject $obj, string $reciprocalRelation, FieldList $fields): string @@ -143,7 +150,7 @@ private function constructLink(string $relation, int $id): string $ownerType = $this->owner->config()->get('cms_edit_owner'); $prefix = is_a($ownerType, CMSMain::class, true) ? 'field' : 'ItemEditForm/field'; return Controller::join_links( - $this->owner->CMSEditLink(), + $this->owner->getCMSEditLink(), $prefix, $relation, 'item', diff --git a/code/Forms/UsedOnTable.php b/code/Forms/UsedOnTable.php index 0521d3a35..53efa32bc 100644 --- a/code/Forms/UsedOnTable.php +++ b/code/Forms/UsedOnTable.php @@ -102,7 +102,7 @@ public function usage(HTTPRequest $request) 'id' => $dataObject->ID, 'title' => $dataObject->getTitle() ?: _t(__CLASS__ . '.UNTITLED', 'Untitled'), 'type' => ucfirst($dataObject->i18n_singular_name() ?? ''), - 'link' => $dataObject->hasMethod('CMSEditLink') ? $dataObject->CMSEditLink() : null, + 'link' => $dataObject->getCMSEditLink(), 'ancestors' => [] ]; @@ -115,7 +115,7 @@ public function usage(HTTPRequest $request) foreach ($ancestorDataObjects as $ancestorDataObject) { $tableRowData['ancestors'][] = [ 'title' => $ancestorDataObject->getTitle() ?: _t(__CLASS__ . '.UNTITLED', 'Untitled'), - 'link' => $ancestorDataObject->hasMethod('CMSEditLink') ? $ancestorDataObject->CMSEditLink() : null, + 'link' => $ancestorDataObject->getCMSEditLink(), ]; } $usageData[] = $tableRowData; diff --git a/tests/php/CMSEditLinkExtensionTest.php b/tests/php/CMSEditLinkExtensionTest.php index ed6a3a02f..0429af90a 100644 --- a/tests/php/CMSEditLinkExtensionTest.php +++ b/tests/php/CMSEditLinkExtensionTest.php @@ -72,7 +72,7 @@ public function testGetEditLinkForDataObjectException() $this->assertNull($root->getCMSEditLinkForManagedDataObject($nested, 'AnotherOfTheSameClass')); } - public function testCMSEditLink() + public function testGetCMSEditLink() { $root = $this->objFromFixture(ManagedDataObject::class, 'root'); $basicNested = $this->objFromFixture(BasicNestedObject::class, 'one'); @@ -80,18 +80,18 @@ public function testCMSEditLink() $polymorphic = $this->objFromFixture(PolymorphicNestedObject::class, 'one'); $rootUrl = "http://localhost/admin/cms-edit-test/belongsHere/EditForm/field/belongsHere/item/$root->ID"; - $this->assertSame($rootUrl, $root->CMSEditLink()); + $this->assertSame($rootUrl, $root->getCMSEditLink()); $this->assertSame( "$rootUrl/ItemEditForm/field/BasicNested/item/$basicNested->ID", - $basicNested->CMSEditLink() + $basicNested->getCMSEditLink() ); $this->assertSame( "$rootUrl/ItemEditForm/field/Nested/item/$nested->ID", - $nested->CMSEditLink() + $nested->getCMSEditLink() ); $this->assertSame( "$rootUrl/ItemEditForm/field/Polymorphic/item/$polymorphic->ID", - $polymorphic->CMSEditLink() + $polymorphic->getCMSEditLink() ); } } diff --git a/tests/php/Navigator/UnversionedRecord.php b/tests/php/Navigator/UnversionedRecord.php index 4efb68c58..5f34fb825 100644 --- a/tests/php/Navigator/UnversionedRecord.php +++ b/tests/php/Navigator/UnversionedRecord.php @@ -28,7 +28,7 @@ public function getMimeType() return 'text/html'; } - public function CMSEditLink() + public function getCMSEditLink(): ?string { return null; }