Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » TableListField to show uploaded file link?

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

Page: 1
Go to End
Author Topic: 2168 Views
  • danietsai
    Avatar
    Community Member
    2 Posts

    TableListField to show uploaded file link? Link to this post

    Hi everyone,

    I have tried to search for an answer of my question in the forum or document, but could not find one.

    I have a custom form (a contact form for my company website) which includes a File field. I want to show the list of all submissions in the admin page using TableListField. My code is listed below. I got the following error message "__call() Method 'forTemplate' not found in class 'File'". If I remove Attachment from below, it just works perfectly fine.

    I understand that a File field doesn't have forTemplate method. The problem is how can I get the file download link correctly?

    Thanks in advance if anyone can give me a hand. Appreciate that.

    function getReportField() {      
       $resultSet = new DataObjectSet();
       $filter = '';
       $sort = "ContactPageSubmission.ID ASC";
       $join = '';
       $instance = singleton('ContactPageSubmission');      
       $query = $instance->buildSQL($filter, $sort, null, $join);

       $report = new TableListField(
          'ContactPageSubmissionReport',
          'ContactPageSubmission',
          array(
             'ID' => 'ID',
             'Created' => 'Date/Time',
             'FirstName' => 'First Name',
             'LastName' => 'Last Name',
             'Email' => 'Email Address',
             'Attachment' => ''
          )
       );
          
       $report->setCustomQuery($query);
          
       $report->setFieldFormatting(array(
          'Email' => '<a href=\"mailto: $Email\" title=\"Email $FirstName\">$Email</a>',
          'Attachment' => '<a href=\"$Attachment.URL\" title=\"$Attachment.Title\">$Attachment.Title</a>'
       ));
          
       $report->setFieldCasting(array(
          'Created' => 'Date->Nice'
       ));
          
       $report->setShowPagination(true);
       if(isset($_REQUEST['printable'])) {
          $report->setPageSize(false);
       } else {
          $report->setPageSize(50);
       }
          
       $report->setPermissions(array(
          'export',
          'delete',
          'print'
       ));
          
       return $report;
    }

  • danietsai
    Avatar
    Community Member
    2 Posts

    Re: TableListField to show uploaded file link? Link to this post

    I should have provided my data model class as below:

    class ContactPageSubmission extends DataObject {
    static $db = array(
    'FirstName' => 'Text',
    'LastName' => 'Text',
    'Email' => 'Text',
    'Comments' => 'Text'
    );

    static $has_one = array(
    'Attachment' => 'File',
    );
    }

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: TableListField to show uploaded file link? Link to this post

    change the following line:

    <a href=\"$Attachment.URL\" title=\"$Attachment.Title\">$Attachment.Title</a>

    To

    <a href=\"$Attachment.Filename\" title=\"$Attachment.Title\">$Attachment.Title</a>

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: TableListField to show uploaded file link? Link to this post

    By the way, I forgot to ask, where do you put the 'function getReportField()', is it in ContactUsPage.php ( class ContactUsPage extends Page )? assuming ContactUsPage.php is the page which contains the form. Thanks!

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