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.

Customising the CMS

[SOLVED] Customize the TinyMCE's MediaForm upload folder


Reply

6 Posts   653 Views

Avatar
svandragt

18 April 2013 at 4:00am Community Member, 44 Posts

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

18 April 2013 at 8:39pm Community Member, 44 Posts

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

19 April 2013 at 8:13pm Community Member, 146 Posts

Hi svandragt,

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

Avatar
svandragt

1 May 2013 at 3:52am (Last edited: 1 May 2013 3:54am), Community Member, 44 Posts

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

1 May 2013 at 4:18am Community Member, 146 Posts

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

1 May 2013 at 8:36pm Community Member, 44 Posts

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

}