[feature/twig] Check the template context for language vars

We output some language vars to the context (e.g. L_TITLE in the ACP). These
do not exist in user->lang, so we must check the context vars first, if not
in context, we output the result of user->lang.

PHPBB3-11598
This commit is contained in:
Nathaniel Guse 2013-07-02 12:17:56 -05:00
parent 709b3e9803
commit f39edcea3f
2 changed files with 36 additions and 2 deletions

View file

@ -17,11 +17,15 @@ if (!defined('IN_PHPBB'))
class phpbb_template_twig_extension extends Twig_Extension class phpbb_template_twig_extension extends Twig_Extension
{ {
/** @var phpbb_template_context */
protected $context;
/** @var phpbb_user */ /** @var phpbb_user */
protected $user; protected $user;
public function __construct($user) public function __construct(phpbb_template_context $context, $user)
{ {
$this->context = $context;
$this->user = $user; $this->user = $user;
} }
@ -69,7 +73,7 @@ class phpbb_template_twig_extension extends Twig_Extension
public function getFunctions() public function getFunctions()
{ {
return array( return array(
new Twig_SimpleFunction('lang', array($this->user, 'lang')), new Twig_SimpleFunction('lang', array($this, 'lang')),
); );
} }
@ -140,4 +144,33 @@ class phpbb_template_twig_extension extends Twig_Extension
return twig_slice($env, $item, $start, $end, $preserveKeys); return twig_slice($env, $item, $start, $end, $preserveKeys);
} }
/**
* Get output for a language variable (L_FOO, LA_FOO)
*
* This function checks to see if the language var was outputted to $context
* (e.g. in the ACP, L_TITLE)
* If not, we return the result of $user->lang()
*
* @param string $lang name
* @return string
*/
function lang()
{
$args = func_get_args();
$key = $args[0];
$context = $this->context->get_data_ref();
$context_vars = $context['.'][0];
if (isset($context_vars['L_' . $key]))
{
return $context_vars['L_' . $key];
}
// LA_ is transformed into lang(\'$1\')|addslashes, so we should not
// need to check for it
return call_user_func_array(array($this->user, 'lang'), $args);
}
} }

View file

@ -134,6 +134,7 @@ class phpbb_template_twig implements phpbb_template
$this->twig->addExtension( $this->twig->addExtension(
new phpbb_template_twig_extension( new phpbb_template_twig_extension(
$this->context,
$this->user $this->user
) )
); );