Skip to content

Commit

Permalink
NEW Provide a standardised CMSEditLink method (#11338)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Aug 25, 2024
1 parent 696a4a4 commit e3508d4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/Forms/GridField/GridFieldDetailForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/ORM/CMSPreviewable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
20 changes: 20 additions & 0 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -4143,6 +4162,7 @@ public static function enable_subclass_access()
*/
private static $casting = [
"Title" => 'Text',
'CMSEditLink' => 'Text',
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getCMSValidator()
);
}

public function CMSEditLink()
public function getCMSEditLink(): ?string
{
return sprintf('my-admin/%d', $this->ID);
}
Expand Down

0 comments on commit e3508d4

Please sign in to comment.