J3 Issue ?
avatar tassosm
tassosm
6 Jul 2018

I am not sure if #18066 should have fixed this issue but it's still happening on 3.8.10. I am currently working on a new Field which adds a custom script declaration and for some reason the script is added twice.

Steps to reproduce the issue

  1. Add a Text custom field to an article without touching any of the settings.
  2. Add the following code to /plugins/fields/text/tmpl/text.php
JFactory::getDocument()->addScriptDeclaration('
	alert("Hello world!");
');

Expected result

Get an alert

Actual result

Getting two alerts

screenshot from 2018-07-06 12-46-58

System information (as much as possible)

Joomla 3.8.10
PHP 7.1.18

avatar tassosm tassosm - open - 6 Jul 2018
avatar joomla-cms-bot joomla-cms-bot - change - 6 Jul 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 6 Jul 2018
avatar franz-wohlkoenig franz-wohlkoenig - change - 6 Jul 2018
Category com_fields JavaScript
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 15 Jul 2018

@laoneo can you please have a Look?

avatar franz-wohlkoenig franz-wohlkoenig - change - 15 Jul 2018
Status New Information Required
avatar ggppdk
ggppdk - comment - 15 Jul 2018

The display of fields (their ... ->value property) is always created twice
aka the layout will be loaded twice

  • once for automatic display via a configurable event (this can be disabled) and
  • once for manual display (onContentPrepare event) (there is no option to disable this)

See the PR linked below for explanation and for discussion,
about option to disable manual creation , it was decided to be a B/C break or something

[com_fields] Fix fields display HTML prepared 4 or 5 times per article, make it be prepared only twice
#17895

avatar franz-wohlkoenig
franz-wohlkoenig - comment - 15 Jul 2018

@ggppdk thanks for Comment, so Issue can be closed as expected Behaviour?

avatar laoneo
laoneo - comment - 15 Jul 2018

@tassosm set a break point in your tmpl. Where do the two different calls do come from?

avatar dgrammatiko
dgrammatiko - comment - 15 Jul 2018

You can create a js file include it through jhtml::script();. There problem solved

avatar dgrammatiko
dgrammatiko - comment - 15 Jul 2018

To explain a little better my suggestion above: joomla doesn’t have any way to force uniqness on inclusion of inline scripts. The only way to assure you will only load once an asset is through jhtml script for a static js file. There are way too many pitfalls in the architecture here that needs to be patched and is far easier to just say use always js files for your scripts than try to fix wrong architecture decisions (which maybe dated to mampo era).
Anyways my 2c

avatar ggppdk
ggppdk - comment - 15 Jul 2018

@laoneo
i checked layout is loaded once, did something change ? some magic method for manual display or other change ?

avatar ggppdk
ggppdk - comment - 15 Jul 2018

Ok what i wrote above applies,
the layout is loaded twice as expected ...

once for automatic display via a configurable event (this can be disabled) and
once for manual display (onContentPrepare event) (there is no option to disable this)

just the output is captured, lol, i forgot of it
so you can not print a back-trace in it, instead you should log it to a file or ... use a debugger

Manual display (in case they are needed)

#5  PlgSystemFields->onContentPrepare() called at [libraries\joomla\event\event.php:70]
#6  JEvent->update() called at [libraries\joomla\event\dispatcher.php:160]
#7  JEventDispatcher->trigger() called at [components\com_content\views\article\view.html.php:193]

Automatic display (according to configuration, e.g. in my case set to onContentBeforeDisplay)

#6  PlgSystemFields->onContentBeforeDisplay() called at [libraries\joomla\event\event.php:70]
#7  JEvent->update() called at [libraries\joomla\event\dispatcher.php:160]
#8  JEventDispatcher->trigger() called at [components\com_content\views\article\view.html.php:199]

Back-trace in log file

#0  include() called at [administrator\components\com_fields\libraries\fieldsplugin.php:131]
#1  FieldsPlugin->onCustomFieldsPrepareField() called at [libraries\joomla\event\event.php:70]
#2  JEvent->update() called at [libraries\joomla\event\dispatcher.php:160]
#3  JEventDispatcher->trigger() called at [administrator\components\com_fields\helpers\fields.php:201]
#4  FieldsHelper::getFields() called at [plugins\system\fields\fields.php:479]
#5  PlgSystemFields->onContentPrepare() called at [libraries\joomla\event\event.php:70]
#6  JEvent->update() called at [libraries\joomla\event\dispatcher.php:160]
#7  JEventDispatcher->trigger() called at [components\com_content\views\article\view.html.php:193]
#8  ContentViewArticle->display() called at [libraries\src\MVC\Controller\BaseController.php:672]
#9  Joomla\CMS\MVC\Controller\BaseController->display() called at [components\com_content\controller.php:113]
#10 ContentController->display() called at [libraries\src\MVC\Controller\BaseController.php:710]
#11 Joomla\CMS\MVC\Controller\BaseController->execute() called at [components\com_content\content.php:43]
#12 require_once(components\com_content\content.php) called at [libraries\src\Component\ComponentHelper.php:382]
#13 Joomla\CMS\Component\ComponentHelper::executeComponent() called at [libraries\src\Component\ComponentHelper.php:357]
#14 Joomla\CMS\Component\ComponentHelper::renderComponent() called at [libraries\src\Application\SiteApplication.php:194]
#15 Joomla\CMS\Application\SiteApplication->dispatch() called at [libraries\src\Application\SiteApplication.php:233]
#16 Joomla\CMS\Application\SiteApplication->doExecute() called at [libraries\src\Application\CMSApplication.php:195]
#17 Joomla\CMS\Application\CMSApplication->execute() called at [index.php:49]


#0  include() called at [administrator\components\com_fields\libraries\fieldsplugin.php:131]
#1  FieldsPlugin->onCustomFieldsPrepareField() called at [libraries\joomla\event\event.php:70]
#2  JEvent->update() called at [libraries\joomla\event\dispatcher.php:160]
#3  JEventDispatcher->trigger() called at [administrator\components\com_fields\helpers\fields.php:201]
#4  FieldsHelper::getFields() called at [plugins\system\fields\fields.php:388]
#5  PlgSystemFields->display() called at [plugins\system\fields\fields.php:325]
#6  PlgSystemFields->onContentBeforeDisplay() called at [libraries\joomla\event\event.php:70]
#7  JEvent->update() called at [libraries\joomla\event\dispatcher.php:160]
#8  JEventDispatcher->trigger() called at [components\com_content\views\article\view.html.php:199]
#9  ContentViewArticle->display() called at [libraries\src\MVC\Controller\BaseController.php:672]
#10 Joomla\CMS\MVC\Controller\BaseController->display() called at [components\com_content\controller.php:113]
#11 ContentController->display() called at [libraries\src\MVC\Controller\BaseController.php:710]
#12 Joomla\CMS\MVC\Controller\BaseController->execute() called at [components\com_content\content.php:43]
#13 require_once(components\com_content\content.php) called at [libraries\src\Component\ComponentHelper.php:382]
#14 Joomla\CMS\Component\ComponentHelper::executeComponent() called at [libraries\src\Component\ComponentHelper.php:357]
#15 Joomla\CMS\Component\ComponentHelper::renderComponent() called at [libraries\src\Application\SiteApplication.php:194]
#16 Joomla\CMS\Application\SiteApplication->dispatch() called at [libraries\src\Application\SiteApplication.php:233]
#17 Joomla\CMS\Application\SiteApplication->doExecute() called at [libraries\src\Application\CMSApplication.php:195]
#18 Joomla\CMS\Application\CMSApplication->execute() called at [index.php:49]
avatar tassosm
tassosm - comment - 16 Jul 2018

@laoneo the 2nd call is coming from the Automatic Display option as @ggppdk described very well.

@dgrammatiko loading a script file seems to be the only solution here, indeed. However, in my case, seems like an overkill to use a script file for just two lines of code.

avatar laoneo
laoneo - comment - 16 Jul 2018

You will be then CSP compliant. Another benefit, even for two lines.

avatar ggppdk
ggppdk - comment - 16 Jul 2018

@tassosm , also a static variable should work

static $layout_scripts_added = null;

if ($layout_scripts_added === null)
{
	$layout_scripts_added = true;
	JFactory::getDocument()->addScriptDeclaration('
		alert("Hello world!");
	');
}
avatar mbabker
mbabker - comment - 16 Jul 2018

You will be then CSP compliant. Another benefit, even for two lines.

You can do CSP compliance with inline scripts (without exclusion rules), that's where nonce's come into play (do we have that capability in our CSP code yet, haven't really looked deep at it).

avatar ggppdk
ggppdk - comment - 16 Jul 2018

And a reminder here
layouts (whatever their type is ...) are reused

even if you eliminate the double loading of the layout file for this particular field instance

there are still legitimate reasons to load it multiple times
e.g. you have 3 fields of type 'myfieldtype', and you display all 3 fields
then layout file should be loaded at least 3 times

or even you may set a modified value or configuration to the field and call API to recreate it is display
e.g. display a value in format A and then also in format B

[EDIT]
probably you wanted to add the script once per field instance, in that case my comment above is not applicable

avatar franz-wohlkoenig franz-wohlkoenig - change - 17 Jul 2018
Status Information Required Discussion
avatar brianteeman brianteeman - change - 2 Aug 2018
Labels Added: J3 Issue
avatar brianteeman brianteeman - labeled - 2 Aug 2018
avatar jwaisner jwaisner - change - 18 Mar 2020
Status Discussion Expected Behaviour
Closed_Date 0000-00-00 00:00:00 2020-03-18 02:53:14
Closed_By jwaisner
avatar joomla-cms-bot joomla-cms-bot - change - 18 Mar 2020
Status Expected Behaviour Closed
Closed_By jwaisner joomla-cms-bot
avatar joomla-cms-bot joomla-cms-bot - close - 18 Mar 2020
avatar joomla-cms-bot
joomla-cms-bot - comment - 18 Mar 2020

Set to "closed" on behalf of @jwaisner by The JTracker Application at issues.joomla.org/joomla-cms/21004

avatar jwaisner
jwaisner - comment - 18 Mar 2020

Closing as expected behavior.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/21004.

Add a Comment

Login with GitHub to post a comment