-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOC Document new CMSEditLinkExtension
- Loading branch information
1 parent
4f1d191
commit 2621b64
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
--- | ||
title: Managing Records | ||
summary: Manage your DataObject records | ||
icon: list-alt | ||
--- | ||
|
||
# Managing Records | ||
|
||
Most records in Silverstripe CMS are managed [in a GridField](../forms/field_types/gridfield) - whether in the [GridField](api:SilverStripe\Forms\GridField\GridField) | ||
of some other record or directly [in a ModelAdmin](../customising_the_admin_interface/modeladmin/). The notable exceptions to this are | ||
[SiteConfig](api:SilverStripe\SiteConfig\SiteConfig) and [SiteTree](api:SilverStripe\CMS\Model\SiteTree). | ||
|
||
## Getting an edit link | ||
|
||
To get a link for editing a record, other than some exceptions like `SiteTree`, you have historically had to generate the link mostly | ||
from scratch with mostly hardcoded paths. This was especially tedious with nested `GridField` setups. | ||
|
||
As of Silverstripe CMS 4.12.0 there is a new [CMSEditLinkExtension](api:SilverStripe\Admin\CMSEditLinkExtension) specifically | ||
for the purpose of generating links to the edit forms of records. It operates on the assumption that your record is being edited in | ||
a [GridFieldDetailForm](../forms/field_types/gridfield#gridfielddetailform) in some `GridField` (be it on another record or in a | ||
`ModelAdmin`). | ||
|
||
When using this extension, your model must also declare its `canonical_edit_owner` as a | ||
[configuration property](../configuration/configuration/#configuration-properties). The value must either be the class name of the | ||
`ModelAdmin` that directly manages the record, or the `has_one` relation for the record that this model is edited on. | ||
|
||
If the `canonical_edit_owner` is a `has_one` relation, the class on the other end of the relation _must_ have | ||
a reciprocal `has_many` relation as documented in [Relations](./relations#has-many). For best results, use dot notation on the | ||
`has_many` relation. It must also implement a [getEditLinkForManagedDataObject()](api:SilverStripe\Admin\CMSEditLinkExtension::getEditLinkForManagedDataObject()) | ||
method. The easiest way to do that is for it to have `CMSEditLinkExtension` applied as well. | ||
|
||
** app/src/Model/MyModel.php ** | ||
```php | ||
namespace MyProject\Model; | ||
|
||
use SilverStripe\Admin\CMSEditLinkExtension; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
class MyModel extends DataObject | ||
{ | ||
private static string $canonical_edit_owner = 'Parent'; | ||
|
||
private static $has_one = [ | ||
'Parent' => MyParentModel::class, | ||
]; | ||
|
||
private static $extensions = [ | ||
CMSEditLinkExtension::class, | ||
]; | ||
} | ||
``` | ||
|
||
** app/src/Model/MyParentModel.php ** | ||
```php | ||
namespace MyProject\Model; | ||
|
||
use MyProject\Admin\MyModelAdmin; | ||
use SilverStripe\Admin\CMSEditLinkExtension; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
class MyParentModel extends DataObject | ||
{ | ||
private static string $canonical_edit_owner = MyModelAdmin::class; | ||
|
||
private static $has_many = [ | ||
'Children' => MyModel::class, | ||
]; | ||
|
||
private static $extensions = [ | ||
CMSEditLinkExtension::class, | ||
]; | ||
} | ||
``` | ||
|
||
[hint] | ||
If the `canonical_edit_owner` is in some vendor dependency that you don't control, you can always apply `CMSEditLinkExtension` | ||
and the `canonical_edit_owner` via yaml. | ||
[/hint] | ||
|
||
With the above code examples, you can call `CMSEditLink()` on any instance of `MyModel` or `MyParentModel` and it will produce | ||
an appropriate edit link for that record (assuming the relations are set up). This can be used, for example, in email reminders | ||
to update content, or as a link (available to admins) on the front-end to go straight to the edit form for the record. | ||
|
||
It is also useful when [making a previewable `DataObject`](../customising_the_admin_interface/preview/), as `CMSEditLink()` is | ||
one of the methods in the [CMSPreviewable](api:SilverStripe\ORM\CMSPreviewable) interface. | ||
|
||
[info] | ||
`SiteTree` already has `CMSEditLinkExtension` applied, which means any `canonical_edit_owner` pointing to a `has_one` relation of | ||
a `SiteTree` will work, assuming the page has a `GridField` for its reciprocal `has_many` relation with a `GridFieldDetailForm` | ||
in it. | ||
[/info] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters