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.

Form Questions /

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

Display Form using Shortcode


Go to End


6 Posts   3158 Views

Avatar
x75

Community Member, 43 Posts

22 September 2010 at 4:11am

Hi,

I want to be able to embed a form in any page by using a short code. For example let's say the shortcode [feedback] is suppost to embedd a feedback form.

I got the shortcode working based on: http://ssbits.com/2-4-using-short-codes-to-embed-a-youtube-video/ but I can't figure out, how to display and process a form in the shortcode handler.

I tried adding a function feedbackform to my page_controller and then call it from the template that i use to render the shortcode, but it does not show up. I guess the page_controller is not the right place, but where would I put the form?

Thanks
Johannes

Avatar
x75

Community Member, 43 Posts

2 October 2010 at 2:43am

Ok, for anyone interested:

Add the function that creates the form to your page_controller.
In the shortcode handler function use Controller::curr() to get a reference to the controller and call the function.
Add it's output to the dataarray passed to $template->process.

Thanks to the silversripte irc channel for the help!

Johannes

Avatar
MarijnKampf

Community Member, 176 Posts

9 August 2011 at 11:41pm

Thanks X75. To make it even easier for others here is some sample code:

Presuming the form is generated in function ShowYourForm, in the YourForm class add:

	public static function YourFormShortCodeHandler($arguments, $content = null, $parser = null) {
		$current = Controller::curr();
		return $current->ShowYourForm()->forTemplate();
	}

In _config.php add:

ShortcodeParser::get()->register('YourForm',array('YourForm','YourFormShortCodeHandler'));

In the CMS editor include

[YourForm]

Avatar
MarijnKampf

Community Member, 176 Posts

10 August 2011 at 12:48am

Just wondering whether it's also possible to include the results of the processing of the form (e.g. function UserAction($data, $form)) from the same (or a different) short code handler? Anyone any ideas?

Avatar
Pix

Community Member, 158 Posts

24 November 2011 at 1:09pm

um......what?

OK, this is exactly what I want to do to!!!!! But I'm not quite piecing it together here :0(

Would it be possible for anyone to provide a mostly complete working example of how this all goes? A basic short code contact/feedback form perhaps? Please please pllllleeeeeease?

I didn't quite get this part:
"Add it's output to the dataarray passed to $template->process. "

I've implemented the YouTube shrotcode handler fine no problem but if I could get this one working it would be GREAT!

Thanks

Avatar
martimiz

Forum Moderator, 1391 Posts

29 November 2011 at 11:00pm

I've just recently completed something like this: a simple course-subscription form (using a custom template) that can be instantiated either from template or from a shortcode.

On submission it sets a session value and redirects, after which it again reads the session value (and clears it). Based on the session value it uses a custom template to either show the form or a success message. Basically very simple: just like you would normally go about creating a custom form, the only real extra is a very simple shotcode handler.

I documented it somewhat extensively in my logs: http://www.balbuss.com/custom-form-and-shortcode/ Have a look if you want (and let me know if there's anything missing)

Martine