Jump to:

10979 Posts in 2722 Topics by 1815 members

All other Modules

SilverStripe Forums » All other Modules » Extend user defined form with 'Keep with next' configuration

Discuss all other Modules here.

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

Page: 1 2
Go to End
Author Topic: 2424 Views
  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Extend user defined form with 'Keep with next' configuration Link to this post

    I would like to add a checkbox field labeled 'Keep with next' to Field Configuration of user defined form fields. When checked it should add css class to the fields' markup that allows for CSS styling to display fields side by side e.g. First name + Last name.

    I thought I could do this by extending the EditableFormField class:
    CustomEditableFormField.php

    <?php
    /**
    * Represents the base class of a editable form field
    * object like {@link EditableTextField}.
    *
    * @package userforms
    */

    class CustomEditableFormField extends Extension {
    function extraStatics() {
          return array(
             'db' => array(
                "KeepWithNext" => "Boolean"
             )
          );
       }

       /**
        * Implement custom field Configuration on this field. Includes such things as
        * settings and options of a given editable form field
        *
        * @return FieldSet
        */
       public function getFieldConfiguration() {
          //Debug::Show($fields);
          $fields = parent::getFieldConfiguration();

          $extraFields = new FieldSet(
             new CheckboxField($this->owner->getFieldName('KeepWithNext'), _t('CustomEditableFormField.KEEPWITHNEXT', 'Keep with next?'), $this->owner->KeepWithNext)
          );

          $fields->merge($extraFields);
          return $fields;
       }
    }

    And adding DataObject::add_extension('EditableFormField', 'CustomEditableFormField'); to _config.php.

    However, this doesn't seem to work.

    Am I doing something wrong? Should I extend each FormField individually?

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Extend user defined form with 'Keep with next' configuration Link to this post

    You would also need to alter the built in EditableFormField class so that it actually looked at extensions (which I don't believe it does) as SilverStripe doesn't do this by default. Add in $this->extend('functionName', $this); to the end (or beginning) of any function you need to add hooks into (like getFieldConfiguration).

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: Extend user defined form with 'Keep with next' configuration Link to this post

    I couldn't get the $this->extend('functionName', $this); to work so I ended up hacking into the code (as the extend needed to be added to the existing code anyway).

    I've posted my code below in case someone finds this thread looking for how to add fields to user defined form.

    Index: code/editor/EditableFormField.php
    ===================================================================
    --- code/editor/EditableFormField.php   (revision 115687)
    +++ code/editor/EditableFormField.php   (working copy)
    @@ -1,7 +1,7 @@
    <?php
    /**
    - * Represents the base class of a editable form field
    - * object like {@link EditableTextField}.
    + * Represents the base class of a editable form field
    + * object like {@link EditableTextField}.
    *
    * @package userforms
    */
    @@ -16,6 +16,7 @@
          "Default" => "Varchar",
          "Sort" => "Int",
          "Required" => "Boolean",
    +      "KeepWithPrevious" => "Boolean",
          "CustomErrorMessage" => "Varchar(255)",
          "CustomRules" => "Text",
          "CustomSettings" => "Text",
    @@ -291,6 +292,7 @@
          $this->Default       = (isset($data['Default'])) ? $data['Default'] : "";
          $this->Sort       = (isset($data['Sort'])) ? $data['Sort'] : null;
          $this->Required    = !empty($data['Required']) ? 1 : 0;
    +      $this->KeepWithPrevious   = !empty($data['KeepWithPrevious']) ? 1 : 0;
          $this->Name       = $this->class.$this->ID;
          $this->CustomRules   = "";
          $this->CustomErrorMessage   = (isset($data['CustomErrorMessage'])) ? $data['CustomErrorMessage'] : "";
    @@ -341,7 +343,8 @@
                $this->getSettingFieldName('RightTitle'),
                _t('EditableFormField.RIGHTTITLE', 'Right Title'),
                $this->getSetting('RightTitle')
    -         )
    +         ),
    +         new CheckboxField($this->getFieldName('KeepWithPrevious'), _t('EditableFormField.KeepWithPrevious', 'Keep with previous?'), $this->KeepWithPrevious)
          );
       }
       
    Index: code/UserDefinedForm.php
    ===================================================================
    --- code/UserDefinedForm.php   (revision 115571)
    +++ code/UserDefinedForm.php   (working copy)
    @@ -389,7 +389,11 @@
                if($right = $editableField->getSetting('RightTitle')) {
                   $field->setRightTitle($right);
                }
    -            
    +
    +            if($editableField->KeepWithPrevious) {
    +               $field->addExtraClass('keepWithPrevious');
    +            }
    +
                // if this field is required add some
                if($editableField->Required) {
                   $field->addExtraClass('requiredField');

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Extend user defined form with 'Keep with next' configuration Link to this post

    Hi, great thread -- very helpful. Where does this code live:

    @@ -341,7 +343,8 @@
    $this->getSettingFieldName('RightTitle'),
    _t('EditableFormField.RIGHTTITLE', 'Right Title'),
    $this->getSetting('RightTitle')
    - )
    + ),
    + new CheckboxField($this->getFieldName('KeepWithPrevious'), _t('EditableFormField.KeepWithPrevious', 'Keep with previous?'), $this->KeepWithPrevious)
    );
    }

    I'm not seeing any instances of "RightTitle" anywhere in EditableFormField.php.

    Thanks,
    Garrett

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: Extend user defined form with 'Keep with next' configuration Link to this post

    Around line 332 you should see the following code, where the above changes need to be made.

    /**
        * Implement custom field Configuration on this field. Includes such things as
        * settings and options of a given editable form field
        *
        * @return FieldSet
        */
       public function getFieldConfiguration() {
          return new FieldSet(
             new TextField(
                $this->getSettingFieldName('RightTitle'),
                _t('EditableFormField.RIGHTTITLE', 'Right Title'),
                $this->getSetting('RightTitle')
             )
          );
       }

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Extend user defined form with 'Keep with next' configuration Link to this post

    Hi Marijn,
    Thanks Marijn,

    Ok I've copied all the code indicated in this thread,

    ERROR [User Error]: Uncaught Exception: Object->__call(): the method 'getfieldname' does not exist on 'EditableTextField'

    And indeed, I do not see this function anywhere in Userforms [v0.3.0]. What am I missing? Basically, I am just trying to add a field that goes along with every text field called "html_id", which will allow a user to attach a custom ID to the field for the purposes of hooking into other systems.

    Thanks again,
    Garrett

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Extend user defined form with 'Keep with next' configuration Link to this post

    Good news -- I've now got my html_id field appearing on the Form tab in the CMS!

    HOWEVER

    Now the task is to get the server to write it in the published page. I.e.: <input id="$html_id".../> It looks as though I'll need to do this using the "id()" function in FormField.php. But this class doesn't know about this field, so I am trying to add it to the constructor as follows:

    function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null, $html_id) {

    But this gives me an error:

    ERROR [Warning]: Missing argument 6 for FormField::__construct(), called in C:\Workspace\DDEX\sapphire\forms\DatalessField.php on line 27 and defined
    IN POST /admin/getitem?ID=112&ajax=1
    Line 84 in C:\Workspace\DDEX\sapphire\forms\FormField.php

    At this point this is deeper than I know Sapphire. Marijn or Willr, can you guys help me to get this custom field to show up on the front end?

    Thanks so much,
    Garrett

    Attached Files
  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: Extend user defined form with 'Keep with next' configuration Link to this post

    Have you tried to adjust that in one of the templates in userforms/templates? You will have to take a look in the templates to see which one you will need to alter exactly.

    2424 Views
Page: 1 2
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.