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 /

[Solved] Limiting TreeMultiSelect


Reply


2 Posts   735 Views

Avatar
phillprice

Community Member, 6 Posts

16 April 2011 at 11:42pm

I have a working TreeMultiSelect, the sourceObject I've used is a subtype of page (Gallery).

However the field shows the whole SiteTree, rather than just these subtypes. Thankfully it ignores if you try to pick a normal page but it still bugs me.

Probably unrelated but Photograph is managed in ModelAdmin

class Photograph extends DataObject {

   ...

   static $many_many = array(
      'Galleries' => 'Gallery'
   );
   
   ...

   function getCMSFields() {
      $fields = parent::getCMSFields();
      $fields->addFieldToTab("Root.Galleries", new TreeMultiselectField('Galleries', 'Choose Galleries to show Photo', "Gallery"));
      return $fields;
   }

   ...

}

class Gallery extends Page {

   ...

   static $belongs_many_many = array(      
      'Photographs' => 'Photograph'
   );

}

class Gallery_Controller extends Page_Controller {
      
   ...

}

Avatar
Shane Garelja

Community Member, 18 Posts

16 April 2011 at 11:55pm

Hi Phill,

This is the correct behaviour when passing in a 'hierarchy' object as the 3rd parameter. It needs to display all types in order to generate the tree view.

If you wish to only see your Gallery page types then you can pass an array of key/values pairs as the 3rd param instead. This will mean that you have a flat list to select from as a hierarchy no longer exists. You could do this like so:

function getCMSFields() {
   $fields = parent::getCMSFields();
   $galleryPages = DataObject::get('Gallery');
   $fields->addFieldToTab("Root.Galleries", new TreeMultiselectField('Galleries', 'Choose Galleries to show Photo', $galleryPages->map()));
   return $fields;
}