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

SS3 the method 'setdisplayfields' does not exist on 'GridField'


Reply

4 Posts   1021 Views

Avatar
Thomashv

17 July 2012 at 9:56pm (Last edited: 17 July 2012 9:57pm), Community Member, 33 Posts

Hi,

I have problems with the setdisplayfields method in SilverStripe 3.0.0. It says that the method 'setdisplayfields' does not exist on 'GridField'. Is there anybody else that has experiend similar problems? I post my code here and hope some of you have an idea of what I'm doing wrong here...

class HomePage extends Page {
   public static $db = array(
   
   );
   
   public static $has_many = array(
      'HomePageSlides' => 'HomePageSlide' // link the slide items
   );
      
   function getCMSFields() {
      $fields = parent::getCMSFields();
      
      $gridfieldConfig = GridFieldConfig::create()->addComponents(
         new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
         new GridFieldSortableHeader(),
         new GridFieldDataColumns(),
         new GridFieldPaginator(15),
         new GridFieldEditButton(),
         new GridFieldDeleteAction(),
         new GridFieldDetailForm()
      );
      
      

      $gridfield = new GridField("HomePageSlides", "Slides", $this->HomePageSlides(), $gridfieldConfig);
      
       $gridfield->setDisplayFields(array(
          'ID' => 'ID'
          ));
      
      $fields->addFieldToTab('Root.Slides', $gridfield);
      
      return $fields;
   }
}

class HomePage_Controller extends Page_Controller {

   public static $allowed_actions = array (
   );

   public function init() {
      parent::init();
   }
}

Avatar
James Bolitho

17 July 2012 at 10:32pm Community Member, 33 Posts

Hi Thomashv,

I was having the same issue with 'setFieldCasting'. It looks like the documentation on this needs to be updated because it seems that this has to be implemented differently now. I looked at the AssetAdmin.php in the cms/code/controllers/ folder for inspiration.

I would try this and see if it works:

$gridfield = new GridField("HomePageSlides", "Slides", $this->HomePageSlides(), $gridfieldConfig);

$columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns'); //new line that needs to be added to your code

$columns->setDisplayFields(array( //change $gridfield in your code to $columns
'ID' => 'ID'
));

Hope that helps.

Cheers,

Jim

Avatar
dpde

18 July 2012 at 9:07pm Community Member, 14 Posts

You can use the variable $summary_fields in your DataObject or something like this:

$gridColumns = new GridFieldDataColumns();
$gridColumns->setDisplayFields(array(
'ID' => 'ID',
'Title' => 'Titel'
));

$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
new GridFieldSortableHeader(),
//new GridFieldDataColumns(),
new GridFieldPaginator(10),
new GridFieldEditButton(),
new GridFieldDeleteAction(),
new GridFieldDetailForm()
$gridColumns
);

Avatar
Martin D.

27 August 2013 at 12:07pm Community Member, 21 Posts

$summary_fields doesn't seems to be working but Jim's solution does.

$columns = $listField->getConfig()->getComponentByType('GridFieldDataColumns');
$columns->setDisplayFields(array(
'Name' => 'Name',
'Prices' => 'Prices'
));

'setDisplayFields' is a method of GridFieldDataColumns, not the GridField itself.