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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Widgets /

Discuss SilverStripe Widgets.

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

Widget image upload problem


Go to End


2 Posts   3564 Views

Avatar
webcook

Community Member, 20 Posts

7 June 2009 at 12:47am

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
Avatar
FungshuiElephant

Community Member, 57 Posts

12 June 2009 at 10:30pm

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