Two separate menus in silverstripe - silverstripe

I recently started to use SilverStripe. RIght now i made a footer menu for my page where already have a left menu. Now i want to make that i can make pages in admin panel for foot and left menu. So my idea was to made a check box in admin panel, if you make a new page and check the checkbox the page go in foot menu, if leave empty then in left menu.
I add this code to my page.php sidetree
static $db = array( 'menuLocationHorizontal' => "Boolean" );
And this in getCMSFields in page.php
$fields->addFieldToTab('Root.Behaviour', new
CheckboxField('menuLocationHorizontal',"Show up in horizontal
menu?"),"ShowInSearch");
This is how looks my footnav template
<ul>
<% control Menu(2) %>
<% if menuLocationHorizontal == 1 %>
<li><span>$MenuTitle.XML</span></li>
<% end_if %>
<% end_control %>
</ul>
After i done this i just add an if statment in my foot menu and thats it. But it crashes the site. Version i use is 3.1. I guess its php foult.
Sorry guys for my English, it is not my native.

control entries are not supported on SS 3.1, what you're looking for is a <% loop %>, like so:
<ul>
<% loop Menu(2) %>
<% if menuLocationHorizontal == 1 %>
<li><span>$MenuTitle.XML</span></li>
<% end_if %>
<% end_loop %>
</ul>
More information available on Silverstripe docs website.

For me the answer was this -> https://github.com/i-lateral/silverstripe-custommenus
The only thing that actually worked.

Related

Using CSS To Style Posts in Ruby on Rails

I'm trying to create a feed of posts for my bloglike application. I'm trying to arrange it so posts are grouped by month and there are 3 posts displayed per line.
This is my code for my feed partial:
<% #posts_by_month.each do |monthname, posts| %>
<%= monthname %>
<% posts.each do |post| %>
<div style="width:100%">
<div style="float:left;width:33%"><%= post.created_at %></div>
</div>
<% end %>
<% end %>
& this is the #posts_by_month in my controller:
#posts_by_month = current_user.feed.group_by { |post| post.created_at.strftime("%B") }
This currently styles it like in this screenshot:
When the code reaches a new month I want the month to be on a new line like the arrow demonstrates, rather than part of the same block like it is currently. Does anyone have any suggestions on how to do this?
You could wrap the month name in:
<div style="clear:left"><%= monthname %></div>
This will make the monthname 'clear' anything that is floated left.
You could make it easier on yourself though by using a grid system or using flexbox like this: https://codepen.io/anon/pen/JNZqaM.
You shouldn't have to use floats for this purpose

Adding Recent post in silverstripe blog

I am working with silverstripe with first time and I almost create a blog in silverstripe but now I stuck at one place where I need help you guys. If anybody have any idea about it then please help me.
I am trying to add recent posts in my blog. I am using the below code for this
public function latestBlog($num=10) {
/* return BlogEntry::get()->sort('PublishDate','desc')->limit($num); */
echo $blogPosts;
return $blogPosts = BlogPost::get()->filter('ParentID', $this->ID)->limit($num);
}
And in my ss page I am using html like this
<% loop $latestBlog %>
<li>$Title</li>
<% end_loop %>
this gives me titles of the each post but in href I want url too
If my title is "TEST POST" then I want href like "www.mydomain.com/TEST-POST";
Can Anybody have Idea about it?
You can use $Link which will return the relative url. Reference https://docs.silverstripe.org/en/3.2/developer_guides/templates/common_variables/#links
<ul>
<% loop $latestBlog %>
<li>$Title</li>
<% end_loop %>
</ul>

SilverStripe GroupedList with has_one data

I'm banging my head trying to figure this out and I feel like I'm over thinking it.
So I've built 2 DataObjects (Brand, Product) and 1 Controller/Page (ProductType).
ProductType has many Products
Product has one Brand
Product has one ProductType
Brand has many Products
What I'm looking to do is:
ProductType pages should make available a list of Brands that are used ONLY by the Products of that Type.
Now I have one somewhat working but it feels hacky and not very portable. Example below:
ProductType Controller:
public function getBrands() {
if($products = $this->Products()) {
$group = GroupedList::create($products->sort('BrandID'))->GroupedBy('BrandID');
}
}
Product Type Template:
<% if Brands %>
<ul>
<li>All</li>
<% loop Brands %>
<% loop Children.Limit(1) %>
<li>$Brand.Title</li>
<% end_loop %>
<% end_loop %>
</ul>
<% end_if %>
Is there a way I can build a method in the ProductType controller that would return a DataList of just the Brands used by Products of that type?
Using Silverstripe 3.1.3
Let me know if I need to be more clear about something and thanks!
Ah, i guess you had a misunderstanding of a GroupedList
You already have the Datalist of all products of this product type.
It's the has_many relation
$this->Products()
now you want to group the current products by BrandID. Your method naming might be confusing, so let's rename it a bit:
public function getProductsGroupedByBrands() {
if($products = $this->Products()) {
$group = GroupedList::create($products->sort('BrandID'))
->GroupedBy('BrandID');
}
}
So in your template you have a GroupedList you can loop over.
<% if Products %>
<ul>
<li>All</li>
<% loop ProductsGroupedByBrands %>
<li>
<%-- here you should be able to see the grouping relation --%>
$Brand.Title
<%-- in doubt use the First element to get the current Brand, it's a has_one -->
<% with $Children.First %>
$Brand.Title
<% end_with %>
<ul>
<% loop Children %>
<%-- here are the products grouped by brand --%>
<li>$Title</li>
<% end_loop %>
</ul>
</li>
<% end_loop %>
</ul>
<% end_if %>
See also Silverstripe Docs or Grouping Lists

Showing the latest news page on the navigation menu in Silverstripe

I have a NewsPage type and in the navigation I want to show an entry called Latest News which will render the latest NewsPage (according to Created field). Is this possible?
Depending on your Template for the navigation you should be able to insert the link to the latest NewsPage inside a list item inside the ul for the navigation.
To get the latest NewsPage you will need to write a function inside your Page Controller that returns the latest news page. Here is an example.
function GetLatestNews()
{
return DataObject::get_one('NewsPage', '', '', 'Created DESC');
}
Then inside your template for the navigation you should do something like this.
<ul id="navigation">
<% control Menu(1) %>
<li class="$LinkingMode">$MenuTitle.XML</li>
<% end_control %>
<li class="$LinkingMode">Latest News</li>
</ul>
That will insert the link at the end of the navigation. If you want to insert the link into a specific position you can use the $Pos inside the control to check which position it is at. Here is an example:<% if Pos
<ul id="navigation">
<% control Menu(1) %>
<% if Pos = 2 %>
<li class="$LinkingMode">Latest News</li>
<% end_if %>
<li class="$LinkingMode">$MenuTitle.XML</li>
<% end_control %>
</ul>
The above will insert the link at the second position inside the navigation.

silverstripe function to return menu level

I'm trying to write a function that returns what menu levels are visible on the page...at the moment I'm using <% if %> statements in the template, ie:
<div class="<% if Menu(1) %>navA<% end_if %> <% if Menu(2) %>navB<% end_if %> <% if Menu(3) %>navC<% end_if %>">...</div>
Which, if there are 3 menu levels on a page, returns <div class="navA navB navC">
What I want is a function that returns just the lowest level menu on the current page, ie <div class="navC">
Thanks
that's perfectly possible.
just add the following to your Page_Controller class:
function LowestLevel() {
$i = 1;
while($this->getMenu($i)->count() > 0) $i++;
return 'level'.($i-1);
}
now you can call it in your template like so:
<div>lowest level: $LowestLevel</div>
$LowestLevel will print 'level1', 'level2' etc.
in case your class names have to be like 'navA', 'navB'... you need to do some matching like 'level1'->'navA', which shouldn't be too hard - come back to me if you need any help on this.
What about the following (untested):
<div class="<% if Menu(3) %>navC<% else_if Menu(2) %>navB<% else %>navA<% end_if %>">...</div>
You might want to consider using some custom code in the Controller for logic-heavy stuff, but this should get you going...

Resources