Jump to:

5451 Posts in 1673 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » 'view all' button on article holder

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

Page: 1
Go to End
Author Topic: 1470 Views
  • Aram
    Avatar
    Community Member
    598 Posts

    'view all' button on article holder Link to this post

    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

    cheers

    Aram

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: 'view all' button on article holder Link to this post

    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 "www.oursite.com/article-holder/showall" 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() {

          if($this->ViewAll()){
             return DataObject::get("ArticlePage", "ParentID = $this->ID", "Date DESC");
          }
          else{
             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! ;)

    cheers

    Aram

    1470 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.