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

Commit

Permalink
[FEATURE] There's now validation for vanity mappings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Glasl committed Feb 11, 2019
1 parent 46b0179 commit 853fdb5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [misdirection](https://packagist.org/packages/nglasl/silverstripe-misdirection)

_The current release is **3.0.0**_
_The current release is **3.1.0**_

> This module allows 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.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
"dev-master": "3.1.x-dev"
},
"expose": [
"client"
Expand Down
33 changes: 29 additions & 4 deletions src/extensions/SiteTreeMisdirectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace nglasl\misdirection;

use SilverStripe\CMS\Controllers\CMSPageSettingsController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\ValidationResult;

/**
* This extension provides vanity mapping directly from a page, and automatically creates the appropriate link mappings when replacing the default automated URL handling.
Expand All @@ -25,10 +27,6 @@ class SiteTreeMisdirectionExtension extends DataExtension {
'VanityMapping' => LinkMapping::class
);

/**
* Display the vanity mapping fields.
*/

public function updateSettingsFields($fields) {

$fields->addFieldToTab('Root.Misdirection', HeaderField::create(
Expand All @@ -52,6 +50,33 @@ public function updateSettingsFields($fields) {
$this->owner->extend('updateSiteTreeMisdirectionExtensionSettingsFields', $fields);
}

public function validate(ValidationResult $result) {

// Retrieve the vanity mapping URL, where this is only possible using the POST variable.

$vanityURL = (!Controller::has_curr() || is_null($controller = Controller::curr()) || is_null($URL = $controller->getRequest()->postVar('VanityURL'))) ? $this->owner->VanityMapping()->MappedLink : $URL;

// Determine whether another vanity mapping already exists.

$existing = LinkMapping::get()->filter(array(
'MappedLink' => $vanityURL,
'RedirectType' => 'Page',
'RedirectPageID:not' => array(
0,
$this->owner->ID
)
))->first();
if($result->isValid() && $existing && ($page = $existing->getRedirectPage())) {
$link = Controller::join_links(CMSPageSettingsController::singleton()->Link('show'), $page->ID);
$result->addError("Vanity URL <a href='{$link}' target='_blank'>already exists</a>!", ValidationResult::TYPE_ERROR, null, ValidationResult::CAST_HTML);
}

// Allow extension.

$this->owner->extend('validateSiteTreeMisdirectionExtension', $result);
return $result;
}

/**
* Update the corresponding vanity mapping.
*/
Expand Down
4 changes: 0 additions & 4 deletions src/objects/LinkMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,6 @@ public function getCMSFields() {
return $fields;
}

/**
* Confirm that the current link mapping is valid.
*/

public function validate() {

$result = parent::validate();
Expand Down

0 comments on commit 853fdb5

Please sign in to comment.