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

getCMSFields conditionally showing field in modeladmin




19 August 2012 at 11:30am Community Member, 62 Posts

I was looking a while for the answer to this but couldn't find it.

I have a DataObject "Testimonial" that is a part of a Page class "BookPage", and I've also added testimonial as a ModelAdmin to list all the testimonials and the books they are under. I used the ModelAdmin to switch Books that the testimonial belongs in, with a "BookPage" dropdown....

   public function getCMSFields(){
      $fields = new FieldList();
      $fields->push( new DropdownField(
       'Please choose a Book',
       $this->getBookSelection() ) );

   function getBookSelection()
    if($Pages = DataObject::get('BookPage'))
       return $Pages->map('ID', 'Title', 'Please Select');
       return array('No Objects found');

I'd like to be able to show the dropdown menu only when I'm adding or editing a testimonial from the modelAdmin (/admin/testimonial/Testimonial/EditForm/field/Testimonial/item/2/edit) ,
but not when I'm accessing the testimonial from editing the pages (/admin/pages/edit/EditForm/field/Testimonials/item/new).