Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » How to show image name or url in ManyManyComplexTableField?

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1498 Views
  • SilverRay
    Avatar
    Community Member
    167 Posts

    How to show image name or url in ManyManyComplexTableField? Link to this post

    OK, I have the following dataobject:

    <?php

    class TestObject extends DataObject {

       static $db = array(
       );

       static $has_one = array(
       'FileName' => 'Image'
       );

       static $belongs_many_many = array(
          'TestPages' => 'TestPage'
       );

       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push( new ImageField( 'FileName', 'ColorImage' ) );
          return $fields;
       }

    }
    ?>

    And then I have the following code in TestPage.php:

       static $many_many = array(
          'TestObjects' => 'TestObject'
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $testTablefield = new ManyManyComplexTableField(
             $this,
             'TestObjects',
             'TestObject',
             array(
                'FileNameID' => 'File'
             ),
             'getCMSFields_forPopup'
          );
          $testTablefield->setPageSize(100);
          $testTablefield->setAddTitle( 'a TestObject' );
          $fields->addFieldToTab( 'Root.Content.TestObjects', $testTablefield );
          return $fields;
       }

    Everything works, saves, etc. But: in the many-many table in the TestObjects tab, the FileName shows up as an ID. If I remove the "ID" from FileName, I get a server error. How can I show the name of the file, or the url, in the many-many table?

    Thanks!

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: How to show image name or url in ManyManyComplexTableField? Link to this post

    ok thats because the field on your TestObject isnt 'FileName' as you defined in your has_one. Rather it creates a seperate table called FileName and then a column in your testobject called 'FileNameID' so from your TestObject the only field on FileName it knows about is is FileNameID.

    Now the most straight forward way to get this to work is to define a method on TestObject which returns the File as a dataobject. So on TestObject you can add something like

    function getImageFileName() {
    if($this->FileNameID) {
    $file = DataObject::get_by_id("Image", $this->FileNameID); // returns the file object
    return ($file) ? $file->Filename : "No File Name";
    }
    return "No File Name";
    }

    then in your table field you can call ImagesFileName ?.

    $testTablefield = new ManyManyComplexTableField(
    $this,
    'TestObjects',
    'TestObject',
    array(
    'ImagesFileName' => 'File Name'
    ),
    'getCMSFields_forPopup'
    );


    Something like that at least

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: How to show image name or url in ManyManyComplexTableField? Link to this post

    Thanks willr for your answer, I will try. I'm kind of swamped right now, but I'll report back, I promise!

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