Jump to content

How to load the authentication.tpl file from a module? (Override)


Recommended Posts

Hi everyone:

 

Greetings from Colombia (co)

 

I've been looking for heaven, earth and sea a solution to the next issue, but first I'll tell you a little introduction to what I'm doing:

 

I'm developing a module to the Prestashop v1.6.1.17, that allows upload products, clients, sales correctly processed and paid, to a Electronic Billing platform. This platform, like my country, require 3 important fields that registration form from Prestashop, doesn't allow:

 

  • Know if the registered client is a person or is a company
  • Get the Identification type (like license driver for a person, or Company ID for a company, obviously)
  • Get the Identification number.

So... I feel that someone will reply me with "Easy, in the Address form, you have a field to put and catch the identification number ;)", and it's true... you're right, but that field (Identification Number) isn't unique, and in my country... that number is unique.

 

I've been able to overtake absolutely everything (even altering the Customer table to add these 3 columns, without needing to broke anything at all), I have managed to make an Override of the Customer class and the AuthController FrontController to add more variables when modifying the form, but my problem is trying to modify this form:

post-784202-0-04494900-1505743499_thumb.jpeg

 

Again I feel that someone will reply me with "Easy, go to the themes/default-bootstrap/authentication.tpl file and edit all you need", and again I'll tell you "you're right", that's the file I need to modify. But my objective is load this form from my module, not from the theme folder. I use override all AuthController to avoid some errors on FrontOffice, but in the override, I modified in the initContent function, this lines:

 

Original AuthController code:

// Just set $this->template value here in case it's used by Ajax$this->setTemplate(_PS_THEME_DIR_.'authentication.tpl');

Override AuthController code:

// Just set $this->template value here in case it's used by Ajax$module_dir_override_tpl = _PS_MODULE_DIR_ . 'my-module/views/templates/front/override/';$this->context->smarty->assign('mts_dir', $module_dir_override_tpl);$this->setTemplate($module_dir_override_tpl . 'authentication.tpl');

The difference is asign to a variable the Prestashop Module Directory path (changing "_PS_THEME_DIR_" for "_PS_MODULE_DIR_" constants), adding the complete file path.

 

Everything it's ok so far, because when I go to the Sign up / Sign in section, all load correctly. The page loads "initially" this section:

post-784202-0-97437900-1505743504_thumb.jpeg

Confirmed: loads from modules/my-module/views/templates/front/override/authentication.tpl

 

If I edit anything in this section, the template load correctly... works fine, but the problem starts when I click on the Create an Account button:

post-784202-0-04494900-1505743499_thumb.jpeg

Confirmed: loads from themes/default-bootstrap/authentication.tpl

 

The page loaded is like the original themes/default-bootstrap/authentication.tpl, ignoring all the changes that I made before.

 

Hours try to know what happen, I rename the file from this:

themes/default-bootstrap/authentication.tpl

To this:

themes/default-bootstrap/authentication2.tpl

When I reload the page and click in the Create an Account button, i got this:

post-784202-0-26102700-1505743506_thumb.jpeg

 

I started searching with a code editor if there was any additional  {include} (aparte from the errors.tpl file), but there are only Hooks, conditionals, and the form I need to load.
 
Maybe I need to check the Hooks, if it is really necessary to implement one (since in itself the operation of the code is between the BackOffice and the FrontOffice, without any hook, except the DisplayHeader hook... I thinks I don't need any other, it is only to load css and js ... I think)
 
Will there be any JavaScript that is loading the original form from the theme path and not from the module path?
I dare to blame a JS because the error that comes out is "after" that the form is loaded ... if there was an error, it would not load anything on the page ... a blank page nothing else (or in the worst case, a 500 error).
 
And the other problem I have (to a lesser degree) is, why when I uninstall the module, does not remove all the corresponding files from the Override folder? Deleting the class_index.php from the cache folder will be manual and mandatory, but only the override of Customer is the one that stays there.
 
Thanks

post-784202-0-04494900-1505743499_thumb.jpeg

post-784202-0-97437900-1505743504_thumb.jpeg

post-784202-0-26102700-1505743506_thumb.jpeg

Link to comment
Share on other sites

Answering myself:

 

Will there be any JavaScript that is loading the original form from the theme path and not from the module path?
I dare to blame a JS because the error that comes out is "after" that the form is loaded ... if there was an error, it would not load anything on the page ... a blank page nothing else (or in the worst case, a 500 error).

 

Yep the file is themes/default_bootstrap/js/authentication.js

Sending by Ajax, the email address to validate if already registered.

 

The initContent(), processSubmitLogin() processSubmitAccount() functions is sending information, with this: $this->AjaxDie(Tools::jsonEncode($return)); only for errors

Keep commentings if I found some news

Link to comment
Share on other sites

I think this is a Good News:

 

I find this core near the line 165 on the AuthController:

$return = array(
    'hasError' => !empty($this->errors),
    'errors' => $this->errors,
    'page' => $this->context->smarty->fetch($this->template),
    'token' => Tools::getToken(false)
);
$this->ajaxDie(Tools::jsonEncode($return));

So, $this->template must contain the template setted before (but no, it's a lie, I don't know why) and contain the original authentication.tpl.

 

Now, in the authentication.js located in the js folder on the template path, I find this script:

function submitFunction() {
    $('#create_account_error').html('').hide();
    $.ajax({
        type: 'POST',
        url: baseUri + '?rand=' + new Date().getTime(),
        async: true,
        cache: false,
        dataType: "json",
        headers: {"cache-control": "no-cache"},
        data:
            {
                controller: 'authentication',
                SubmitCreate: 1,
                ajax: true,
                email_create: $('#email_create').val(),
                back: $('input[name=back]').val(),
                token: token
            },
        success: function (jsonData) {
            if (jsonData.hasError) {
                var errors = '';
                for (error in jsonData.errors)
                    //IE6 bug fix
                    if (error != 'indexOf')
                        errors += '<li>' + jsonData.errors[error] + '</li>';
                $('#create_account_error').html('<ol>' + errors + '</ol>').show();
            }
            else {
                // adding a div to display a transition
                $('#center_column').html('<div id="noSlide">' + $('#center_column').html() + '</div>');
                $('#noSlide').fadeOut('slow', function () {
                    $('#noSlide').html(jsonData.page);
                    $(this).fadeIn('slow', function () {
                        if (typeof bindUniform !== 'undefined')
                            bindUniform();
                        if (typeof bindStateInputAndUpdate !== 'undefined')
                            bindStateInputAndUpdate();
                        document.location = '#account-creation';
                    });
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            error = "TECHNICAL ERROR: unable to load form.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus;
            if (!!$.prototype.fancybox) {
                $.fancybox.open([
                        {
                            type: 'inline',
                            autoScale: true,
                            minHeight: 30,
                            content: "<p class='fancybox-error'>" + error + '</p>'
                        }],
                    {
                        padding: 0
                    });
            }
            else
                alert(error);
        }
    });
}

When the "authentication" controller (according to what I have seen, it's the same AuthController, but I don't know why have a different name) return or respond with a success, this script get the authentication.tpl HTML code for this line in the first code shown:

'page' => $this->context->smarty->fetch($this->template),

And stored in the jsonData.page, to be sent via .html() JQuery function.

 

Printing with console.log(jsonData), I got this:

{hasError: false, errors: Array(0), page: "<h1 class="page-heading">Crear una cuenta</h1>↵↵↵	…sup>Campo requerido</span></p>↵		</div>↵	</form>↵", token: "870c36a04fc1d9b250707cdd6c7ca583"}

in the JSON String, you can see the key page.

 

But I keep wondering "if I set my own template, why the AuthController still using the default authentication.tpl file?

 

Thanks people... you're awesome...

I wll try to replace in this script:

$return = array(
    'hasError' => !empty($this->errors),
    'errors' => $this->errors,
    'page' => $this->context->smarty->fetch($this->template),
    'token' => Tools::getToken(false)
);
$this->ajaxDie(Tools::jsonEncode($return));

$this->template for the path of my own template.

 

Stay connected on this channel. More news on this issue soon.

Link to comment
Share on other sites

Good news everyone!

 

I wll try to replace in this script:

$return = array(
    'hasError' => !empty($this->errors),
    'errors' => $this->errors,
    'page' => $this->context->smarty->fetch($this->template),
    'token' => Tools::getToken(false)
);
$this->ajaxDie(Tools::jsonEncode($return));

$this->template for the path of my own template.

 

Stay connected on this channel. More news on this issue soon.

 

Confirmed: This code line add all the HTML content into the authentication.tpl for the register form.

YOU!... little piece of code...

Knowing this, now if I need to do more tests, if I modify the authentication.tpl file, change to add the fields I need.
 
On the other hand, the question that was of minor importance, happens to have more relevance:
Why does the AuthController override not work correctly? He's just overriding what he wants and not what I need.
 
Stay connected on this channel. More news on this issue soon.
Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

HI GKentaurus,

You've been amazing with your discoveries and explanations to yourself since nobody helped you.

I also i'm trying to figure how can i add a link in another module that takes my to the controller authentication, only with the register function.

Do you think you can help me?

 

Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...