Skip to content

Commit

Permalink
DOC Document standardisation of extension hooks (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Aug 27, 2024
1 parent 3f9c554 commit 9b76456
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion en/02_Developer_Guides/00_Model/07_Permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ use SilverStripe\Security\Permission;

class PermissionsExtension extends Extension
{
public function canView()
protected function canView()
{
if (!Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion en/02_Developer_Guides/00_Model/09_Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Most validation constraints are actually data constraints which belong on the mo
called any time the `write()` method is called, before the `onBeforeWrite()` extension hook.

By default, there is no validation - objects are always valid! However, you can override this method in your `DataObject`
sub-classes to specify custom validation, or use the `validate()` extension hook through an [Extension](api:SilverStripe\Core\Extension).
sub-classes to specify custom validation, or use the `updateValidate()` extension hook through an [Extension](api:SilverStripe\Core\Extension).

Invalid objects won't be able to be written - a [`ValidationException`](api:SilverStripe\ORM\ValidationException) will be thrown and no write will occur.

Expand Down
2 changes: 1 addition & 1 deletion en/02_Developer_Guides/00_Model/10_Versioning.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ use SilverStripe\Security\Permission;

class MyObjectExtension extends DataExtension
{
public function canViewNonLive($member = null)
protected function canViewNonLive($member = null)
{
if (!Permission::check($member, 'DRAFT_STATUS')) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion en/02_Developer_Guides/01_Templates/02_Common_Variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ SilverStripe\CMS\Model\SiteTree:

### Modifying meta tags

You can override the `MetaComponents()` method on your `SiteTree` sub-classes or make use of the `MetaComponents` extension point to manipulate the underlying data that is rendered by `$MetaTags`. Example (for `Page` class):
You can override the `MetaComponents()` method on your `SiteTree` sub-classes or make use of the `updateMetaComponents` extension point to manipulate the underlying data that is rendered by `$MetaTags`. Example (for `Page` class):

```php
namespace App\PageType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MyMemberExtension extends DataExtension
/**
* This extension hook is called every time a member is logged in
*/
public function afterMemberLoggedIn()
protected function onAfterMemberLoggedIn()
{
$this->logVisit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CustomLeftAndMain extends LeftAndMainExtension
{
// ...

public function init()
protected function onInit()
{
// unique identifier for this item. Will have an ID of Menu-$ID
$id = 'LinkToExample';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use SilverStripe\Core\Extension;

class MyAdminExtension extends Extension
{
public function updateEditForm($form)
protected function updateEditForm($form)
{
$form->Fields()->push(/* ... */)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class LoginSessionExtension extends DataExtension
/**
* @param Member $member
*/
public function canView($member)
protected function canView($member)
{
if ($this->getOwner()->Member()->canView($member)) {
// If you can view a Member, you can also view their sessions.
Expand All @@ -103,7 +103,7 @@ class LoginSessionExtension extends DataExtension
/**
* @param Member $member
*/
public function canDelete($member)
protected function canDelete($member)
{
if ($this->getOwner()->Member()->canEdit($member)) {
// If you can edit a Member, you can also log them out of a session.
Expand Down
28 changes: 27 additions & 1 deletion en/08_Changelogs/6.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,33 @@ This release includes a number of bug fixes to improve a broad range of areas. C

### Most extension hook methods are now protected {#hooks-protected}

Core implementations of most extension hooks such as `updateCMSFields()` now have protected visibility. Formally they had public visibility which meant they could be called directly which was not how they were intended to be used. Extension hook implementations are still able to be declared public in project code, though it is recommended that all extension hook methods are declared protected in project code to follow best practice.
Core implementations of most extension hooks such as `updateCMSFields()` now have protected visibility. Formerly they had public visibility which meant they could be called directly which was not how they were intended to be used. Extension hook implementations are still able to be declared public in project code, though it is recommended that all extension hook methods are declared protected in project code to follow best practice.

### Changes to some extension hook names {#hooks-renamed}

In order to better align the codebase in Silverstripe CMS with best practices, the following extension hook methods have changed name:

|class that defined the hook|old name|new name|
|---|---|---|
|[`Member`](api:SilverStripe\Security\Member)|`afterMemberLoggedIn`|`onAfterMemberLoggedIn`|
|[`Member`](api:SilverStripe\Security\Member)|`afterMemberLoggedOut`|`onAfterMemberLoggedOut`|
|[`Member`](api:SilverStripe\Security\Member)|`authenticationFailed`|`onAuthenticationFailed`|
|[`Member`](api:SilverStripe\Security\Member)|`authenticationFailedUnknownUser`|`onAuthenticationFailedUnknownUser`|
|[`Member`](api:SilverStripe\Security\Member)|`authenticationSucceeded`|`onAuthenticationSucceeded`|
|[`Member`](api:SilverStripe\Security\Member)|`beforeMemberLoggedIn`|`onBeforeMemberLoggedIn`|
|[`Member`](api:SilverStripe\Security\Member)|`beforeMemberLoggedOut`|`onBeforeMemberLoggedOut`|
|[`LeftAndMain`](api:SilverStripe\Admin\LeftAndMain)|`init`|`onInit`|
|[`DataObject`](api:SilverStripe\ORM\DataObject)|`flushCache`|`onFlushCache`|
|[`LostPasswordHandler`](api:SilverStripe\Security\MemberAuthenticator\LostPasswordHandler)|`forgotPassword`|`onForgotPassword`|
|[`ErrorPage`](api:SilverStripe\ErrorPage\ErrorPage)|`getDefaultRecords`|`updateDefaultRecords`|
|[`SiteTree`](api:SilverStripe\CMS\Model\SiteTree)|`MetaComponents`|`updateMetaComponents`|
|[`SiteTree`](api:SilverStripe\CMS\Model\SiteTree)|`MetaTags`|`updateMetaTags`|
|[`DataObject`](api:SilverStripe\ORM\DataObject)|`populateDefaults`|`onAfterPopulateDefaults`|
|[`Member`](api:SilverStripe\Security\Member)|`registerFailedLogin`|`onRegisterFailedLogin`|
|[`DataObject`](api:SilverStripe\ORM\DataObject)|`requireDefaultRecords`|`onRequireDefaultRecords`|
|[`DataObject`](api:SilverStripe\ORM\DataObject)|`validate`|`updateValidate`|

If you have implemented any of those methods in an [`Extension`](api:SilverStripe\Core\Extension) subclass, you will need to rename it for it to continue working.

### Strict typing for `Factory` implementations {#factory-strict-typing}

Expand Down

0 comments on commit 9b76456

Please sign in to comment.