-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RedirectorPageController: Add extension hook to customise the HTTPRes…
…ponse for empty links
- Loading branch information
Marco Hermo
committed
May 31, 2021
1 parent
4c8823f
commit 8dffaf0
Showing
3 changed files
with
72 additions
and
1 deletion.
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
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
37 changes: 37 additions & 0 deletions
37
tests/php/Model/RedirectorPageTest_MissingLinkExtension.php
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,37 @@ | ||
<?php | ||
|
||
namespace SilverStripe\CMS\Tests\Model; | ||
|
||
use SilverStripe\Control\HTTPResponse; | ||
use SilverStripe\Core\Extension; | ||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\ORM\FieldType\DBHTMLText; | ||
|
||
class RedirectorPageTest_MissingLinkExtension extends Extension implements TestOnly | ||
{ | ||
|
||
/** | ||
* This extension is only applied whenever the link is null or not found | ||
* One possible scenario is an internal link has been unpublished | ||
* If lots of RedirectorPage then its hard to track and update | ||
* | ||
* An example use case is to change the content and status code to 404 | ||
* Another way is to redirect users to existing 404 page | ||
* | ||
* In this example, this extension only changes the status code to 404 for demonstration | ||
* | ||
* @see RedirectorPageController::index() as the owner | ||
* @param $response | ||
* @return void | ||
*/ | ||
public function onMissingLinkResponse(&$response) | ||
{ | ||
if ($response instanceof HTTPResponse) { | ||
$response->setStatusCode(404); | ||
} | ||
|
||
if ($response instanceof DBHTMLText) { | ||
$response = new HTTPResponse($response->forTemplate(), 404); | ||
} | ||
} | ||
} |