Skip to content
This repository was archived by the owner on Jan 10, 2021. It is now read-only.

Commit 7548913

Browse files
author
Nathan Glasl
committed
[FEATURE] There's now validation for vanity mappings.
1 parent 9ce82e5 commit 7548913

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [misdirection](https://packagist.org/packages/nglasl/silverstripe-misdirection)
22

3-
_The current release is **2.2.26**_
3+
_The current release is **2.3.0**_
44

55
> A module for SilverStripe which will allow both simple and regular expression link redirections based on customisable mappings, either hooking into a page not found or replacing the default automated URL handling.
66

code/dataobjects/LinkMapping.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,6 @@ public function getCMSFields() {
272272
return $fields;
273273
}
274274

275-
/**
276-
* Confirm that the current link mapping is valid.
277-
*/
278-
279275
public function validate() {
280276

281277
$result = parent::validate();

code/extensions/SiteTreeMisdirectionExtension.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ class SiteTreeMisdirectionExtension extends DataExtension {
1515
'VanityMapping' => 'LinkMapping'
1616
);
1717

18-
/**
19-
* Display the vanity mapping fields.
20-
*/
21-
2218
public function updateSettingsFields($fields) {
2319

2420
$fields->addFieldToTab('Root.Misdirection', HeaderField::create(
@@ -42,6 +38,33 @@ public function updateSettingsFields($fields) {
4238
$this->owner->extend('updateSiteTreeMisdirectionExtensionSettingsFields', $fields);
4339
}
4440

41+
public function validate(ValidationResult $result) {
42+
43+
// Retrieve the vanity mapping URL, where this is only possible using the POST variable.
44+
45+
$vanityURL = (!Controller::has_curr() || is_null($controller = Controller::curr()) || is_null($URL = $controller->getRequest()->postVar('VanityURL'))) ? $this->owner->VanityMapping()->MappedLink : $URL;
46+
47+
// Determine whether another vanity mapping already exists.
48+
49+
$existing = LinkMapping::get()->filter(array(
50+
'MappedLink' => $vanityURL,
51+
'RedirectType' => 'Page',
52+
'RedirectPageID:not' => array(
53+
0,
54+
$this->owner->ID
55+
)
56+
))->first();
57+
if($result->valid() && $existing && ($page = $existing->getRedirectPage())) {
58+
$link = Controller::join_links(CMSPageSettingsController::singleton()->Link('show'), $page->ID);
59+
$result->error('Vanity URL already exists!');
60+
}
61+
62+
// Allow extension.
63+
64+
$this->owner->extend('validateSiteTreeMisdirectionExtension', $result);
65+
return $result;
66+
}
67+
4568
/**
4669
* Update the corresponding vanity mapping.
4770
*/

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"extra": {
2626
"installer-name": "misdirection",
2727
"branch-alias": {
28-
"dev-master": "2.2.x-dev"
28+
"dev-master": "2.3.x-dev"
2929
},
3030
"screenshots": [
3131
"https://raw.githubusercontent.com/nglasl/silverstripe-misdirection/master/images/misdirection-testing.png"

0 commit comments

Comments
 (0)