Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Using FileIFrameField in the CMS

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

Page: 1 2
Go to End
Author Topic: 4346 Views
  • Mr. Matt
    Avatar
    Community Member
    5 Posts

    Using FileIFrameField in the CMS Link to this post

    Hey,

    In the CMS I am trying to use an FileIFrameField and restrict it to only uploading PDF files by doing the following:

    $fileUpload = new FileIFrameField( 'PDFUpload', 'PDF Brocure' );
    $fileUpload->setAllowedExtensions( array( 'pdf' ) );

    $fields->addFieldToTab( 'Root.Content.Main', $fileUpload );

    But using this lets me upload any file. Are the extra controls you get e.g. setAllowedExtensions() or setAllowedMaxFileSize() only available in the front end and not the CMS?

    Forgot to add also that this is SS 3.2.1

    Thanks,

    Matt

  • philthegr81
    Avatar
    Community Member
    2 Posts

    Re: Using FileIFrameField in the CMS Link to this post

    I'd like to bump this because I'm having the same problem, albeit outside of CMS. I've installed the blog module and am trying to extend it to allow blog posters to attach an MP3 file. I've gotten this to work with an object of type FileField, and I assume setAllowedExtensions should work on FileIFrameField since it's a subclass of FileField. But, like Mr. Matt, I'm able to attach any type of file. My code is similar to his. This is also on SS version 2.3.1.

    Thanks!
    Phil.

  • aimcom
    Avatar
    Community Member
    8 Posts

    Re: Using FileIFrameField in the CMS Link to this post

    I tried to restrict the maximum filesize for a ImageField, but it didn't work (using SilverStripe 2.3.6). I set it to 1 MB, but I was able to upload a picture with over 2 MB.

    $navigationImage = new ImageField('NavigationImage', 'Navigation image');
    $navigationImage->setAllowedMaxFileSize(array('*' => 1048576));
    $fields->addFieldToTab('Root.Content.Navigation', $navigationImage);

    To be sure that the documentation of the API is correct and the maximum filesize has to be defined in bytes, I tried some smaller numbers like 100, but I was able to upload my 2 MB picture anyway. Now I would like to know if there is any solution for this problem.

  • mattclegg
    Avatar
    Community Member
    56 Posts

    Re: Using FileIFrameField in the CMS Link to this post

    In 2.4+ you want something like;

    $a=array('pdf');
    $f = new FileIFrameField("PDFFileFont","Attached PDF's");
    $f->allowedExtensions = $a;

    $f_validator = new Upload_Validator();
    $f_validator->setAllowedExtensions($a);
    $f->setValidator($f_validator);

  • moloko_man
    Avatar
    Community Member
    72 Posts

    Re: Using FileIFrameField in the CMS Link to this post

    when implementing mattclegg's code, I get the following error:

    [User Error] Uncaught Exception: Object->__call(): the method 'setvalidator' does not exist on 'FieldSet'

    UPDATE:
    Nevermind, I found my error. I had a type in the setValidator line.

  • marc79
    Avatar
    Community Member
    65 Posts

    Re: Using FileIFrameField in the CMS Link to this post

    Hi mattclegg,

    I am trying to allow FileIFrameField to upload mp3 files but am having no luck so far. On my page so far I have the below:

    //----------------------------------//

    class ProductPage extends Page {
    static $db = array(
    );

    static $has_one = array(
       'CoverArt' => 'Image',
       'SampleTrack' => 'File',
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
       $fields->addFieldToTab('Root.Content.CoverArt', new ImageField('CoverArt', "Upload image below", null, null, null, "covers"));
       $fields->addFieldToTab('Root.Content.SampleTrack', new FileIFrameField('SampleTrack', "Upload MP3 below", null, null, null, "music"));
    return $fields;
       
    }
    }
    //----------------------------------//

    But I am not sure where I would add in the validator code that you suggested. My FileIFrameField is working as I can import other file types just not mp3.

    Any help you can offer would be great.

    Cheers

    Marc

  • moloko_man
    Avatar
    Community Member
    72 Posts

    Re: Using FileIFrameField in the CMS Link to this post

    Are you allowing mp3s to be uploaded by setting the allowedExtension?

    $a=array('mp3');
    $fields->allowedExtensions = $a;

    The validator code should go in your getCMSFields() function, just put them after all your addFieldToTab lines but before you return $fields.
    I haven't tested that, but it should work for you.

  • marc79
    Avatar
    Community Member
    65 Posts

    Re: Using FileIFrameField in the CMS Link to this post

    Hi moloko_man,

    Thanks for your reply.

    After posting, and I have to say I don't know what I did, but it started working, without adding any new code. I can now upload mp3 files to a 'music' folder I have created within the assets folder.

    However, I now can't figure how to get the file to appear on the site and allow users to play the track. I've tried simply adding $SampleTrack as I had done with $CoverArt but it just throws up the 'Sorry, there was a problem with handling your request.' error msg.

    Do I need a player? I was hoping just to use either <embed> or <audio>.

    UPDATE: Sorry, just thought I should add in the .ss code I am working with:

    <div id="ProductPage">
    <div id="ProductPageLeftCol">
    $CoverArt.SetWidth(270)
    <div id="Sample">
    $SampleTrack
    </div>
    </div>
       
    <div id="ProductPageRightCol">   
    $Content
    $TrackListing
    </div>
    </div>

    Cheers

    Marc

    4346 Views
Page: 1 2
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.