Jump to:

23023 Posts in 11882 Topics by 2830 members

General Questions

SilverStripe Forums » General Questions » SS3 GridFieldSortableRows() module

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: 476 Views
  • merrick_sd
    Avatar
    Community Member
    89 Posts

    SS3 GridFieldSortableRows() module Link to this post

    does anyone know if this module can work with Model admin.

    works if i attach it to on Page class in cms fields

    eg:

    // Create a default configuration for the new GridField, allowing record editing
          $config = GridFieldConfig_RelationEditor::create();
          $config->addComponent(new GridFieldSortableRows('SortOrder'));
          $config->addComponent(new GridFieldBulkEditingTools());
          //$config->addComponent(new GridFieldBulkImageUpload());
          // Create a gridfield to hold the student relationship
          $columnField = new GridField(
           'Banners', // Field name
           'Banner', // Field title
           Banner::get()->sort("SortOrder"), // List of all related students
           $config
           );
           // Create a tab named "Students" and add our field to it
          $fields->addFieldToTab('Root.Banners', $columnField);
          

    but if i wanted it in model admin

    eg BannerAdmin

    class BannerAdmin extends ModelAdmin {

       public static $managed_models = array(
    'Banner',
       );

    static $url_segment = 'Banners'; // will be linked as /admin/products
       static $menu_title = 'Banners';

    }

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: SS3 GridFieldSortableRows() module Link to this post

    Hi Merrick

    This is untested code but there's no reason it shouldn't work. You'll want to override the getEditForm() function in ModelAdmin.php. So in your BannerAdmin class:

    class BannerAdmin extends ModelAdmin {

    public static $managed_models = array(
    'Banner',
    );

    static $url_segment = 'Banners'; // will be linked as /admin/products
    static $menu_title = 'Banners';

    function getEditForm($id = null, $fields = null) {
          $list = $this->getList();
          $exportButton = new GridFieldExportButton('before');
          $exportButton->setExportColumns($this->getExportFields());
          $listField = GridField::create(
             $this->modelClass,
             false,
             $list,
             $fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
                ->addComponent($exportButton)
                ->removeComponentsByType('GridFieldFilterHeader')
                ->addComponents(new GridFieldPrintButton('before'))
           ->addComponent(new GridFieldSortableRows('SortOrder')) //add your extra components here
          );

          // Validation
          if(singleton($this->modelClass)->hasMethod('getCMSValidator')) {
             $detailValidator = singleton($this->modelClass)->getCMSValidator();
             $listField->getConfig()->getComponentByType('GridFieldDetailForm')->setValidator($detailValidator);
          }

          $form = new Form(
             $this,
             'EditForm',
             new FieldList($listField),
             new FieldList()
          );
          $form->addExtraClass('cms-edit-form cms-panel-padded center');
          $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
          $form->setFormAction(Controller::join_links($this->Link($this->modelClass), 'EditForm'));
          $form->setAttribute('data-pjax-fragment', 'CurrentForm');

          $this->extend('updateEditForm', $form);
          
          return $form;
       }

    }

    476 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.