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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Sorting CMSFields


Reply


3 Posts   194 Views

Avatar
Bereusei

Community Member, 94 Posts

5 February 2014 at 1:42am

Hey guys,

in my modeladmin (SS3.1) I have many different fields: normal TextFields, UploadFields, GridFields, many_many_extraFields, DropdownFields mapped with has_one-Relations.

The problem is that my interface looks a little bit messy. First normal TextFields are displayed, than the UploadField, than the GridField, etc...
Anyway it make no difference what position the single fields have in my code.

Any idea, how I can sort the fields without using jQuery?

Avatar
kinglozzer

Community Member, 151 Posts

5 February 2014 at 5:29am

Hi Bereusei,

The fields are ordered in whichever order you add them to the FieldList. For example:

$fields = new FieldList();
$fieldA = new TextField('Foo', 'Foo');
$fieldB = new TextField('Bar', 'Bar');

$fields->push($fieldB);
$fields->push($fieldA);

This will result in 'Field B' being added before 'Field A'.

If you're not creating the fields yourself, and are allowing the form scaffolder to create them for you, you can re-order them with:

$fields = parent::getCMSFields();
$fields->changeFieldOrder(
'Foo',
'Bar'
);
return $fields;

Loz

Avatar
Bereusei

Community Member, 94 Posts

6 February 2014 at 4:10am

Edited: 06/02/2014 4:11am

Thanks kinglozzer. It works nice on regular fields.
Any idea how this would work on many_many_extrafields?

class ProductPage extends DataObject{

static $many_many = array(
      'Products' => 'Product'
   );

   public static $many_many_extraFields = array(
'Products' => array('Position' => 'Varchar(255)')
);

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

$productFields = singleton('Product')->getCMSFields();
$productFields->addFieldToTab('Root.Main', DropdownField::create('ManyMany[Position]', 'Position on page', array("top", "bottom")));

      $gridfieldConfig = GridFieldConfig_RelationEditor::create();
      $gridfieldConfig->getComponentByType('GridFieldDetailForm')->setFields($productFields);

$fields->addFieldsToTab("Root.Main", array(
   ...
   $gridfield = GridField::create("Products", "Product", $this->Products(), $gridfieldConfig)
));
}

class Product extends DataObject{
...
      $fields->fieldByName("Root.Main")->push($OptionSetField);
      $fields->fieldByName("Root.Main")->push($uploadField);
      $fields->fieldByName("Root.Main")->push(ManyMany[Position]);
$fields->fieldByName("Root.Main")->push($Title);
}

I have the bad feeling, that this doesn´t work anyway, does it?