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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

FileDataObjectManager :: Auto fill popup field with filename


Go to End


9 Posts   4579 Views

Avatar
styleheavy

Community Member, 8 Posts

8 October 2009 at 3:37pm

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.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 October 2009 at 4:16pm

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')
);
}

}

Avatar
styleheavy

Community Member, 8 Posts

3 November 2009 at 6:36pm

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.

Avatar
styleheavy

Community Member, 8 Posts

4 November 2009 at 7:11am

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')
)

);
}
}

Avatar
styleheavy

Community Member, 8 Posts

4 November 2009 at 7:16am

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

The Title field does not get populated with Filename.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

4 November 2009 at 7:51am

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();
}

Avatar
styleheavy

Community Member, 8 Posts

4 November 2009 at 8:23am

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.

Avatar
styleheavy

Community Member, 8 Posts

4 November 2009 at 9:48am

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.

Go to Top