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 81aeb8d378

View File

@ -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);