Jump to:

794 Posts in 408 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Widget image upload problem

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 2422 Views
  • webcook
    Avatar
    Community Member
    20 Posts

    Widget image upload problem Link to this post

    Hi experts,

    This is my code to upload the image for my widget,
    My problem was i cant able to see the browser button to upload the image.
    In my widget must have three fields
    1) title
    2)description
    3)image name.

    How to add those (title,description&browser) fields in my widgets..
    Please help me to solve this problem

    class FeatureProductWidget extends Widget {
       // ... do your other normal widget stuff ...
       static $db = array(
          'Name' => 'Varchar(255)',
          'Description' => 'Text',
          'IPath' => 'Varchar(255)'
       );

       static $title = 'Name';
       static $cmsTitle = 'Description';
       static $description = 'Adds HTML content to the widget sidebar. <br />(Save and refresh the page if you cannot see the text editor field.)';
       
       function Title() {
          return $this->WidgetTitle ? $this->WidgetTitle : self::$title;
       }
       function getCMSFields() {
          return new FieldSet(
             new TextField("Name","Number of galleries to show (0 for All)"),
             new TextareaField("Description","Number of galleries to show (0 for All)"),
             new HiddenField("IPath","Number of galleries to show (0 for All)")
          );
       }
       function Form() {
          $controller = new FeatureProductWidget_Controller($this);
          return $controller->Form();
       }
    }

    /**
    * Controller supporting forms on widgets
    * Note: the widgets shouldn't be used on secure pages for private forms - that kind of security isn't implemented
    */
    class FeatureProductWidget_Controller extends Controller {
       protected $widget;
       
       function __construct($widget = null) {
          if($widget) $this->widget = $widget;
          
          
       }
       function widget() {
          if($this->widget) return $this->widget;
          else if(is_numeric($this->urlParams['ID'])) return $this->widget = DataObject::get_by_id('Widget', $this->urlParams['ID']);
          else user_error('No widget selected', E_USER_ERROR);
       }
       function Link() {
          return $this->class;
       }
       
       function Form() {
          // ... This can be whatever form you like ...
          $widgetform = new WidgetForm($this, 'Form', new FieldSet(
           new SimpleImageField (
           $name = "FileTypeID",
           $title = "Upload your FileType"
           )
          ), new FieldSet(
             new FormAction('doAction', 'Submit')
          ));
          $widgetform->setWidget($this->widget);
          return $widgetform;
       }
       function doAction($data, $form) {
          // ... Do your thing, just like a normal SilverStripe form

          // ... This is a good way of giving feedback to the user about the submission. A message will be shown above the form.

          $this->Form()->sessionMessage("Thanks for submitting my form", "good");
          Director::redirectBack();
       }
    }

    class WidgetForm extends Form {
       protected $widget;
       
       static $url_handlers = array(
          'POST $WidgetID' => 'httpSubmission',
          'GET $WidgetID' => 'httpSubmission',
       );
       
       function FormAction(){
          if($this->widget){
             return parent::FormAction()."/".$this->widget->ID;
          }
          return parent::FormAction();
       }
       
       function setWidget($wgt){
          if($wgt != null){
             $this->widget = $wgt;
          }
       }
       
       function getWidget(){
          return $this->widget;
       }
       
    }

    Attached Files
  • FungshuiElephant
    Avatar
    Community Member
    57 Posts

    Re: Widget image upload problem Link to this post

    Not qute sure what you're trying to do but:

    You can add a simple FileField to your widgetEditor like this:

       function getCMSFields() {

          return new FieldSet(

             new FileField('ImageName', "The file to upload")
          );

       }

    If you need more flexibility you can also override Widget.php's CMSEditor function in your FeatureProductWidget.php class...

    For example this just adds a simple button to the WidgetEditor in the CMS:

       function CMSEditor() {
          $output = parent::CMSEditor();
          $output.='<input type=\'button\' value=\'Upload\'>';

          return $output;
       }

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