Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Sorting CMSFields

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

Page: 1
Go to End
Author Topic: 153 Views
  • Bereusei
    Avatar
    Community Member
    94 Posts

    Sorting CMSFields Link to this post

    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?

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: Sorting CMSFields Link to this post

    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

  • Bereusei
    Avatar
    Community Member
    94 Posts

    Re: Sorting CMSFields Link to this post

    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?

    153 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.