-
Notifications
You must be signed in to change notification settings - Fork 67
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 SingleRecordAdmin class (#609)
- Loading branch information
1 parent
1807b3b
commit c525d14
Showing
10 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
en/02_Developer_Guides/15_Customising_the_Admin_Interface/01_ModelAdmin.md
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
63 changes: 63 additions & 0 deletions
63
en/02_Developer_Guides/15_Customising_the_Admin_Interface/03_SingleRecordAdmin.md
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,63 @@ | ||
--- | ||
title: Single-record Admin Sections | ||
summary: Create admin UI's for managing a single record with SingleRecordAdmin | ||
--- | ||
|
||
# Single-record admin sections | ||
|
||
The [`SingleRecordAdmin`](api:SilverStripe\Admin\SingleRecordAdmin) class lets you create an admin section which presents an edit form for a single record. Unlike [`ModelAdmin`](api:SilverStripe\Admin\ModelAdmin), there's no UI mechanism in a `SingleRecordAdmin` to swap what record you're editing. | ||
|
||
The main use cases for using `SingleRecordAdmin` are for a settings section (where a single record holds all the settings needed for that section), or editing a record that is unique for each signed-in user (such as that user's profile). | ||
|
||
```php | ||
namespace App\Admin; | ||
|
||
use App\Model\MySettingsModel; | ||
use SilverStripe\Admin\SingleRecordAdmin; | ||
|
||
class MySettingsAdmin extends SingleRecordAdmin | ||
{ | ||
private static $url_segment = 'my-settings'; | ||
|
||
private static $menu_title = 'My Settings'; | ||
|
||
private static $model_class = MySettingsModel::class; | ||
} | ||
``` | ||
|
||
This admin section will fetch a record of the `MySettingsModel` class using the [`get_one()`](api:SilverStripe\ORM\DataObject::get_one()) method. | ||
|
||
If you don't want the admin section to fetch your record in this way, you can set the [`restrict_to_single_record`](api:SilverStripe\Admin\SingleRecordAdmin->restrict_to_single_record) configuration property to false. In this case you must provide another way for the admin section to know which record to edit. This could be in the form of a separate action on the controller (e.g. `edit/$ID`), or by calling [`setCurrentPageID()`](api:SilverStripe\Admin\LeftAndMain::setCurrentPageID()) in the [`init()`](api:SilverStripe\Admin\LeftAndMain::init()) method of the controller. | ||
|
||
If there's no record to edit, by default it will create one for you. To disable that behaviour, set the [`allow_new_record`](api:SilverStripe\Admin\SingleRecordAdmin->allow_new_record) configuration property to false. | ||
|
||
> [!NOTE] | ||
> If you need more complex behaviour to fetch your record, for example you use the `silverstripe/subsites` or `tractorcow/silverstripe-fluent` module, you could either override the [`getSingleRecord()`](api:SilverStripe\Admin\SingleRecordAdmin::getSingleRecord()) method, or you could create [an extension](/developer_guides/extending/extensions/) that implements the `augmentSQL()` extension hook. | ||
> | ||
> The latter option is the most flexible, as it affects all high-level ORM queries for your model which ensures consistency when fetching the record from other areas of your code. | ||
For records that hold settings, it's common to provide a static method to get the current settings record. Below is a basic example. | ||
|
||
```php | ||
namespace App\Model; | ||
|
||
use SilverStripe\ORM\DataObject; | ||
|
||
class MySettingsModel extends DataObject | ||
{ | ||
// ... | ||
|
||
/** | ||
* Get the current settings record | ||
*/ | ||
public static function currentRecord(): MySettingsModel | ||
{ | ||
$record = MySettingsModel::get_one(); | ||
if (!$record) { | ||
$record = MySettingsModel::create(); | ||
$record->write(skipValidation: true); | ||
} | ||
return $record; | ||
} | ||
} | ||
``` |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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