Jump to:

22977 Posts in 11806 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Can I upload a file in site content editor without having to use Files and Images tab?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 1387 Views
  • RichHead
    Avatar
    Community Member
    13 Posts

    Can I upload a file in site content editor without having to use Files and Images tab? Link to this post

    I'm really struggling with the security model for a multi-user CMS site, I do not want to grant editors access to Files and Images as that seems to allow them to add/delete ANY file.

    So, assuming the above is correct and I am not missing something obvious, is there a way to allow files to be uploaded that can then be linked to as a download (PDF files) from within the page content editor?

    Rich

  • JoshuaLewis
    Avatar
    Community Member
    76 Posts

    Re: Can I upload a file in site content editor without having to use Files and Images tab? Link to this post

    Modify mysite/code/Page.php with the following

    class Page extends SiteTree {
    ...
    $has_one = array ('pdf' => 'File');
    ...
    public function getCMSFields(){
    $fields = parent::getCMSFields();
    $fields->addFieldToTab(new FileIFrameField('pdf', 'Field Title');
    return $fields;
    }

    Or better yet create a subclass of Page. The tutorials at http://doc.silverstripe.org/doku.php go into detail.

  • RichHead
    Avatar
    Community Member
    13 Posts

    Re: Can I upload a file in site content editor without having to use Files and Images tab? Link to this post

    I tried this (but obviously did something wrong); as I lose the site and admin functions - just end up with a blank web site.

    Here's what I have:

    <?php
    class Page extends SiteTree {
       
       public static $db = array(
       );
       
       public static $has_one = array(
       );

    public static $has_one = array ('pdf' => 'File');

    public function getCMSFields(){
    $fields = parent::getCMSFields();
    $fields->addFieldToTab(new FileIFrameField('pdf', 'Field Title');
    return $fields;
    }

    class Page_Controller extends ContentController {
       
       public function init() {
          parent::init();

          // Note: you should use SS template require tags inside your templates
          // instead of putting Requirements calls here. However these are
          // included so that our older themes still work
          Requirements::themedCSS("layout");
          Requirements::themedCSS("typography");
          Requirements::themedCSS("form");
       }
       
       /**
        * Site search form
        */
       function SearchForm() {
          $searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
          $fields = new FieldSet(
           new TextField("Search", "", $searchText)
           );
          $actions = new FieldSet(
           new FormAction('results', 'Search')
           );

           return new SearchForm($this, "SearchForm", $fields, $actions);
       }
       
       /**
        * Process and render search results
        */
       function results($data, $form){
           $data = array(
           'Results' => $form->getResults(),
           'Query' => $form->getSearchQuery(),
           'Title' => 'Search Results'
           );

           return $this->customise($data)->renderWith(array('Page_results', 'Page'));
       }
       
    }

    ?>

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Can I upload a file in site content editor without having to use Files and Images tab? Link to this post

    This line is incorrect:

       $fields->addFieldToTab(new FileIFrameField('pdf', 'Field Title');

  • RichHead
    Avatar
    Community Member
    13 Posts

    Re: Can I upload a file in site content editor without having to use Files and Images tab? Link to this post

    Appreciate the reply Hamish, unfortunately I have very little (none!) PHP experience, I am simply trying to get out village community web site online...

    I do not know what I need to do to correct this {embarrassed}

  • JoshuaLewis
    Avatar
    Community Member
    76 Posts

    Re: Can I upload a file in site content editor without having to use Files and Images tab? Link to this post

    Sorry about that, I didn't double check what I posted.

    The location of the new field needs to be set and there should be two closing parentheses, one for FileIFameField() and another for addFieldToTab().

    This should now be correct.

    $fields->addFieldToTab('Root.Content.Main', new FileIFrameField('pdf', 'Field Title'));

  • RichHead
    Avatar
    Community Member
    13 Posts

    Re: Can I upload a file in site content editor without having to use Files and Images tab? Link to this post

    Thank you.. I'll give that a go later

  • RichHead
    Avatar
    Community Member
    13 Posts

    Re: Can I upload a file in site content editor without having to use Files and Images tab? Link to this post

    Exactly the same result (blank page, nothing in public or admin view at all):

    <?php
    class Page extends SiteTree {

    public static $db = array(
    );

    public static $has_one = array(
    );

    public static $has_one = array ('pdf' => 'File');

    public function getCMSFields(){
    $fields = parent::getCMSFields();
    $fields->addFieldToTab(new FileIFrameField('pdf', 'Field Title'));
    return $fields;
    }

    class Page_Controller extends ContentController {

    public function init() {
    parent::init();

    // Note: you should use SS template require tags inside your templates
    // instead of putting Requirements calls here. However these are
    // included so that our older themes still work
    Requirements::themedCSS("layout");
    Requirements::themedCSS("typography");
    Requirements::themedCSS("form");
    }

    /**
    * Site search form
    */
    function SearchForm() {
    $searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
    $fields = new FieldSet(
    new TextField("Search", "", $searchText)
    );
    $actions = new FieldSet(
    new FormAction('results', 'Search')
    );

    return new SearchForm($this, "SearchForm", $fields, $actions);
    }

    /**
    * Process and render search results
    */
    function results($data, $form){
    $data = array(
    'Results' => $form->getResults(),
    'Query' => $form->getSearchQuery(),
    'Title' => 'Search Results'
    );

    return $this->customise($data)->renderWith(array('Page_results', 'Page'));
    }

    }

    ?>

    1387 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.