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

Linking multiple pages using has_many and TreeMultiselectField


Reply

978 Views

Avatar
maxx

10 May 2010 at 8:51am Community Member, 4 Posts

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