Skip to content

Commit

Permalink
API Update method signature for CMSEditLink (#1236)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Aug 25, 2024
1 parent beaf8d9 commit e38593f
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 22 deletions.
6 changes: 3 additions & 3 deletions docs/en/05_examples/00_elementalarea_with_dataobject.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class BlogPost extends DataObject
}
```

If you are using `ElementalArea` together with `DataObject`, it is important to define the `CMSEditLink()` method in the class.
[`BaseElement::CMSEditLink()`](api:DNADesign\Elemental\Models\BaseElement::CMSEditLink()) relies on a valid CMS link being available in the parent `DataObject` - in this case, `BlogPost`. It is used to navigate directly to the editing section of the particular element.
If you are using `ElementalArea` together with `DataObject`, it is important to define the `getCMSEditLink()` method in the class.
[`BaseElement::getCMSEditLink()`](api:DNADesign\Elemental\Models\BaseElement::getCMSEditLink()) relies on a valid CMS link being available in the parent `DataObject` - in this case, `BlogPost`. It is used to navigate directly to the editing section of the particular element.

> [!NOTE]
> If you have a nested [`GridField`](api:SilverStripe\Forms\GridField\GridField) this method can get more complicated. Similarly, if your class is used in multiple admins, you will have to choose one to be the canonical admin section for the purposes of this method. This example only shows the simplest case, where the `BlogPost` class is used directly in `BlogPostsAdmin`.
Expand All @@ -68,7 +68,7 @@ class BlogPost extends DataObject
{
// ...

public function CMSEditLink()
public function getCMSEditLink(): ?string
{
// In this example we use BlogPostsAdmin class as Controller
$admin = BlogPostsAdmin::singleton();
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/ElementalAreaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected function getReadOnlyBlockReducer()
'ElementTitle' => $element->Title,
'ElementEditLink' => Controller::join_links(
// Always get the edit link for the block directly, not the in-line edit form if supported
$element->CMSEditLink(true),
$element->getCMSEditLink(true),
'#Root_History'
),
],
Expand Down
18 changes: 11 additions & 7 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ public function isCMSPreview()
* @return null|string
* @throws \SilverStripe\ORM\ValidationException
*/
public function CMSEditLink($directLink = false)
public function getCMSEditLink($directLink = false): ?string
{
// Allow for repeated calls to be returned from cache
if (isset($this->cacheData['cms_edit_link'])) {
Expand Down Expand Up @@ -919,10 +919,14 @@ private function getElementCMSLink(bool $directLink)
}

if ($page instanceof SiteTree) {
$link = $page->CMSEditLink();
} elseif (ClassInfo::hasMethod($page, 'CMSEditLink')) {
$link = Controller::join_links($page->CMSEditLink(), 'ItemEditForm');
$link = $page->getCMSEditLink();
} else {
$baseLink = $page->getCMSEditLink();
if ($baseLink) {
$link = Controller::join_links($baseLink, 'ItemEditForm');
}
}

// In-line editable blocks should just take you to the page.
// Editable ones should add the suffix for detail form.
if (!$this->inlineEditable() || $directLink) {
Expand All @@ -937,7 +941,7 @@ private function getElementCMSLink(bool $directLink)
'edit'
);
} else {
// If $page is not a Page, then generate $link base on $page->CMSEditLink()
// If $page is not a Page, then generate $link base on $page->getCMSEditLink()
return Controller::join_links(
$link,
'field',
Expand Down Expand Up @@ -1015,7 +1019,7 @@ public function unsanitiseClassName($class, $delimiter = '-')
*/
public function getEditLink()
{
return Director::absoluteURL((string) $this->CMSEditLink());
return Director::absoluteURL((string) $this->getCMSEditLink());
}

/**
Expand All @@ -1028,7 +1032,7 @@ public function PageCMSEditLink()
if ($page = $this->getPage()) {
return DBField::create_field('HTMLText', sprintf(
'<a href="%s">%s</a>',
$page->CMSEditLink(),
$page->getCMSEditLink(),
$page->Title
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/ElementalArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function Breadcrumbs()
if ($owner = $ownerClassName::get()->filter('ElementalAreaID', $this->ID)->first()) {
return DBField::create_field('HTMLText', sprintf(
'<a href="%s">%s</a>',
$owner->CMSEditLink(),
$owner->getCMSEditLink(),
$owner->Title
));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reports/ElementsInUseReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function columns()
$value = $item->Title;

if (!empty($value)) {
if ($link = $item->CMSEditLink()) {
if ($link = $item->getCMSEditLink()) {
return $this->getEditLink($value, $link);
}
return $value;
Expand Down Expand Up @@ -88,7 +88,7 @@ public function columns()
'title' => _t(__CLASS__ . '.Page', 'Page'),
'formatting' => function ($value, BaseElement $item) {
if ($value) {
if ($link = $item->getPage()->CMSEditLink()) {
if ($link = $item->getPage()->getCMSEditLink()) {
return $this->getEditLink($value, $link);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="elemental-preview">
<a <% if $CMSEditLink %>href="$CMSEditLink"<% end_if %> class="elemental-edit">
<a <% if $getCMSEditLink %>href="$getCMSEditLink"<% end_if %> class="elemental-edit">
<div class="elemental-preview__icon">$Icon</div>

<div class="elemental-preview__detail">
Expand Down
6 changes: 3 additions & 3 deletions tests/BaseElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public function getElementCMSLinkDataProvider()
'admin/pages/edit/EditForm/1/field/ElementalArea/item/1/edit',
true
],
// DataObject without CMSEditLink method
// DataObject without getCMSEditLink method implemented
'element6' => [
TestElement::class,
'elementDataObject2',
Expand All @@ -354,10 +354,10 @@ public function getElementCMSLinkDataProvider()
/**
* @dataProvider getElementCMSLinkDataProvider
*/
public function testCMSEditLink(string $class, string $element, ?string $link, bool $directLink = false)
public function testGetCMSEditLink(string $class, string $element, ?string $link, bool $directLink = false)
{
$object = $this->objFromFixture($class, $element);
$editLink = $object->CMSEditLink($directLink);
$editLink = $object->getCMSEditLink($directLink);

if ($link) {
$this->assertStringContainsString($link, $editLink);
Expand Down
4 changes: 2 additions & 2 deletions tests/ElementalAreaDataObjectTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ DNADesign\Elemental\Models\ElementalArea:

DNADesign\Elemental\Tests\Src\TestDataObjectWithCMSEditLink:
dataObject1:
Title: DataObject with CMSEditLink method
Title: DataObject with getCMSEditLink method
ElementalAreaID: =>DNADesign\Elemental\Models\ElementalArea.areaDataObject1

DNADesign\Elemental\Tests\Src\TestDataObject:
dataObject2:
Title: DataObject without CMSEditLink, Link, or PreviewLink methods
Title: DataObject without getCMSEditLink, Link, or PreviewLink methods
ElementalAreaID: =>DNADesign\Elemental\Models\ElementalArea.areaDataObject2

DNADesign\Elemental\Tests\Src\TestPreviewableDataObject:
Expand Down
2 changes: 1 addition & 1 deletion tests/Src/TestDataObjectWithCMSEditLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestDataObjectWithCMSEditLink extends DataObject implements TestOnly
'ElementalArea',
];

public function CMSEditLink()
public function getCMSEditLink(): ?string
{
$link = Controller::join_links(
'admin/',
Expand Down
2 changes: 1 addition & 1 deletion tests/Src/TestPreviewableDataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getMimeType()
return null;
}

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

0 comments on commit e38593f

Please sign in to comment.