phptask-parallel-libraryprestashopsmarty

Smarty error after hardcoding ps_socialfollow.tpl to support multilanguage


My prestashop version is 1.7.3.4 on php 7.1.

This is the default content of ps_socialfollow.tpl:

 {block name='block_social'}
  <div class="social_follow">
    <ul>
      {foreach from=$social_links item='social_link'}
        <li class="{$social_link.class}"><a href="{$social_link.url}" target="_blank">{$social_link.label}</a></li>
      {/foreach}
    </ul>
  </div>
{/block}

The module by default doesn't support multi language. So I've hard coded it to support multi language like this:

{block name='block_social'}
{if $language.language_code == 'en-us'}
 <div class="social_follow">
    <ul>
        <li class="facebook"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.label}</a></li>
    </ul>
  </div>

{elseif $language.language_code == 'ko'}
  <div class="social_follow">
    <ul>
        <li class="facebook"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.label}</a></li>
    </ul>
  </div>

{/if}

{/block}

This works flawlessly as long as I don't activate the debug mode. Once I activate the debug mode, I get this error message:

>   ContextErrorException in smarty_internal_templatebase.php(157) :
> eval()'d code line 68:
>     Notice: Undefined index: social_link
>     in smarty_internal_templatebase.php(157) : eval()'d code line 68
>     at ErrorHandler->handleError('8', 'Undefined index: social_link', '/var/www/html/store/vendor/prestashop/smarty/sysplugins/smarty_internal_templatebase.php(157)
> : eval()'d code', '68', array('_smarty_tpl' =>
> object(SmartyDevTemplate))) in smarty_internal_templatebase.php(157) :
> eval()'d code line 68
>     at content_5b4fc96564eec0_18848187(object(SmartyDevTemplate)) in smarty_internal_templatebase.php line 188
>     at Smarty_Internal_TemplateBase->fetch(object(SmartyDevTemplate), null, null, null, false, true, false) in SmartyDevTemplate.php line 41
>     at SmartyDevTemplateCore->fetch() in Module.php line 2282
>     at ModuleCore->fetch('module:ps_socialfollow/ps_socialfollow.tpl', 'ps_socialfollow|1|3|3|3|1|1') in ps_socialfollow.php line 201
>     at Ps_Socialfollow->renderWidget('displayFooterAfter', array('smarty' => object(SmartyDevTemplate), 'cookie' =>
> object(Cookie), 'cart' => object(Cart), 'altern' => '1')) in Hook.php
> line 929
>     at HookCore::coreRenderWidget(object(Ps_Socialfollow), 'displayFooterAfter', array('smarty' => object(SmartyDevTemplate),
> 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => '1'))
> in Hook.php line 881
>     at HookCore::exec('displayFooterAfter', array('smarty' => object(SmartyDevTemplate), 'cookie' => object(Cookie), 'cart' =>
> object(Cart), 'altern' => '1'), null) in smarty.config.inc.php line
> 168
>     at smartyHook(array('h' => 'displayFooterAfter'), object(SmartyDevTemplate)) in SmartyLazyRegister.php line 83
>     at SmartyLazyRegister->__call('smartyHook', array(array('h' => 'displayFooterAfter'), object(SmartyDevTemplate))) in
> smarty_internal_templatebase.php(157) : eval()'d code line 691
>     at content_5b4fc96527b774_70366195(object(SmartyDevTemplate)) in smarty_internal_templatebase.php(157) : eval()'d code line 248
>     at content_5b4fc965282d34_37575711(object(SmartyDevTemplate)) in smarty_internal_templatebase.php line 188
>     at Smarty_Internal_TemplateBase->fetch('index.tpl', null, 'layouts/layout-full-width.tpl', object(SmartyDev), false, true,
> false) in SmartyDev.php line 41
>     at SmartyDev->fetch('index.tpl', null, 'layouts/layout-full-width.tpl') in FrontController.php line 684
>     at FrontControllerCore->smartyOutputContent('index.tpl') in FrontController.php line 668
>     at FrontControllerCore->display() in Controller.php line 273
>     at ControllerCore->run() in Dispatcher.php line 428
>     at DispatcherCore->dispatch() in index.php line 28

My knowledge of php is not even worth mentioning, since it's more based on the little bit of logical thinking I'm capable of doing, rather than actual knowledge. Thus, I'm unable to fix the smarty error on my own.

What'd be the modification I had to do? What'd be the proper way of implementing multi language (website) support into this module?

I've edited the ps_socialfollow.tpl according to Alexander Grosul's suggestion.

  <div class="social_follow">
    <ul>
        <li class="facebook"><a href="" target="_blank">{$social_link.facebook.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.twitter.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.instagram.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.googleplus.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.pinterest.label}</a></li>
    </ul>
  </div>

The result is the exact same smarty error.

I've also tried this version:

{block name='block_social'}
  <div class="social_follow">
    <ul>
      {foreach from=$social_links item='social_link'}

{if $language.language_code == 'en-us'}
         <li class="facebook"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.label}</a></li>



{elseif $language.language_code == 'ko'}
        <li class="facebook"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.label}</a></li>

{/if}

{/foreach}
 </ul>
  </div>
{/block}

Which didn't trigger a smarty error but just displayes all of the icons. at once, regardles sof the selected language.


Solution

  • In your example, you have the loop in the default version but don't have it in your modified one. That's mean that you cannot use $social_link because it does not exist there, it is created within the loop. You can use only incoming $social_links and point out what element of this array you want to use, something like $social_links.twitter.label etc.