mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 06:05:56 +00:00
ENHANCEMENT Using new URLPathFilter API in SiteTree->generateURLSegment(), allowing customisation of URL filtering and transliteration
This commit is contained in:
parent
fc400d9971
commit
0b5d20513c
@ -1385,15 +1385,10 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
|||||||
if((!$this->URLSegment || $this->URLSegment == 'new-page') && $this->Title) {
|
if((!$this->URLSegment || $this->URLSegment == 'new-page') && $this->Title) {
|
||||||
$this->URLSegment = $this->generateURLSegment($this->Title);
|
$this->URLSegment = $this->generateURLSegment($this->Title);
|
||||||
} else if($this->isChanged('URLSegment')) {
|
} else if($this->isChanged('URLSegment')) {
|
||||||
// Make sure the URLSegment is valid for use in a URL
|
$filter = Object::create('URLPathFilter');
|
||||||
$segment = ereg_replace('[^A-Za-z0-9]+','-',$this->URLSegment);
|
$this->URLSegment = $filter->filter($this->URLSegment);
|
||||||
$segment = ereg_replace('-+','-',$segment);
|
|
||||||
|
|
||||||
// If after sanitising there is no URLSegment, give it a reasonable default
|
// If after sanitising there is no URLSegment, give it a reasonable default
|
||||||
if(!$segment) {
|
if(!$this->URLSegment) $this->URLSegment = "page-$this->ID";
|
||||||
$segment = "page-$this->ID";
|
|
||||||
}
|
|
||||||
$this->URLSegment = $segment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that this object has a non-conflicting URLSegment value.
|
// 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
|
* @return string Generated url segment
|
||||||
*/
|
*/
|
||||||
function generateURLSegment($title){
|
function generateURLSegment($title){
|
||||||
$t = Convert::raw2url($title);
|
$filter = Object::create('URLPathFilter');
|
||||||
|
$t = $filter->filter($title);
|
||||||
|
|
||||||
if(!$t || $t == '-' || $t == '-1') {
|
// Fallback to generic page name if path is empty (= no valid, convertable characters)
|
||||||
$t = "page-$this->ID";
|
if(!$t || $t == '-' || $t == '-1') $t = "page-$this->ID";
|
||||||
}
|
|
||||||
|
|
||||||
// Hook for extensions
|
// Hook for extensions
|
||||||
$this->extend('updateURLSegment', $t, $title);
|
$this->extend('updateURLSegment', $t, $title);
|
||||||
|
@ -101,7 +101,7 @@ class SiteTreeTest extends SapphireTest {
|
|||||||
'staff' => 'my-staff',
|
'staff' => 'my-staff',
|
||||||
'about' => 'about-us',
|
'about' => 'about-us',
|
||||||
'staffduplicate' => 'my-staff-2',
|
'staffduplicate' => 'my-staff-2',
|
||||||
'product1' => '1-1-test-product',
|
'product1' => '1.1-test-product',
|
||||||
'product2' => 'another-product',
|
'product2' => 'another-product',
|
||||||
'product3' => 'another-product-2',
|
'product3' => 'another-product-2',
|
||||||
'product4' => 'another-product-3',
|
'product4' => 'another-product-3',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user