Skip to content

Commit

Permalink
Add RootURLController Extension now that functionality is removed fro…
Browse files Browse the repository at this point in the history
…m core
  • Loading branch information
altwohill committed Sep 15, 2019
1 parent a707fb6 commit 1b4be81
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions _config/extensions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SilverStripe\CMS\Model\SiteTree:
extensions:
- Twohill\HomepageForDomain\HomepageForDomainExtension
SilverStripe\CMS\Controllers\RootURLController:
extensions:
- Twohill\HomepageForDomain\RootURLControllerExtension
33 changes: 33 additions & 0 deletions code/RootURLControllerExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


namespace Twohill\HomepageForDomain;


use SilverStripe\Core\Extension;
use SilverStripe\CMS\Model\SiteTree;

class RootURLControllerExtension extends Extension
{

/**
* @param $link
* @return string
*/
public function updateHomepageLink(&$link)
{
$host = str_replace('www.', null, $_SERVER['HTTP_HOST']);
$candidates = SiteTree::get()->where([
'"SiteTree"."HomepageForDomain" LIKE ?' => "%$host%"
]);
if ($candidates) {
/** @var SiteTree $candidate */
foreach ($candidates as $candidate) {
if (preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) {
$link = trim($candidate->RelativeLink(true), '/');
}
}
}
return $link;
}
}

0 comments on commit 1b4be81

Please sign in to comment.