Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Linking multiple pages using has_many and TreeMultiselectField

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

Page: 1
Go to End
Author Topic: 927 Views
  • maxx
    Avatar
    Community Member
    4 Posts

    Linking multiple pages using has_many and TreeMultiselectField Link to this post

    Hello!

    On my Homepage (PageType: HomePage) I would like to have a list of Teasers to use. Each Teaser is defined in a separate entry (PageType: HPTeaser) in the Content-Tree. The idea is to use a teaser on several pages, being able to change the content of the teaser in a single place.

    I used the following code in my HomePage.php file:

    class HomePage extends Page {
       
       public static $db = array(
       );
       
       public static $has_many = array(
          "HPTeasers" => "HPTeaser"
       );
       
       
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $hpteaserfield = new TreeMultiselectField(
             "HPTeasers",
             "Homepage Teasers",
             "HPTeaser"
          );   
             

          $fields->addFieldToTab("Root.Content.Main", $hpteaserfield);
                
          return $fields;
       }
       
    }

    The DropDown is correctly displayed while editing a page of type HomePage, but I have the following problems:

    - The selection is not limited to entries of type HPTeaser
    - When I select one ore more entries they are listed correctly, but nothing seems to be saved to the database (when I reload the editor the field HPTeasers is always empty

    So what could I do to fix these problems? And is there a better FieldType to use (eg. allowing Drag&Drop reordering of the selected Teasers)?

    Many Thanks for your help

    Maxx

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