I am working on a silverstripe subdomains’ issue, someone else did the code, so any help would be appreciated.
There is a “About Us” nav bar on the main website eg http://www.mainwebsite.com, and we would like the “About Us” to disappear for any subdomains eg subdomain.mainwebsite.com.
I can see from the Silverstripe backend, there is a ‘Hide in Subdomains’ function, and I ticked it. However, the “About Us” link disappears for some time and it comes back at other tomes on both main site and subdomains.
Can anybody point me to the right direction how can I fix this problem? I can copy any code here if you need. Please help.
Thanks heaps,
S:)
add comments, I have found this code in my page.php in my site – code folder if it is useful
function ShowMenuInSubdomain()
{
$host = explode('.',$_SERVER['HTTP_HOST']);
$subdomain = $host[0];
if($subdomain != 'www' && $this->HideInSubdomains) {
return true;
}
}
add comments, here is navigation part in Page.ss in the theme – templates folder I am using
<div id="Header">
<div id="HeaderWrapper">
<div id="LogoWrapper"><a href="{$BaseHref}">$GetSubDomainHeaderImage</a></div>
<div id="Navigation">
<% cached 'Navigation', Aggregate(Page).Max(LastEdited) %>
<% include Navigation %>
<% end_cached %>
</div>
</div>
</div>
add comments, here is what my navigation.ss in the templates – includes folder looks like
<ul>
<% control Menu(1) %>
<% if ShowMenuInSubdomain %>
<% else %>
<li <% if Children %>class="hasChildren"<% end_if %>>
<a href="$Link" title="$Title.XML" class="$LinkingMode">$MenuTitle.XML <% if ShowMenuInSubdomain %>0<% end_if %></a>
<% if Children %>
<ul>
<% control Children %>
<li <% if Children %>class="hasChildren"<% end_if %>>
<a href="$Link" title="$Title.XML" class="$LinkingMode">$MenuTitle.XML</a>
<% if Children %>
<ul>
<% control Children %>
<ul class="thridUL{$Pos}">
<% if DisableLink %>
<li>
<span class="$LinkingMode thirdLevelTitle lookLikeALink">$MenuTitle.XML</span>
</li>
<% else_if ShowLabelInMenu==0 %>
<li>
<a href="$Link" title="$Title.XML" class="$LinkingMode thirdLevelTitle">$MenuTitle.XML</a>
</li>
<% end_if %>
<% if Children %>
<% control Children %>
<li>
<a href="$Link" title="$Title.XML" class="$LinkingMode">$MenuTitle.XML</a>
<% if Summary %>
<span class="menuSummary">
$Summary
<span class="menuSummaryThumb">$Thumbnail.PaddedImage(160, 160)</span>
</span>
<% end_if %>
</li>
<% end_control %>
<% end_if %>
</ul>
<div class="clear"> </div>
<% end_control %>
</ul>
<% end_if %>
</li>
<% end_control %>
</ul>
<% end_if %>
</li>
<% end_if %>
<% end_control %>
<li id="calculatorWrapper">
<a id="Calculator" href="$distanceCalculator.Link" rel="shadowbox;height=800;width=1000"><span>Journey Planner</span></a>
</li> </ul>
Sorry it is a bit long but any help is appreciated. Thanks.
Hi everyone, it seems to be working
now when I deleted <% cached
‘Navigation’,
Aggregate(Page).Max(LastEdited) %> <%
end_cached %> in page.ss. Can someone
please kindly explain what was this
line for or the meaning of it? Thanks.
as you’ve already found a method to the subodmain check for you (‘ShowMenuInSubdomain’), look now for the template where the menu is rendered. for a standard silverstripe installation this is most probably
/mysite/templates/Page.ss, or one of the files in/mysite/templates/layout.your menu might be rendered inside a block like the following:
you just have to wrap the list item then with a control block calling your
ShowMenuInSubdomainfunction like so:please post the corresponding template code block in case you’re having trouble with this.
NOTE: i think there’s a ‘!’ missing in your ShowMenuInSubdomain function, as it currently reads like ‘if (HideInSubdomains) then ShowMenuInSubdomain is true’, so the line in question should probably be:
(watch out for the ‘!’ before $this->HideInSubdomains)
EDIT
first, forget about the
GetSubDomainMenufunction, looks as though it’s been replaced by the much cleaner solution of using the built-inMenucontrol in conjunction withShowMenuInSubdomainfunction for the subdomain check.second, forget about my note on the missing ‘!’ before
$this->HideInSubdomainsabove. from the way the function is used i can see that it does what it’s supposed to, the function is just named in a misleading way:ShowMenuInSubdomainshould readHideMenuInSubdomain. confusing, but not the source of the problem, apparently.so, from the code you’ve posted so far, there is no visible mistake, so you should try to verify the following:
a) is the ‘navigation.ss’ (NOT .cc as you’ve posted i guess) actually the template that’s rendered? just add some test output inside the file to be sure (and add ‘?flush=1’ to your url to clear the template cache)
b) does the
ShowMenuInSubdomainfunction actually get called? make it return some string like ‘return “working”‘ in the first line, then add$ShowMenuInSubdomainto your templatein case you’re still stuck then, you may zip your silverstripe project folder and put it somewhere for download (remove critical info like db access credentials first!), so i can have a look.
good luck!
EDIT II – SOLUTION – FINALLY 🙂
looks as though you found the flawed code part. the lines you’ve deleted were supposed to cache the navigation, so it doesn’t have to get assembled each time it’s rendered (i guess you’re familiar with the concept of ‘caching’). have a look at the silverstripe docs on partial caching for further explanations on this. watch out for the first code snippet under ‘Aggregates’ there – it’s exactly the code you’ve deleted.
but why does removing the caching part solve your problem? in fact, the answer is quite simple: as your cached navigation will only update after editing some page, the subdomain checking function won’t be called unless then too, wheter you’re on a subdomain or not. it’s that easy sometimes 🙂