Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » FileDataObjectManager :: Auto fill popup field with filename

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 3012 Views
  • styleheavy
    Avatar
    Community Member
    8 Posts

    FileDataObjectManager :: Auto fill popup field with filename Link to this post

    Firstly, thanks UncleCheese for a fantastic module.

    I'm confronted with a situation where it would be a tremendous time saver to have a field in the popup pre filled with the file name that has just been uploaded.

    I have not been able to find a solution yet.

    Thanks,
    InnoSys.

  • UncleCheese
    Avatar
    4085 Posts

    Re: FileDataObjectManager :: Auto fill popup field with filename Link to this post

    Couldn't you just do..

    class MyDataObject extends DataObject
    {
    static $db = array (
    'SomeField' => 'Varchar(255)'
    );
    static $has_one = array (
    'Holder' => 'HolderPage',
    'SomeFile' => 'File'
    );

    public function getCMSFields()
    {
    return new FieldSet(
    new TextField('SomeField','Some field with a pre-populated value', $this->SomeFile()->Filename),
    new FileIFrameField('SomeFile')
    );
    }

    }

  • styleheavy
    Avatar
    Community Member
    8 Posts

    Re: FileDataObjectManager :: Auto fill popup field with filename Link to this post

    Hi UncleCheese,

    I had tested this with your example:
    new TextField('SomeField','Some field with a pre-populated value', $this->SomeFile()->Filename),

    The field does not populate with the Filename value.

    In an effort to debug, I tried:
    new TextField('SomeField','Some field with a pre-populated value', 'test string'),

    And the field doesn't populate with the 'test string' value either.

    Can this be confirmed?

    Thanks,
    InnoSys.

  • styleheavy
    Avatar
    Community Member
    8 Posts

    Re: FileDataObjectManager :: Auto fill popup field with filename Link to this post

    Here's the complete code example:

    /**
    * Defines the Audiofile DataObject
    */

    class AudioFile extends DataObject {
    static $db = array (
    'Title' => 'Text',
    'Mood' => 'Text',
    'Rating' => 'Int'
    );

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

    public function getCMSFields_forPopup() {

    return new FieldSet(
    new TextField('Title','Title For Song',$this->Attachment()->Filename),
    new FileIFrameField('Attachment'),
    new Dropdownfield('Mood','Mood',
    array('blue'=>'Blue','pink'=>'Pink','green'=>'Green','orange'=>'Orange')
    )

    );
    }
    }

  • styleheavy
    Avatar
    Community Member
    8 Posts

    Re: FileDataObjectManager :: Auto fill popup field with filename Link to this post

    The result is the same when you import, or upload a file.

    The Title field does not get populated with Filename.

  • UncleCheese
    Avatar
    4085 Posts

    Re: FileDataObjectManager :: Auto fill popup field with filename Link to this post

    Yeah, that's because the dataobject gets written on the upload, not just the file. So you're going to have to do something a little smarter in your dataobject class.

    Maybe something like...

    public function onAfterWrite()
    {
    parent::onAfterWrite();
    if(!$this->Title && $this->Attachment()) $this->Title = $this->Attachment()->Filename;
    $this->write();
    }

  • styleheavy
    Avatar
    Community Member
    8 Posts

    Re: FileDataObjectManager :: Auto fill popup field with filename Link to this post

    UncleCheese,

    That put me on the right track!

    Using the $this->write(); on onAfterWrite() actually destroyed the window contents, however, I found a reference to some other posts you were involved in regarding onBeforeWrite().

    It appears if we set the $this->Title with onBeforeWrite(), the field is pre-populating with the intended value when page is written out.

    Here's the working snippet:

    public function onBeforeWrite() {
    parent::onBeforeWrite();
    if(!$this->Title && $this->Attachment()) $this->Title = $this->Attachment()->Filename;
    }

    Thanks your your help.

    Cheers,
    InnoSys.

  • styleheavy
    Avatar
    Community Member
    8 Posts

    Re: FileDataObjectManager :: Auto fill popup field with filename Link to this post

    I just wanted to quickly add for anyone that is interested:

    The above code is a great way to set defaults values for any field that you may have such as dropdowns.

    In the event that you have many files and don't want to step trough each one of them with the dialogue, set the default values for your fields with the example above, and you can simply just hit close on the file addition lightbox, and it will populate all the fields with the default values you had set.

    This is a great time saver for adding many files and the provided defaults are good enough.

    InnoSys.

    3012 Views
Page: 1 2
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.