Jump to:

5537 Posts in 1737 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Extending MemberLogin

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

Page: 1
Go to End
Author Topic: 311 Views
  • adambuczek
    Avatar
    Community Member
    5 Posts

    Extending MemberLogin Link to this post

    Hi, I'm trying to customize login form to tailor HTML to my design.

    I added

    Object::useCustomClass('MemberLoginForm', 'CustomLoginForm');

    to mysite/_config.php.
    Created CustomLoginForm.php in mysite/code:

    class CustomLoginForm extends MemberLoginForm
    {

       public function __construct($controller, $name)
       {

             if(!$fields) {
                $label=singleton('Member')->fieldLabel(Member::config()->unique_identifier_field);
                $fields = new FieldList(
                   new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this),
                   // Regardless of what the unique identifer field is (usually 'Email'), it will be held in the
                   // 'Email' value, below:
                   new TextField("Email", $label, Session::get('SessionForms.MemberLoginForm.Email'), null, $this),
                   new PasswordField("Password", _t('Member.PASSWORD', 'Password'))
                );
                if(Security::config()->autologin_enabled) {
                   $fields->push(new CheckboxField(
                      "Remember",
                      _t('Member.REMEMBERME', "Remember me next time?")
                   ));
                }
             }

       parent::__construct($controller, $name);

       }

    }

    And copied some stuff from parent class just to make sure it works.

    I get

    [Warning] Missing argument 1 for CustomLoginForm::__construct()

    and I don't know what is going on...

  • adambuczek
    Avatar
    Community Member
    5 Posts

    Re: Extending MemberLogin Link to this post

    I got some help on IRC channel. Thanks Zauberfish!

    I don't know what caused the warning but here is the code I use to render a field using custom ss file.

    class CustomLoginForm extends MemberLoginForm {

       public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true)
       {
          parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser);
          foreach($this->Fields() as $field) {
             if (!$field->is_a('HiddenField')) {
                // skip hidden fields

                $field->setFieldHolderTemplate('CustomFrontEndField_holder');
             }
          }
       }
    }

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: Extending MemberLogin Link to this post

    To help avoid others an hours worth of head scratching: make sure to set strong to true on useCustomClass in /_config.php.

    Example code adds Register link to SilverStripe login form. Using first MemberProfilePage module's page in the system (replace as required).

    mysite/_config.php

    Object::useCustomClass('MemberLoginForm', 'LoginOrRegisterForm', true);

    mysite/code/LoginOrRegisterForm.php

    <?php

    class LoginOrRegisterForm extends MemberLoginForm {
       public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) {
          Debug::Show("LoginOrRegister");
          parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser);
          $this->Actions()->FieldByName('action_dologin')->addExtraClass('button small');
          $this->Actions()->insertAfter(new LiteralField('Add', '<a href="' . MemberProfilePage::get()->First()->Link() . '" class="right button small">Register &gt;</a>'), "action_dologin");
       }
    }

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