mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX Fixed finding a translated homepage without an explicit URLSegment (e.g. http://mysite.com/?lang=de) - see #3540
ENHANCEMENT Added Translatable::get_homepage_urlsegment_by_language() ENHANCEMENT Added RootURLController::get_default_homepage_urlsegment() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@72054 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
338bb01f1a
commit
4b4b7cca9a
@ -38,9 +38,9 @@ class ModelAsController extends Controller implements NestedController {
|
|||||||
$url = Controller::join_links(
|
$url = Controller::join_links(
|
||||||
Director::baseURL(),
|
Director::baseURL(),
|
||||||
$child->URLSegment,
|
$child->URLSegment,
|
||||||
$this->urlParams['Action'],
|
(isset($this->urlParams['Action'])) ? $this->urlParams['Action'] : null,
|
||||||
$this->urlParams['ID'],
|
(isset($this->urlParams['ID'])) ? $this->urlParams['ID'] : null,
|
||||||
$this->urlParams['OtherID']
|
(isset($this->urlParams['OtherID'])) ? $this->urlParams['OtherID'] : null
|
||||||
);
|
);
|
||||||
|
|
||||||
$response = new HTTPResponse();
|
$response = new HTTPResponse();
|
||||||
|
@ -6,8 +6,19 @@
|
|||||||
* @subpackage control
|
* @subpackage control
|
||||||
*/
|
*/
|
||||||
class RootURLController extends Controller {
|
class RootURLController extends Controller {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var boolean $is_at_root
|
||||||
|
*/
|
||||||
protected static $is_at_root = false;
|
protected static $is_at_root = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string $default_homepage_urlsegment Defines which URLSegment value on a {@link SiteTree} object
|
||||||
|
* is regarded as the correct "homepage" if the requested URI doesn't contain
|
||||||
|
* an explicit segment. E.g. http://mysite.com should show http://mysite.com/home.
|
||||||
|
*/
|
||||||
|
protected static $default_homepage_urlsegment = 'home';
|
||||||
|
|
||||||
public function init() {
|
public function init() {
|
||||||
Director::set_site_mode('site');
|
Director::set_site_mode('site');
|
||||||
parent::init();
|
parent::init();
|
||||||
@ -20,7 +31,6 @@ class RootURLController extends Controller {
|
|||||||
$this->init();
|
$this->init();
|
||||||
|
|
||||||
$controller = new ModelAsController();
|
$controller = new ModelAsController();
|
||||||
|
|
||||||
$request = new HTTPRequest("GET", self::get_homepage_urlsegment().'/', $request->getVars(), $request->postVars());
|
$request = new HTTPRequest("GET", self::get_homepage_urlsegment().'/', $request->getVars(), $request->postVars());
|
||||||
$request->match('$URLSegment//$Action', true);
|
$request->match('$URLSegment//$Action', true);
|
||||||
|
|
||||||
@ -32,9 +42,13 @@ class RootURLController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the URL segment for the current HTTP_HOST value
|
* Return the URL segment for the current HTTP_HOST value
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
static function get_homepage_urlsegment() {
|
static function get_homepage_urlsegment() {
|
||||||
// Temporarily restricted to MySQL database while testing db abstraction
|
$urlSegment = '';
|
||||||
|
|
||||||
|
// @todo Temporarily restricted to MySQL database while testing db abstraction
|
||||||
if(DB::getConn() instanceof MySQLDatabase) {
|
if(DB::getConn() instanceof MySQLDatabase) {
|
||||||
$host = $_SERVER['HTTP_HOST'];
|
$host = $_SERVER['HTTP_HOST'];
|
||||||
$host = str_replace('www.','',$host);
|
$host = str_replace('www.','',$host);
|
||||||
@ -45,10 +59,12 @@ class RootURLController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($homePageOBJ) {
|
if($homePageOBJ) {
|
||||||
return $homePageOBJ->URLSegment;
|
$urlSegment = $homePageOBJ->URLSegment;
|
||||||
} else {
|
} elseif(Translatable::is_enabled()) {
|
||||||
return 'home';
|
$urlSegment = Translatable::get_homepage_urlsegment_by_language(Translatable::current_lang());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ($urlSegment) ? $urlSegment : self::get_default_homepage_urlsegment();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,6 +75,13 @@ class RootURLController extends Controller {
|
|||||||
if(!self::$is_at_root) return self::get_homepage_urlsegment() == $currentPage->URLSegment;
|
if(!self::$is_at_root) return self::get_homepage_urlsegment() == $currentPage->URLSegment;
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
static function get_default_homepage_urlsegment() {
|
||||||
|
return self::$default_homepage_urlsegment;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
@ -816,6 +816,31 @@ class Translatable extends DataObjectDecorator {
|
|||||||
return $returnMap;
|
return $returnMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a URLSegment value for a homepage in another language.
|
||||||
|
* The value is inferred by finding the homepage in default language
|
||||||
|
* (as identified by RootURLController::$default_homepage_urlsegment).
|
||||||
|
* Returns NULL if no translated page can be found.
|
||||||
|
*
|
||||||
|
* @param string $Lang
|
||||||
|
* @return string|boolean URLSegment (e.g. "home")
|
||||||
|
*/
|
||||||
|
static function get_homepage_urlsegment_by_language($lang) {
|
||||||
|
$origHomepageObj = Translatable::get_one_by_lang(
|
||||||
|
'SiteTree',
|
||||||
|
Translatable::default_lang(),
|
||||||
|
sprintf('"URLSegment" = \'%s\'', RootUrlController::get_default_homepage_urlsegment())
|
||||||
|
);
|
||||||
|
if($origHomepageObj) {
|
||||||
|
$translatedHomepageObj = $origHomepageObj->getTranslation(Translatable::current_lang());
|
||||||
|
if($translatedHomepageObj) {
|
||||||
|
return $translatedHomepageObj->URLSegment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -478,6 +478,40 @@ class TranslatableTest extends FunctionalTest {
|
|||||||
// reset language
|
// reset language
|
||||||
Translatable::set_reading_lang('en');
|
Translatable::set_reading_lang('en');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testRootUrlDefaultsToTranslatedUrlSegment() {
|
||||||
|
$_originalHost = $_SERVER['HTTP_HOST'];
|
||||||
|
|
||||||
|
$origPage = $this->objFromFixture('Page', 'homepage_en');
|
||||||
|
$origPage->publish('Stage', 'Live');
|
||||||
|
$translationDe = $origPage->createTranslation('de');
|
||||||
|
$translationDe->URLSegment = 'heim';
|
||||||
|
$translationDe->write();
|
||||||
|
$translationDe->publish('Stage', 'Live');
|
||||||
|
|
||||||
|
// test with translatable enabled
|
||||||
|
$_SERVER['HTTP_HOST'] = '/?lang=de';
|
||||||
|
Translatable::set_reading_lang('de');
|
||||||
|
$this->assertEquals(
|
||||||
|
RootURLController::get_homepage_urlsegment(),
|
||||||
|
'heim',
|
||||||
|
'Homepage with different URLSegment in non-default language is found'
|
||||||
|
);
|
||||||
|
|
||||||
|
// test with translatable disabled
|
||||||
|
Translatable::disable();
|
||||||
|
$_SERVER['HTTP_HOST'] = '/';
|
||||||
|
$this->assertEquals(
|
||||||
|
RootURLController::get_homepage_urlsegment(),
|
||||||
|
'home',
|
||||||
|
'Homepage is showing in default language if ?lang GET variable is left out'
|
||||||
|
);
|
||||||
|
Translatable::enable();
|
||||||
|
|
||||||
|
// setting back to default
|
||||||
|
Translatable::set_reading_lang('en');
|
||||||
|
$_SERVER['HTTP_HOST'] = $_originalHost;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TranslatableTest_DataObject extends DataObject implements TestOnly {
|
class TranslatableTest_DataObject extends DataObject implements TestOnly {
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
Page:
|
Page:
|
||||||
|
homepage_en:
|
||||||
|
Title: Home
|
||||||
|
URLSegment: home
|
||||||
|
Lang: en
|
||||||
testpage_en:
|
testpage_en:
|
||||||
Title: Home
|
Title: Home
|
||||||
MenuTitle: A Testpage
|
MenuTitle: A Testpage
|
||||||
|
Loading…
Reference in New Issue
Block a user