Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Highlight "category" instead of Parent page in menu

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 2189 Views
  • stallain
    Avatar
    Community Member
    61 Posts

    Highlight "category" instead of Parent page in menu Link to this post

    Hello,
    I've got several pages, let's say :

    - AllRecipes
          - Recipe (A)
          - Recipe (B)
          - Recipe (C)
          - ...
    - Category (a)
    - Category (b)
    - Category (c)
    - ...

    Each Recipe can belong to several Categories ; each Category can hold several Recipes ; I created a dataobject called "RecipeCategory" so that I can manage all the relationships between my Category Pages and my Recipe Pages.
    If I click on "Category a" in my main navigation bar, it displays every title of all the recipes belonging to the category.

    My problem is when I call the Recipe page itself.
    The main navigation bar is coded this way :
    <ul>
    <% control Menu(1) %>
    <li><a href="$Link" class="$LinkingMode">$MenuTitle</a></li>
    <% end_control %>
    </ul>
    So the highlighted menu item is always "AllRecipes", since all my Recipes are children of the AllRecipes page...

    I would like the Category (or even the categories, since it's a many-to-many relationship) to be the highlighted menu item, instead of the parent page.

    What would be the best way to get to this ?
    Thank you !

    Stan

  • tobych
    Avatar
    Community Member
    97 Posts

    Re: Highlight "category" instead of Parent page in menu Link to this post

    Stan,

    It strikes me that you've taken the wrong route from the start here, actually. If I understand you correctly, you've created a class called RecipeCategory to capture the many-many relationship between Recipe and Category. You don't need to do this: SilverStripe will create the necessary database table itself.

    See these:

    Assuming you want both Recipes and Categories to be in the site tree, you need something like this:

    class Recipe extends Page {
    static $many_many = array('Categories' => 'Category');
    }

    class Category extends Page {
    static $belongs_many_many = array('Recipes' => 'Recipe');
    }

    For the navigation, if you want to show recipes for a particular category, you can't rely on the site tree since you have, as you say, only your Recipes page with its child Recipe pages. Instead, you need to get hold of the appropriate Category object, perhaps using <% Page(categories) %> - assuming the URLSegment of your categories page is categories - and in the template, use <% control Recipes %> to iterate through the recipes.

    You'll need to use ManyManyComplexTableField to get the CMS to manage your many-many relationship. See the tutorial in the link above.

    Toby

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: Highlight "category" instead of Parent page in menu Link to this post

    Toby, you've been really helpful, and I think I understand Silverstripe a little better thanks to you.
    Actually, I think I stuck to much to the tutorial #5

    So, what I did first was get rid of the useless dataobject. And then :

    In Recette.php :

    class Recette extends Page {
       
    static $many_many = array(
    'Categories' => 'Categorie'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();

    $categoriesTablefield = new ManyManyComplexTableField(
    $this,
    'Categories',
    'Categorie',
    array(
        'MenuTitle' => 'MenuTitle'
    ),
    'getCMSFields_forPopup'
    );
    $categoriesTablefield->setAddTitle( 'Catégories' );
        $categoriesTablefield -> setPermissions(array());
    $fields->addFieldToTab( 'Root.Content.Categories', $categoriesTablefield );

    return $fields;
    }
    }

    In Categorie.php :

    class Categorie extends Page {
       
    static $belongs_many_many = array(
    'Recettes' => 'Recette',
    );
       
    }

    And I added this function to my HomePage controller to retrieve all the recipes :

    function Recettes() {
          return DataObject::get("Recette");   
       }

    And it works well : I can select many categories every time I create a new "recette" (recipe) ; and every recipe is child of a category.

    Now, is it possible to go a little further, and add a ManyManyComplexTableField to the "Categorie" pages in the CMS, to select several existing Recipes every time I create a new category ?

    Thanks a lot !
    Stan

  • tobych
    Avatar
    Community Member
    97 Posts
  • stallain
    Avatar
    Community Member
    61 Posts

    Re: Highlight "category" instead of Parent page in menu Link to this post

    Hello again

    So, I tried to add a ManyManyComplexTableField to the "Categorie" pages in the CMS, but it doesn't work : since it's on the belongs_many_many side, I've got the same issue as here : http://www.silverstripe.org/dataobjectmanager-module-forum/show/264429#post264429

    Any solution ?

    Thanks again !

  • tobych
    Avatar
    Community Member
    97 Posts

    Re: Highlight "category" instead of Parent page in menu Link to this post

    Seems there's a bug in SilverStripe: http://open.silverstripe.org/ticket/4546

    This is such a basic thing... but then SilverStripe is still very young and there are many other basic things that don't work.

    This should be possible: as a poster points out, SiteTree is a subclass of DataObject, and in that sense is nothing special. Certainly I wouldn't recommend using many_many on both sides of a relationship: you're not supposed to do that, as I understand it. As the book points out, the only reason both many_many and belongs_many_many exist is to for the ORM to determine what to call the table (eg RecipeCategory or CategoryRecipe).

    Toby

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: Highlight "category" instead of Parent page in menu Link to this post

    And using $many_many at both sides simply doesn't work : the second ManyManyComplexTableField doesn't take into account the relationships set up in the first one.

  • MDrollette
    Avatar
    Community Member
    10 Posts

    Re: Highlight "category" instead of Parent page in menu Link to this post

    I'm in the same situation. Are there any solutions for this? I have People and Groups and people can be in multiple groups and I want to be able to manage the relations in ModelAdmin from both sides. ie. When creating a group I can select the people off that are in it, or when creating a person i can select the groups they are in... Is there no easy solution in SS? seems like this should the standard behavior of $many_many relationships in ModelAdmin no?

    2189 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.