Showing the latest news page on the navigation menu in Silverstripe - 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.

Related

Pagination Wordpress in Custom Plugin ( Javascript )

Hi I hope someone can help with this. I'm building a wordpress plugin. I just have basic knowledge of php and i managed to build the whole plugin except pagination. I have tried so many code using javascript but none is working for me. Here is the image. I want to divide the items with pagination
Thank you in advance!
A good approach will be to think about the pages like tabs. Then the page link will behave like buttons that show/hide based on the page link clicked.
Here's an example using jQuery
// Add an event listener for when the page link is clicked
$('.page-link').on('click', function(){
// Save the page number that was clicked
var pageNum = $(this).data('page-id')
// Hide any open 'pages'
$('.page-content').hide();
// Find and show the selected page
$('[data-page=' + pageNum + ']'').show();
});
For this to work, you'd need a HTML structure a bit like this
<!-- The Page Link -->
<div>
<ul>
<li class="page-link" data-page-id="1">Page 1</li>
<li class="page-link" data-page-id="2">Page 2</li>
<li class="page-link" data-page-id="3">Page 3</li>
</ul>
<!-- The Page Content -->
<div class="page-content" data-page="1" style="display: none;">
Page 1 content
</div>
<div class="page-content" data-page="2" style="display: none;">
Page 2 content
</div>
<div class="page-content" data-page="3" style="display: none;">
Page 3 content
</div>
</div>
Here is a super basic jsfiddle

Two separate menus in 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.

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

Menu icon does not open menu after clicking a link

I am developing an app using Rails 4 and Foundation 4. I'm using the top-bar navigation elements from Foundation 4 which hide the menu in a menu icon when the display is small.
The problem I am having is that I can only toggle the menu when the page is first loaded (after going to it directly or reloading it). If I click on any links, the menu stops working until the page is refreshed.
If I open up Chome's debugger I find
Error in event handler for 'undefined': IndexSizeError: DOM Exception 1 Error: Index or size was negative, or greater than the allowed value.
Googling that seemed to show it was a bug in Chrome, but Firefox doesn't work, nor does my phone.
Here is my code for the top bar
<nav class="top-bar">
<ul class="title-area">
<li class="name">
<h1><%= link_to "My App",root_path %></h1>
</li>
<li class="toggle-topbar menu-icon"><span></span></li>
</ul>
<section class="top-bar-section">
<!-- Right Nav Section -->
<ul class="right">
<% if signed_in? %>
<li class="divider hide-for-small"></li>
<li><%= link_to "My Account", account_path %></li>
<li class="divider"></li>
<% end %>
<li class="divider show-for-small"></li>
<li class="has-form">
<% if signed_in? %>
<%= link_to "Sign Out", signout_path, class: "button", method: "delete" %>
<% else %>
<%= link_to "Sign In", signin_path, class: "button" %>
<% end %>
</li>
</ul>
</section>
</nav>
Any ideas on how to get this working? I haven't changed any of the css for the top-bar and I'm using Foundation 4.3.1
It turns out this is caused by TurboLinks in Rails 4.0
I found this page http://blog.joshsoftware.com/2013/06/28/troubleshooting-turbolinks-with-rails-4/ that talks about a solution.
My application.js file looks like
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require foundation
$(document).foundation();
$(function(){ $(document).foundation(); });
//= require_tree .
//= require turbolinks
and I had to add the following line to my Gemfile
gem 'jquery-turbolinks'

show/hide css menu item depending user role using asp.net

I have the css layout: One column fixed width layout, from maxdesign.com
I have two menu items defined like the following:
<div id="navigation">
<ul>
<li>Data Entry</li>
<li>Reports</li>
</ul>
</div>
Now, let's say that I have two roles: guest and operator, and I want that for example if a user with role guest is logged in, then just the Report item from the menu appear, and in case of a user operator is logged in, then both options appear.
How can I accomplish that?
EDIT:
Based on your responses, I'll go with the server side logic to deal with this:
<div id="navigation">
<ul>
<li><asp:LinkButton ID="lkbDataEntry" runat="server">Data Entry</asp:LinkButton></li>
<li><asp:LinkButton ID="lkbReports" runat="server">Reports</asp:LinkButton></li>
</ul>
</div>
Thanks!
You can put this in the Page_Load..
Dim cs As ClientScriptManager = Page.ClientScript
If Not cs.IsClientScriptBlockRegistered(Me.GetType(), "RoleVariable") Then
Dim js As New String
js = "var _role = " & role & ";"
cs.RegisterStartupScript(Me.GetType(), "RoleVariable", js, True)
End If
And from there, you will have the role in the Javascript realm, where you can manipulate the visibility of the items you want.
So...
<script type="text/javascript">
function hideStuff() {
if (_role === "operator") {
// hide/show your elements here
}
else if (_role === "guest") {
// hide/show your elements here
}
}
</script>
Keep in mind that this approach is all client-side and is therefore easy for another developer to manipulate if they really wanted to. But on the other hand, it's the simplest. Don't use this approach for high-security situations.
You could give your menu elements an ID attribute and then in your codebehind either use RegisterClientSideScriptBlock or use Response.Write to send JavaScript to the client to hide (or show) elements based on some condition.
how about something simple like?
<% if(Page.User.IsInRole("operator") || Page.User.IsInRole("guest")) { %>
<div id="navigation">
<ul>
<% if(Page.User.IsInRole("operator")) { %>
<li>Data Entry</li>
<% } %>
<li>Reports</li>
</ul>
</div>
<% } %>
I don't 100% think you can (or should) be doing logic operations with stylesheets. You may need to have some javascript and then decide based on guest or operator which style to display

Resources