Skip to content

Commit

Permalink
API Change public CMSEditLink to protected updateCMSEditLink (#1809)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Aug 25, 2024
1 parent 4b9bf15 commit a8b6f66
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
19 changes: 13 additions & 6 deletions code/CMSEditLinkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T&static>
Expand Down Expand Up @@ -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')) {
Expand All @@ -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
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions code/Forms/UsedOnTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => []
];

Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions tests/php/CMSEditLinkExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,26 @@ 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');
$nested = $this->objFromFixture(NestedObject::class, 'one');
$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()
);
}
}
2 changes: 1 addition & 1 deletion tests/php/Navigator/UnversionedRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getMimeType()
return 'text/html';
}

public function CMSEditLink()
public function getCMSEditLink(): ?string
{
return null;
}
Expand Down

0 comments on commit a8b6f66

Please sign in to comment.