Jump to:

5451 Posts in 1673 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » [SOLVED] Customize the TinyMCE's MediaForm upload folder

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

Page: 1
Go to End
Author Topic: 479 Views
  • svandragt
    Avatar
    Community Member
    43 Posts

    [SOLVED] Customize the TinyMCE's MediaForm upload folder Link to this post

    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?

  • svandragt
    Avatar
    Community Member
    43 Posts

    Re: [SOLVED] Customize the TinyMCE's MediaForm upload folder Link to this post

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

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: [SOLVED] Customize the TinyMCE's MediaForm upload folder Link to this post

    Hi svandragt,

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

  • svandragt
    Avatar
    Community Member
    43 Posts

    Re: [SOLVED] Customize the TinyMCE's MediaForm upload folder Link to this post

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

    }

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: [SOLVED] Customize the TinyMCE's MediaForm upload folder Link to this post

    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.

  • svandragt
    Avatar
    Community Member
    43 Posts

    Re: [SOLVED] Customize the TinyMCE's MediaForm upload folder Link to this post

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

    }

    479 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.