Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Sort By Category

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2 3
Go to End
Author Topic: 4007 Views
  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Sort By Category Link to this post

    I am wondering if there is a way I can auto-sort my Data Objects based on category on the font-facing part of my site. So using my dropdown for category when the user ads an object, I'd like it to be put into the appropriate "bucket" on the Resources page template.

    What would be the best way to achieve this?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Sort By Category Link to this post

    Do you mean sort by category? Or filter by category? There's a difference. Either one is quite easy, though.

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: Sort By Category Link to this post

    I want to have them sorted I believe, so that my template looks like this

    CATEGORY 1
    Resource 1 Title
    Resource 2 Title
    Resource 3 Title

    CATEGORY 2
    Resource 1 Title
    Resource 2 Title
    Resource 3 Title

    CATEGORY 3
    Resource 1 Title
    Resource 2 Title
    Resource 3 Title

  • UncleCheese
    Avatar
    4085 Posts

    Re: Sort By Category Link to this post

    something like this oughta work:

    public function Categories()
    {
    $cats = new DataObjectSet();
    foreach(singleton($this->class)->dbObject('Category')->enumValues() as $category) {
    $cats->push(new ArrayData(array(
    'Title' => $category,
    'Resources' => $this->Resources("Category = '$category'")
    )));
    }
    return $cats;
    }

    <% control Categories %>
    <h3>$Title</h3>
    <% control Resources %>
    ...
    <% end_control %>
    <% end_control %>

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: Sort By Category Link to this post

    I was looking at something like that as well before I posted. I put that function into the ResourcePage Controller, but then when I add the code to my template

    <% control Categories %>
    <h3>$Title</h3>
    <% control Resources %>
    <a href="$Attachment.Link">$Attachment.Name &gt; $Description</a>
    <% end_control %>
    <% end_control %>

    The template doesn't render. I just get a white page.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Sort By Category Link to this post

    That's a PHP error. Crank up your error reporting and find out what's going on.

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: Sort By Category Link to this post

    UncleCheese, the error is on this line:

    foreach(singleton($this->class)->dbObject('Category')->enumValues() as $category) {

    Any ideas?

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: Sort By Category Link to this post

    I got it working. I replaced "$this->class" to Resources and it worked fine.

    One more question - I inserted a filter on the DOM in the admin section, so I can allow the admins to filter the categories if they wanted. However, it seems when I filter by category and then re-order, it doesn't apply (I can only reorder using no filter). I want the user to be able to filter by category on the backend and sort the resources, and for that order to be applied on the templates.

    Any easy way to go about this?

    4007 Views
Page: 1 2 3
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.