Jump to:

1970 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Display Form using Shortcode

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

Page: 1
Go to End
Author Topic: 1530 Views
  • x75
    Avatar
    Community Member
    42 Posts

    Display Form using Shortcode Link to this post

    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

  • x75
    Avatar
    Community Member
    42 Posts

    Re: Display Form using Shortcode Link to this post

    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

  • MarijnKampf
    Avatar
    Community Member
    161 Posts

    Re: Display Form using Shortcode Link to this post

    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]

  • MarijnKampf
    Avatar
    Community Member
    161 Posts

    Re: Display Form using Shortcode Link to this post

    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?

  • Pix
    Avatar
    Community Member
    120 Posts

    Re: Display Form using Shortcode Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Display Form using Shortcode Link to this post

    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

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