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.

Customising the CMS /

Extending MemberLogin


Reply


3 Posts   455 Views

Avatar
adambuczek

Community Member, 5 Posts

17 March 2014 at 11:13am

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

Avatar
adambuczek

Community Member, 5 Posts

18 March 2014 at 5:51am

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');
         }
      }
   }
}

Avatar
MarijnKampf

Community Member, 164 Posts

22 July 2014 at 11:19pm

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");
   }
}