NEW Provide a standardised CMSEditLink method (#11338)

This commit is contained in:
Guy Sartorelli 2024-08-26 10:56:15 +12:00 committed by GitHub
parent 696a4a4dc5
commit e3508d41d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 8 deletions

View File

@ -175,7 +175,7 @@ class GridFieldDetailForm extends AbstractGridFieldComponent implements GridFiel
/** /**
* Try and find another URL at which the given record can be edited. * 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}. * This only works when the list passed to the GridField is a {@link DataList}.
* *
* @param $gridField The current GridField * @param $gridField The current GridField
@ -203,9 +203,7 @@ class GridFieldDetailForm extends AbstractGridFieldComponent implements GridFiel
} }
$existing = DataObject::get($list->dataClass())->byID($id); $existing = DataObject::get($list->dataClass())->byID($id);
if ($existing && $existing->hasMethod('CMSEditLink')) { $link = $existing?->getCMSEditLink();
$link = $existing->CMSEditLink();
}
if ($link && $link == $request->getURL()) { if ($link && $link == $request->getURL()) {
throw new \LogicException(sprintf( throw new \LogicException(sprintf(
@ -282,7 +280,7 @@ class GridFieldDetailForm extends AbstractGridFieldComponent implements GridFiel
* Enable redirection to missing records. * Enable redirection to missing records.
* *
* If a GridField shows a filtered list, and the record is not in the list but exists in the * 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. * URL returned by that method.
*/ */
public function setRedirectMissingRecords(bool $redirectMissingRecords): GridFieldDetailForm public function setRedirectMissingRecords(bool $redirectMissingRecords): GridFieldDetailForm

View File

@ -32,9 +32,9 @@ interface CMSPreviewable
public function getMimeType(); 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: * controller subclassing {@link LeftAndMain}. Example:
* http://mysite.com/admin/edit/6 * http://mysite.com/admin/edit/6
*/ */
public function CMSEditLink(); public function getCMSEditLink(): ?string;
} }

View File

@ -2692,6 +2692,25 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
return SSViewer::get_templates_by_class(static::class, $suffix, $this->baseClass()); 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. * Gets the value of a field.
* Called by {@link __get()} and any getFieldName() methods you might create. * Called by {@link __get()} and any getFieldName() methods you might create.
@ -4143,6 +4162,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
*/ */
private static $casting = [ private static $casting = [
"Title" => 'Text', "Title" => 'Text',
'CMSEditLink' => 'Text',
]; ];
/** /**

View File

@ -75,7 +75,7 @@ class Person extends DataObject implements TestOnly
); );
} }
public function CMSEditLink() public function getCMSEditLink(): ?string
{ {
return sprintf('my-admin/%d', $this->ID); return sprintf('my-admin/%d', $this->ID);
} }