Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Displaying link to file in GridField

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

Page: 1
Go to End
Author Topic: 455 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Displaying link to file in GridField Link to this post

    I have a GridField diplaying as $has_many realtion with files, I'm tryin to add a column with a link to the file. I thought I'd be able to do it setFieldFormatting but at that level it seems to only have access the page object even though is I remove it, the gridfield does show the $Filename

       public function FileForm(){
          
          $config = GridFieldConfig_Base::create();
          
          
          
          $fileField = new GridField("Files", "Documents", $this->Files(), $config);
          
          
          $dataColumns = $fileField->getConfig()->getComponentByType('GridFieldDataColumns');
          
          $dataColumns->setDisplayFields(array(
             'Select' => 'Select',
             'Title' => 'Title',
             'Description' => 'Description',
             'Size' => 'Size',
             'LastEdited' => 'Changed',
             'Filename'=> 'Download'
          ));

          
          
          $dataColumns->setFieldFormatting(array(
             "Filename" => "<a href=/'$Filename/'>Download</a>"
          ));
          
          $fields = new FieldList (
             $fileField
          );
          
          $actions = new FieldList(FormAction::create("doEmail")->setTitle("Send Files"));
          
          return new Form($this, "FileForm", $fields, $actions);
       }   

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: Displaying link to file in GridField Link to this post

    I've gotten a little farther using a new GridField Componant

    class GridFieldDownloadButton implements GridField_ColumnProvider {
       
       public function augmentColumns($field, &$cols) {
          if(!in_array('Download', $cols)) $cols[] = 'Download';
       }
       
       public function getColumnsHandled($field) {
          return array('Download');
       }
       
       public function getColumnContent($field, $record, $col) {
          if($record->canView()) {
             $data = new ArrayData(array(
                'Link' => $record->Filename
             ));
             return $data->renderWith('GridFieldDowloadButton');
          }
       }
       
       public function getColumnAttributes($field, $record, $col) {
          return array('class' => 'col-link');
       }

       public function getColumnMetadata($gridField, $col) {
          return array('title' => "Link to File", 'sortable' => false);
       }

       
    }

    But the column renders as sortable with a blank Title

    Any suggestions?

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