Merge branch 'pulls/localized-urlsegment-creation' of https://github.com/chillu/silverstripe-cms into chillu-pulls/localized-urlsegment-creation

Conflicts:
	tests/model/SiteTreeTest.php
This commit is contained in:
Daniel Hensby 2015-06-13 15:23:22 +01:00
commit 715cd9ab4e
4 changed files with 91 additions and 10 deletions

View File

@ -15,7 +15,7 @@ class SiteTreeURLSegmentField extends TextField {
/** /**
* @var string * @var string
*/ */
protected $helpText, $urlPrefix, $urlSuffix; protected $helpText, $urlPrefix, $urlSuffix, $defaultUrl;
private static $allowed_actions = array( private static $allowed_actions = array(
'suggest' 'suggest'
@ -30,7 +30,8 @@ class SiteTreeURLSegmentField extends TextField {
parent::getAttributes(), parent::getAttributes(),
array( array(
'data-prefix' => $this->getURLPrefix(), 'data-prefix' => $this->getURLPrefix(),
'data-suffix' => '?stage=Stage' 'data-suffix' => '?stage=Stage',
'data-default-url' => $this->getDefaultURL()
) )
); );
} }
@ -75,6 +76,7 @@ class SiteTreeURLSegmentField extends TextField {
*/ */
public function setHelpText($string){ public function setHelpText($string){
$this->helpText = $string; $this->helpText = $string;
return $this;
} }
/** /**
@ -90,6 +92,7 @@ class SiteTreeURLSegmentField extends TextField {
*/ */
public function setURLPrefix($url){ public function setURLPrefix($url){
$this->urlPrefix = $url; $this->urlPrefix = $url;
return $this;
} }
/** /**
@ -103,8 +106,23 @@ class SiteTreeURLSegmentField extends TextField {
return $this->urlSuffix; return $this->urlSuffix;
} }
/**
* @return Indicator for UI to respond to changes accurately,
* and auto-update the field value if changes to the default occur.
* Does not set the field default value.
*/
public function getDefaultURL(){
return $this->defaultUrl;
}
public function setDefaultURL($url) {
$this->defaultUrl = $url;
return $this;
}
public function setURLSuffix($suffix) { public function setURLSuffix($suffix) {
$this->urlSuffix = $suffix; $this->urlSuffix = $suffix;
return $this;
} }
public function Type() { public function Type() {

View File

@ -1533,7 +1533,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
} }
// If there is no URLSegment set, generate one from Title // If there is no URLSegment set, generate one from Title
if((!$this->URLSegment || $this->URLSegment == 'new-page') && $this->Title) { $defaultSegment = $this->generateURLSegment(_t(
'CMSMain.NEWPAGE',
array('pagetype' => $this->i18n_singular_name())
));
if((!$this->URLSegment || $this->URLSegment == $defaultSegment) && $this->Title) {
$this->URLSegment = $this->generateURLSegment($this->Title); $this->URLSegment = $this->generateURLSegment($this->Title);
} else if($this->isChanged('URLSegment', 2)) { } else if($this->isChanged('URLSegment', 2)) {
// Do a strict check on change level, to avoid double encoding caused by // Do a strict check on change level, to avoid double encoding caused by
@ -1971,8 +1975,12 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
(self::config()->nested_urls && $this->ParentID ? $this->Parent()->RelativeLink(true) : null) (self::config()->nested_urls && $this->ParentID ? $this->Parent()->RelativeLink(true) : null)
); );
$urlsegment = new SiteTreeURLSegmentField("URLSegment", $this->fieldLabel('URLSegment')); $urlsegment = SiteTreeURLSegmentField::create("URLSegment", $this->fieldLabel('URLSegment'))
$urlsegment->setURLPrefix($baseLink); ->setURLPrefix($baseLink)
->setDefaultURL($this->generateURLSegment(_t(
'CMSMain.NEWPAGE',
array('pagetype' => $this->i18n_singular_name())
)));
$helpText = (self::config()->nested_urls && count($this->Children())) ? $this->fieldLabel('LinkChangeNote') : ''; $helpText = (self::config()->nested_urls && count($this->Children())) ? $this->fieldLabel('LinkChangeNote') : '';
if(!Config::inst()->get('URLSegmentFilter', 'default_allow_multibyte')) { if(!Config::inst()->get('URLSegmentFilter', 'default_allow_multibyte')) {
$helpText .= $helpText ? '<br />' : ''; $helpText .= $helpText ? '<br />' : '';

View File

@ -39,7 +39,10 @@
self.data('OrigVal', title); self.data('OrigVal', title);
// Criteria for defining a "new" page // Criteria for defining a "new" page
if ((urlSegmentInput.val().indexOf('new') == 0) && liveLinkInput.val() == '') { if (
urlSegmentInput.val().indexOf(urlSegmentInput.data('defaultUrl')) === 0
&& liveLinkInput.val() == ''
) {
self.updateURLSegment(title); self.updateURLSegment(title);
} else { } else {
$('.update', self.parent()).show(); $('.update', self.parent()).show();

View File

@ -407,7 +407,6 @@ class SiteTreeTest extends SapphireTest {
Config::inst()->update('SiteTree', 'enforce_strict_hierarchy', true); Config::inst()->update('SiteTree', 'enforce_strict_hierarchy', true);
} }
public function testDeleteFromLiveOperatesRecursivelyStrict() { public function testDeleteFromLiveOperatesRecursivelyStrict() {
$this->logInWithPermission('ADMIN'); $this->logInWithPermission('ADMIN');
@ -545,7 +544,7 @@ class SiteTreeTest extends SapphireTest {
// Confirm that Member.editor can still edit the page // Confirm that Member.editor can still edit the page
$this->objFromFixture('Member','editor')->logIn(); $this->objFromFixture('Member','editor')->logIn();
$this->assertTrue($page->canEdit()); $this->assertTrue($page->canEdit());
} }
public function testCompareVersions() { public function testCompareVersions() {
// Necessary to avoid // Necessary to avoid
@ -693,6 +692,59 @@ class SiteTreeTest extends SapphireTest {
$this->assertTrue($ceo->isSection()); $this->assertTrue($ceo->isSection());
} }
public function testURLSegmentAutoUpdate() {
$sitetree = new SiteTree();
$sitetree->Title = _t(
'CMSMain.NEWPAGE',
array('pagetype' => $sitetree->i18n_singular_name())
);
$sitetree->write();
$this->assertEquals($sitetree->URLSegment, 'new-page',
'Sets based on default title on first save'
);
$sitetree->Title = 'Changed';
$sitetree->write();
$this->assertEquals($sitetree->URLSegment, 'changed',
'Auto-updates when set to default title'
);
$sitetree->Title = 'Changed again';
$sitetree->write();
$this->assertEquals($sitetree->URLSegment, 'changed',
'Does not auto-update once title has been changed'
);
}
public function testURLSegmentAutoUpdateLocalized() {
$oldLocale = i18n::get_locale();
i18n::set_locale('de_DE');
$sitetree = new SiteTree();
$sitetree->Title = _t(
'CMSMain.NEWPAGE',
array('pagetype' => $sitetree->i18n_singular_name())
);
$sitetree->write();
$this->assertEquals($sitetree->URLSegment, 'neue-seite',
'Sets based on default title on first save'
);
$sitetree->Title = 'Changed';
$sitetree->write();
$this->assertEquals($sitetree->URLSegment, 'changed',
'Auto-updates when set to default title'
);
$sitetree->Title = 'Changed again';
$sitetree->write();
$this->assertEquals($sitetree->URLSegment, 'changed',
'Does not auto-update once title has been changed'
);
i18n::set_locale($oldLocale);
}
/** /**
* @covers SiteTree::validURLSegment * @covers SiteTree::validURLSegment
*/ */