ENHANCEMENT Using new URLPathFilter API in SiteTree->generateURLSegment(), allowing customisation of URL filtering and transliteration

This commit is contained in:
Ingo Schommer 2011-11-14 12:28:25 +01:00
parent fc400d9971
commit 0b5d20513c
2 changed files with 8 additions and 13 deletions

View File

@ -1385,15 +1385,10 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
if((!$this->URLSegment || $this->URLSegment == 'new-page') && $this->Title) {
$this->URLSegment = $this->generateURLSegment($this->Title);
} else if($this->isChanged('URLSegment')) {
// Make sure the URLSegment is valid for use in a URL
$segment = ereg_replace('[^A-Za-z0-9]+','-',$this->URLSegment);
$segment = ereg_replace('-+','-',$segment);
$filter = Object::create('URLPathFilter');
$this->URLSegment = $filter->filter($this->URLSegment);
// If after sanitising there is no URLSegment, give it a reasonable default
if(!$segment) {
$segment = "page-$this->ID";
}
$this->URLSegment = $segment;
if(!$this->URLSegment) $this->URLSegment = "page-$this->ID";
}
// Ensure that this object has a non-conflicting URLSegment value.
@ -1583,11 +1578,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* @return string Generated url segment
*/
function generateURLSegment($title){
$t = Convert::raw2url($title);
$filter = Object::create('URLPathFilter');
$t = $filter->filter($title);
if(!$t || $t == '-' || $t == '-1') {
$t = "page-$this->ID";
}
// Fallback to generic page name if path is empty (= no valid, convertable characters)
if(!$t || $t == '-' || $t == '-1') $t = "page-$this->ID";
// Hook for extensions
$this->extend('updateURLSegment', $t, $title);

View File

@ -101,7 +101,7 @@ class SiteTreeTest extends SapphireTest {
'staff' => 'my-staff',
'about' => 'about-us',
'staffduplicate' => 'my-staff-2',
'product1' => '1-1-test-product',
'product1' => '1.1-test-product',
'product2' => 'another-product',
'product3' => 'another-product-2',
'product4' => 'another-product-3',