diff --git a/code/CMSEditLinkExtension.php b/code/CMSEditLinkExtension.php index ce20bce31..247a5cb47 100644 --- a/code/CMSEditLinkExtension.php +++ b/code/CMSEditLinkExtension.php @@ -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 diff --git a/code/Forms/UsedOnTable.php b/code/Forms/UsedOnTable.php index 0521d3a35..44fe4337f 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->CMSEditLink(), '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->CMSEditLink(), ]; } $usageData[] = $tableRowData; diff --git a/tests/php/Navigator/UnversionedRecord.php b/tests/php/Navigator/UnversionedRecord.php index 4efb68c58..bec97a64d 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 CMSEditLink(): ?string { return null; }