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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

FileDataObjectManager :: Auto fill popup field with filename


Go to End
Reply

9 Posts   3011 Views

Avatar
styleheavy

8 October 2009 at 3:37pm Community Member, 8 Posts

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

8 October 2009 at 4:16pm 4085 Posts

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

3 November 2009 at 6:36pm Community Member, 8 Posts

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

4 November 2009 at 7:11am Community Member, 8 Posts

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

4 November 2009 at 7:16am Community Member, 8 Posts

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

The Title field does not get populated with Filename.

Avatar
UncleCheese

4 November 2009 at 7:51am 4085 Posts

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

4 November 2009 at 8:23am Community Member, 8 Posts

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

4 November 2009 at 9:48am Community Member, 8 Posts

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