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

[SOLVED] Customize the TinyMCE's MediaForm upload folder


Go to End


6 Posts   2435 Views

Avatar
svandragt

Community Member, 44 Posts

18 April 2013 at 4:00am

I'm looking to change the default folder that files get uploaded into, when using TinyMCE's editor using a DataExtension. No success so far, so looking for help!

The HtmnlEditorField's MediaForm method sets the default upload folder on line 433 (3.0.5):

$computerUploadField->setFolderName(Upload::$uploads_folder);

Also it allows people to extend the method:

		// Allow other people to extend the fields being added to the imageform 
		$this->extend('updateMediaForm', $form);

so I've created a dataextension but have no luck getting any classes added (unobtrusive way to ensure my extension is loading correctly.

// mysite/code/framework/HtmlEditorFieldExtension.php:
class HtmlEditorFieldExtension extends DataExtension {

	public function updateMediaForm(Form $form) {
		$computerUploadField = $form->Fields()->fieldByName(_t('HtmlEditorField.FROMCOMPUTER','From your computer'));
		$computerUploadField->addExtraClass('TEST');
	}
 
}

// config.php:
Object::add_extension('Page', 'HtmlEditorFieldExtension');

Any ideas?

Avatar
svandragt

Community Member, 44 Posts

18 April 2013 at 8:39pm

changed code to this but no luck yet:

<?php

/* mysite/code/framework/HtmlEditorFieldExtension.php */
class HtmlEditorFieldExtension extends DataExtension {

	public function updateMediaForm(Form $form) {
		$computerUploadField = $form->Fields()->fieldByName('AssetUploadField');
		$computerUploadField->setFolderName(Upload::$uploads_folder . "/custom" );
	}
 
}

/* mysite/_config.php */
...
Object::add_extension('HtmlEditorField', 'HtmlEditorFieldExtension');

Avatar
kinglozzer

Community Member, 187 Posts

19 April 2013 at 8:13pm

Hi svandragt,

Try including the tabset/tab names in the function call, i.e. ->fieldByName("MediaFormInsertMediaTabs.FromComputer.AssetUploadField");

Avatar
svandragt

Community Member, 44 Posts

1 May 2013 at 3:52am

Edited: 01/05/2013 3:54am

Thanks kinglozzer. I've tried this but the problems seems to be that the updateMediaForm isn't called.
I've put in a log_writer but i'm never getting the user error:

I've added to mysite/_config.php:

SS_Log::add_writer(new SS_LogEmailWriter('sander.vandragt@angus.ac.uk'), SS_Log::NOTICE, '<=');
Object::add_extension('HtmlEditorField', 'HtmlEditorFieldExtension');

mysite/code/framework/HtmlEditorFieldExtension.php:

class HtmlEditorFieldExtension extends DataExtension {

	public function updateMediaForm(Form $form) {
			user_error("helo?", E_USER_ERROR);
				$computerUploadField = $form->Fields()->fieldByName("MediaFormInsertMediaTabs.FromComputer.AssetUploadField");
		$computerUploadField->setFolderName(Upload::$uploads_folder . "/custom" );
	}

}

Avatar
kinglozzer

Community Member, 187 Posts

1 May 2013 at 4:18am

Hi svandragt,

I think you might be adding your extension to the wrong class; I added mine to HtmlEditorField_Toolbar:

Object::add_extension('HtmlEditorField_Toolbar', 'HtmlEditorFieldExtension');

The _Toolbar class, and others, are in the same HtmlEditorField.php file.

Avatar
svandragt

Community Member, 44 Posts

1 May 2013 at 8:36pm

You were right once again kinglozzer, many thanks!
The code below works, I might turn this into a module with the foldername made more customizable.

mysite/_config.php:

Object::add_extension('HtmlEditorField_Toolbar', 'HtmlEditorFieldExtension');

mysite/code/framework/HtmlEditorFieldExtension.php:

<?php

class HtmlEditorFieldExtension extends DataExtension {

	public function updateMediaForm(Form $form) {
		$page_id = $_SESSION['CMSMain']['currentPage'];
		$page_urlsegment = SiteTree::get()->byID($page_id)->URLSegment;

		$computerUploadField = $form->Fields()->dataFieldByName('AssetUploadField');
		$computerUploadField->setFolderName(sprintf("%s/pages/%s-%s", Upload::$uploads_folder, $page_id, $page_urlsegment));
	}

}