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 /

Storing and Displaying a Collection of Testimonials


Reply


35 Posts   5591 Views

Avatar
juneallison

Community Member, 108 Posts

26 July 2011 at 3:38am

@martimiz - Thanks for the suggestion! I had actually tried that and I still have the same problem. The database stops rebuilding as soon as it hits the Category table. Thanks anyway!

Avatar
swaiba

Forum Moderator, 1808 Posts

26 July 2011 at 3:44am

what error do you see?

these are my two "wrapper functions" for converting to a multiselect

public static function ChangeManyManyFieldToMultiSelect($doCurrent, $fields,$strTabName,$strDataObjectName,$strFieldName,$strInsertAfterFieldName,$strOptionTextField='Name',$filter='', $controller = null) {
      $fields->removeFieldFromTab('Root',$strTabName);
      $dos = DataObject::get($strDataObjectName,$filter);
      $map = $dos ? $map = $dos->toDropdownMap('ID',$strOptionTextField) : array();
      $fields->insertBefore(new MultiSelectField($strFieldName,$strTabName, $map),$strInsertAfterFieldName);
      return $fields;
}

public static function ChangeManyManyFieldToMultiSelectAddToTab($doCurrent, $fields,$strTabName,$strDataObjectName,$strFieldName,$strAddToTab,$strOptionTextField='Name',$filter='') {
      $fields->removeFieldFromTab('Root',$strTabName);
      $dos = DataObject::get($strDataObjectName,$filter);
      $map = $dos ? $map = $dos->toDropdownMap('ID',$strOptionTextField) : array();
      $fields->addFieldToTab($strAddToTab, new MultiSelectField($strFieldName,$strTabName, $map));
      return $fields;
}

Avatar
juneallison

Community Member, 108 Posts

26 July 2011 at 4:21am

@swaiba - Thanks for your reply. I turned on dev mode in my config file and don't see any errors when I rebuild the database. It just stops at the category table.

Are you saying I need to use something like the code you just shared instead of this...
$source = DataObject::get('Category');
new MultiSelectField(
"Categories", // Relationship
"Testimonial Categories", // Field name
$source->map('ID','Title') // Source records (array)
);

...if so I may have some questions.

Thanks for your help!

Avatar
swaiba

Forum Moderator, 1808 Posts

26 July 2011 at 4:28am

well that code fragment is simply not going to compile (I know you have lifted it from http://silverstripe.org/multiselectfield-module/, but it does expect some knowledge as how to use it).

Instead I've given you exactly what I use to implement it - Whenever I do the same 3/4 lines over and again I make it into a function.

Avatar
juneallison

Community Member, 108 Posts

26 July 2011 at 4:45am

Thanks for bearing with me! To make sure I understand correctly, that "$source=" code snippet will still go into my Testimonial.php file, right?

I then need to add the two functions you shared to my code. Do these belong in MultiSelectField.php file or somewhere else? And I can use them exactly as they are or do I need to change anything?

Thanks!

Avatar
swaiba

Forum Moderator, 1808 Posts

26 July 2011 at 5:47am

that "$source=" code snippet will still go into my Testimonial.php file, right?

no, but I think I should have provided a better example... try this... it is to replace that $source= code

$fields->removeFieldFromTab('Root','Categories');
$dos = DataObject::get('Category ');
$map = $dos ? $map = $dos->toDropdownMap() : array();
$msf = new MultiSelectField("Testimonial Categories",'Categories', $map);
$fields->insertAfter($msf,'Title');

Avatar
juneallison

Community Member, 108 Posts

26 July 2011 at 6:17am

thanks swaiba! does the code snippet that you just shared still needs to go inside of a function? because it doesn't appear to work on its own.

thanks again!

Avatar
swaiba

Forum Moderator, 1808 Posts

26 July 2011 at 6:57am

yes of course...

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

   $fields->removeFieldFromTab('Root','Categories');
   $dos = DataObject::get('Category ');
   $map = $dos ? $map = $dos->toDropdownMap() : array();
   $msf = new MultiSelectField("Testimonial Categories",'Categories', $map);
   $fields->insertAfter($msf,'Title');

   return $fields;
}