XF 2.0 Question regarding phraseDeferred

DragonByte Tech

Well-known member
Hey all,

Based on the name, it was my understanding that using \XF::phraseDeferred() it would not attempt to render or preload this phrase during runtime, and would add this phrase's key to the list of phrases to load when the View was finally rendered. However, that is not what I am seeing.

If it matters, this is using the PHP Callback method of generating an option, and the deferred phrases are being added to code which is passed to return self::getTemplate('admin:template_name_here', $option, $htmlParams, $viewParams) using the $viewParams.

I'm posting this here as I'm not sure if it's a bug, in either case I'll post the stack trace of the queries from the debug mode:

Code:
SELECT title, phrase_text
FROM xf_phrase_compiled
WHERE language_id = ?
    AND title IN ('phraseKeyHere')
Params: 1
Run Time: 0.000631
Select Type    Table    Type    Possible Keys    Key    Key Len    Ref    Rows    Extra
SIMPLE    xf_phrase_compiled    const    PRIMARY    PRIMARY    106    const,const    1     

XF\Db\Mysqli\Statement->execute() in src/XF/Db/AbstractAdapter.php at line 69
XF\Db\AbstractAdapter->query() in src/XF/Db/AbstractAdapter.php at line 106
XF\Db\AbstractAdapter->fetchPairs() in src/XF/Language.php at line 351
XF\Language->loadPhrases() in src/XF/Language.php at line 249
XF\Language->getPhraseText() in src/XF/Language.php at line 167
XF\Language->renderPhrase() in src/XF/Phrase.php at line 52
XF\Phrase->render() in src/XF/Phrase.php at line 59
XF\Phrase->__toString() in internal_data/code_cache/templates/l1/s0/admin/templateNameHere.php at line 11
XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 1170
XF\Template\Templater->renderTemplate() in src/XF/Option/AbstractOption.php at line 92
XF\Option\AbstractOption::getTemplate() in /path/to/File.php at line 120
Product\Name\Option\Status::renderStatus()
call_user_func() in src/XF/Entity/Option.php at line 75
XF\Entity\Option->renderDisplayCallback()
call_user_func_array() in src/XF/Template/Templater.php at line 878
XF\Template\Templater->method() in internal_data/code_cache/templates/l1/s0/admin/option_macros.php at line 194
XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 601
XF\Template\Templater->callMacro() in internal_data/code_cache/templates/l1/s0/admin/anotherTemplateName.php at line 61
XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 601
XF\Template\Templater->callMacro() in internal_data/code_cache/templates/l1/s0/admin/option_list.php at line 64
XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 1170
XF\Template\Templater->renderTemplate() in src/XF/Template/Template.php at line 24
XF\Template\Template->render() in src/XF/Mvc/Renderer/Html.php at line 48
XF\Mvc\Renderer\Html->renderView() in src/XF/Mvc/Dispatcher.php at line 332
XF\Mvc\Dispatcher->renderView() in src/XF/Mvc/Dispatcher.php at line 303
XF\Mvc\Dispatcher->render() in src/XF/Mvc/Dispatcher.php at line 44
XF\Mvc\Dispatcher->run() in src/XF/App.php at line 1845
XF\App->run() in src/XF.php at line 328
XF::runApp() in admin.php at line 13

If I am doing something wrong or not using phraseDeferred correctly then please do let me know how I can correct the code to no longer query individual phrases :)

Thanks!


Fillip
 
Based on the name, it was my understanding that using \XF::phraseDeferred() it would not attempt to render or preload this phrase during runtime
That is roughly what it's doing, but your logic is backwards.

XF::phrase will add the phrase to the pre-load list, meaning the first time any phrase is rendered, it will proactively fetch the value for any phrase that has been "seen" before. (Create 10 phrase objects; the first render will fetch the values of all ten in a single query.)

XF::phraseDeferred doesn't pre-load the phrase, essentially. The value won't be requested until that specific phrase is rendered.

The only time to use phraseDeferred is in situations where, more often than not, you don't need the phrase value. Common scenarios for this would be where the phrase is returning a detailed error message (such as one targeted at an admin) and normal use wouldn't use this value.
 
I have no idea why I assumed that "deferred" meant it would fetch all the phrases in one go once the view was rendered, I made the change in the code and it works exactly as you say.

Thank you for the help, as always :)


Fillip
 
Back
Top Bottom