Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

'view all' button on article holder

Go to End

2 Posts   2172 Views

Carbon Crayon

Community Member, 598 Posts

28 December 2008 at 8:24am

Edited: 28/12/2008 8:25am

Hi guys

I am trying to create a button that would switch between a set amount of articles on a page and all the articles. I have been trying to figure out how UncleCheese did this in the Image Gallery module but I can't get it to work.

Does anyone know of a simple (or complex) way to do this? I would have thought it would be rather strait forward but can't find any documentation around.

Any help is much appreciated :)



Carbon Crayon

Community Member, 598 Posts

28 December 2008 at 9:41am

Ok, finally figured it out from UncleCheeses code :)

Here is a brief explenation for anyone who is interested.

You need 2 functions in the ArticleHolder controller class, one to test if we are in view all mode and the other to return our Articles.

The first function is called from the template to decide whether to put a 'veiw all' link or a 'view some' link. It looks like this:

function ViewAll() {
		$params = Director::urlParams();	
		return (isset($params['Action']) && $params['Action'] == 'showall');

The first line gets our URL parameters These are anything that follows the base URL, so for example if we type "" the parameter that we provide is 'showall'.

Parameters are held in an array with keys of 'Action', 'ID', 'OtherID', corresponding to {pageURL}/Action/ID/OtherID.
In this case we only need to use the first perameter which is 'Action'.

We return a boolean based on whether 'Action' is set to 'showall'. Now if we typed in "{pageURL}/showall" and called ViewAll we would get a value of 1 or true.

Next is our actual article fetching function:

function NewsItems() {

			return DataObject::get("ArticlePage", "ParentID = $this->ID", "Date DESC");
			return DataObject::get("ArticlePage", "ParentID = $this->ID", "Date DESC", "", "$this->Show");

The IF statement calls our first function and if we get a true, i.e. we are on the /showall page then it gets all the articles which are children of this holder, ELSE we just get the amount of articles that the $show variable (an int in $db) dictates.

Now we can create our template to use these functions:

        <% if ViewAll %>
		<a href="$Link" title="View paginated articles">View $Show Articles</a>
	<% else %>			
		<a href="{$Link}showall" title="View all articles">View all Articles</a>
	<% end_if %>  

        <% control NewsItems %>          
                       {usual article Code}
       <% end_control %>  

Or first bit tests whetehr we are on the /showall page or just the normal page and adjusts teh link accordingly. The second bit then loops through each article and draws it as normal. I have left out all the HTML in this bit to avoid it getting cluttered :)

Anyway I hope that kindof makes sense and saves some people the caos of my last few hours! ;)