Jump to content

Template is not receiving variables when a module is overrided


pabloMarch

Recommended Posts

Hello, I'm overriding one module but smarty is not receiving the variables sent from renderWidget of the parent class, this code shows an example on one the Ps_Languageselector module:

path: override\modules\ps_languageselector\ps_languageselector.php

<?php

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

class Ps_LanguageselectorOverride extends Ps_Languageselector implements WidgetInterface
{

  // is this necessary?
  public function install()
  {
      // testing a module override on 'custom?' hook:
      return (parent::install() && $this->registerHook('displayFooterAfter'));
  }

  public function hookDisplayFooterAfter($params)
  {
    // return parent::renderWidget();
    return $this->display(__FILE__, 'ps_languageselector.tpl');
  }

}

And this is my module.tpl:

path: themes\mytheme\modules\ps_languageselector\ps_languageselector.tpl

 {* variable not passed is $languages *}
 <div id="mod-ps-languageselector">
  <h4>{l s='Language:' d='Shop.Theme.Global'}</h4>
  {if isset($languages)}
    {foreach from=$languages item=language}
      {assign isActive $language.id_lang == $current_language.id_lang}
      <a href="{url entity='language' id=$language.id_lang}">
        <img src="/img/l/{$language.id_lang}.jpg" class="{if isActive}active{/if}" alt="{$language.iso_code}" />
      </a>
    {/foreach}
  {/if}
</div>

help will be appreciate, I'll be posting any solution found. If helps this is the parent class:

https://github.com/PrestaShop/ps_languageselector/blob/1fa02ff042034e1322b3eef2e923eaea8c58f3d6/ps_languageselector.php

 

Edited by pabloMarch (see edit history)
Link to comment
Share on other sites

  • pabloMarch changed the title to Smarty is not receiving variables when a module is overrided

I found a solution for this, exist one method of the 'WidgetInterface' called 'getWidgetVariables', you must send the variables throw this method in the example module I'm using, like this:

<?php

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

if (!defined('_PS_VERSION_'))
	exit;

class Ps_LanguageselectorOverride extends Ps_Languageselector implements WidgetInterface
{

  // is this necessary?
  public function install()
  {
      // testing a module override on 'custom?' hook:
      if (!parent::install() ||!$this->registerHook('displayFooterAfter'))
        return false;
      return true;
  }

  public function hookDisplayFooterAfter($params)
  {
    if (!$this->active)
			return;

    // sent this array to smarty: array('languages' => $languages, 'current_language' => $current)
    $this->smarty->assign(parent::getWidgetVariables('displayFooterAfter'));
    return $this->display(__FILE__, 'ps_languageselector.tpl');
  }

}

Recommend you to read this post about how to:

http://build.prestashop.com/howtos/module/how-to-override-modules/

 

Link to comment
Share on other sites

  • pabloMarch changed the title to Template is not receiving variables when a module is overrided

Use the following function:

public function hookDisplayFooterAfter($params)
{
    $languages = Language::getLanguages(true, $this->context->shop->id);
    if (1 < count($languages)) {
            $this->smarty->assign($this->getWidgetVariables());
            $this->display(__FILE__, 'ps_languageselector.tpl');
    }
    return false;
}
Edited by tantan199 (see edit history)
Link to comment
Share on other sites

1 hour ago, tantan199 said:

Use the following function:


public function hookDisplayFooterAfter($params)
{
    $languages = Language::getLanguages(true, $this->context->shop->id);
    if (1 < count($languages)) {
            $this->smarty->assign($this->getWidgetVariables());
            $this->display(__FILE__, 'ps_languageselector.tpl');
    }
    return false;
}

appreciate it, 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...