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.

Customising the CMS /

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

Customize Upload Manager for Images


Go to End


4 Posts   3391 Views

Avatar
s_a_m_y

Community Member, 31 Posts

6 July 2013 at 1:17am

Hi everyone,

for a project I have set up a nice project catalog with the help of the DataObjectsAsPage-Module. Everything works beautifully with SS3. Each project can have multiple categories and multiple images attached. This is also all working great. Images and categories can be defined in separate tabs of the project admin area.
However my customer now wants to add a photographer to each image and I am struggling somewhat on how to add an extra field to the image uploadField. I figured I need a class that extends Image or DataExtension and adds a table to the database. I defined this extended class in my project page as $many_many relation. I can add images but the extra field doesn't show.

I've tried something like, trying to adapt the doc on UploadFileds:

class ExtendedImage extends DataExtension {
	static $db = array(
		'Photographer' => 'Varchar',
		);
			
	static $has_one = array(
                'Image' => 'Image'
                );	
    
    function getCustomFields() {
        $fields = new FieldList();
        $fields->push(new TextField('Photographer', 'Fotograf'));
        return $fields;
    }
}

class Project extends DataObjectAsPage 
{
[...]
static $many_many = array(
        'ExtendedImages' => 'ExtendedImage',
        'Categories' => 'Category'
    );
 public function getCMSFields() {
        $fields = parent::getCMSFields();
        [...]
        $fields->addFieldToTab("Root.Images", $uploadField->setFileEditFields('getCustomFields'););

        return $fields;

But I get an internal server error. This must be fairly simple. I'm just not seeing it. All I really need is an additional Textfield in the UploadField of the image Class. I'd be very grateful for any hint on this.

Sam

Avatar
s_a_m_y

Community Member, 31 Posts

6 July 2013 at 1:54am

OK. I am getting there slowly. Here is the current code:

class ExtendedImage extends Image {
	static $db = array(
		'Photographer' => 'Varchar',
		);
			
	static $has_one = array(
        'Image' => 'Image'
    );	
    
    public function getCustomFields() {
        $fields = new FieldList();
        $fields->push(new TextField('Title', 'Titel'));
        $fields->push(new TextField('Filename', 'Dateiname'));
        $fields->push(new TextField('Alt', 'Alt'));
        $fields->push(new TextField('Photographer', 'Fotograf'));
        $fields->push(new DropdownField('Owner', 'Eigentümer'));
        $fields->push(new DropdownField('Folder', 'Ordner'));
        return $fields;
    }
}

class Project extends DataObjectAsPage 
{ 
[...] 
static $many_many = array( 
'ExtendedImages' => 'ExtendedImage', 
'Categories' => 'Category' 
); 
public function getCMSFields() { 
$fields = parent::getCMSFields(); 
[...] 
$uploadField = new UploadField('Photo','Bilder');
$uploadField->setConfig('fileEditFields', 'getCustomFields');
$fields->addFieldToTab("Root.Bilder", $uploadField);

return $fields;

I now can access the tab an all field I defined in getCustomFields are being offered. Title will also grasp the title from the image Class as well as the filename. However owner and folder are empty dropdownFields now. I am not sure the field name is correct here or if I have to reset the relation. But as title and filename are rendered correctly I assume it's somthing else I'm not getting. Still any help appreciated.

Samy

Avatar
karantan

Community Member, 3 Posts

23 August 2013 at 9:46pm

the last code was correct. but you have forgot to /dev/build your site. (90% that this is the problem)

i have another question. the method that samy described overwrites the default view which contains image and data of that image (type, dimensions, path, ...).

how do you only extend the default fields?

Avatar
spotelf13

Community Member, 1 Post

3 July 2017 at 4:24pm

4 Years late to the party lol :o but for anyone wanting to know the answer to how we can extend the default image fields, heres how you do it:

Instead of creating a whole new FieldList and replacing the default fields on the image object as seen with the example below...

    public function getCustomFields() {
        $fields = new FieldList();
        $fields->push(new TextField('Title', 'Titel'));
        $fields->push(new TextField('Filename', 'Dateiname'));
        $fields->push(new TextField('Alt', 'Alt'));
        $fields->push(new TextField('Photographer', 'Fotograf'));
        $fields->push(new DropdownField('Owner', 'Eigentümer'));
        $fields->push(new DropdownField('Folder', 'Ordner'));
        return $fields;
    }

You can simply just utilise updateCMSFields on your DataExtension

public function updateCMSFields(FieldList $fields)
{
      $photographer = new TextField('Photographer', 'Photographer');
      $fields->addFieldToTab('Root.Main', $photographer);
 }

By updating the FieldList with the new fields (not overwriting) will solve your problem for the default view being removed :)

Hope this helps,
Spotelf13, signing out.