Jump to:

10990 Posts in 2727 Topics by 1818 members

All other Modules

SilverStripe Forums » All other Modules » [Solved] Adding Session Data to a UserDefinedForm

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 304 Views
  • SnowBoarder82
    Avatar
    Community Member
    52 Posts

    [Solved] Adding Session Data to a UserDefinedForm Link to this post

    Hi Willr / everyone.

    I'm working on a project where I would like to be able to add session data to the submitted form (and email sent out) if the session data exists. For example, the contents of a shopping cart. What would be the best way to achieve this?

    The project in question is on SS 2.4 using the applicable userforms module.

    PS - the Userforms module is great, thank you for your work on this!

    Cheers,

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: [Solved] Adding Session Data to a UserDefinedForm Link to this post

    Glad you find the module useful. If you want to add session data, there would be several ways to go about doing it. The first question would be to you need this on all forms or just one particular form?

    A quick overview of things you could try

    A) Add a DataExtension to SubmittedForm which pushed the additional data to the record on onBeforeWrite()
    B) Create a new EditableFormField subclass which doesn't create a form field on the front end, but can be used to save into the database columns.

    Option A is probably easier, with the biggest challenge being you DataExtension's are added to every form so you would need to check for the page in question (say have a checkbox on the userform page for whether you want to attach the data).

  • SnowBoarder82
    Avatar
    Community Member
    52 Posts

    Re: [Solved] Adding Session Data to a UserDefinedForm Link to this post

    Thanks for your reply Willr,

    I would need this information on just one form. In the specific website case, I have a contact form (created using UserForms), and then there is also a checkout / order form (created via Jedateach's Shop module). I would like the shopping cart data (if it exists in the session) to be passed into the contact form.
    Exploring Option A - would that potentially cause problems to the checkout / order form processing, or would adding a Dataextension to SubmittedForm only affect all forms created using UserForms.....

    Thanks,

  • SnowBoarder82
    Avatar
    Community Member
    52 Posts

    Re: [Solved] Adding Session Data to a UserDefinedForm Link to this post

    Hi Willr,

    I've been exploring around the userforms code, tyring to get a feel for it. Looking into Option B also i've created a new editableformfield called "EditableCartField", looking at EditableLiteralField as an example.
    Not sure exactly how i can use this to save into the DB / form without creating the hidden field...
    Trying to create the hidden cart field has posed a problem with attempting to call the Cart contents as an array or string within the hidden field declaration.... getting a bit lost, any advice or code snippet examples would be much appreciated.

    Many thanks,

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: [Solved] Adding Session Data to a UserDefinedForm Link to this post

    Well how did you need the contents of the cart stored? Will a text value do or have you got a complex object?

    A simple instance of a custom form field for your case would look like

    <?php

    class EditableCartContentsField extends EditableFormField {

       public function getFormField() {
          return null;
       }
       
       public function getFieldValidationOptions() {
          return false;
       }

       public function getValueFromData($data) {
          return "This is a string to be saved into the database"
       }
    }

    When a form with that field is saved, the getValueFromData() method will return the data to be included in the SubmittedFormField object. It can only be a string, but if you wanted to store a more complex object you can define your own subclass of SubmittedFormField and tell your object to use that instead.

    public function getSubmittedFormField() {
    return new CartSubmittedFormField();
    }

    Then your CartSubmittedFormField can store / format completely customise data.

  • SnowBoarder82
    Avatar
    Community Member
    52 Posts

    Re: [Solved] Adding Session Data to a UserDefinedForm Link to this post

    That's Great Thanks Willr,

    A text value is fine for what i'm after.
    Using the simple instance that you had provided I was able to edit the getValueFromData() method to return the cart items in each form submission.

    For reference for anyone else, here's the specific code I used to get the cart as a string to return:

    public function getValueFromData($data) {
       
       $order = ShoppingCart::singleton()->current();
          
       if($order){
          $cartitems = "Artwork: ";
          $items = $order->Items();
              if($items->exists()){
                $i = 1;
                 foreach($items as $item){
                   $cartitems .= $i.") ". $item->Product()->Title." -> ";
                   $cartitems .= $item->SubTitle()." |||| ";
                   $i++;
                }
             }
             return $cartitems;
       }
       else return "No Artwork Added";
    }

    Many Thanks,

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