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.
JFactory::getDocument()->addScriptDeclaration('
alert("Hello world!");
');
Get an alert
Getting two alerts
Joomla 3.8.10
PHP 7.1.18
Labels |
Added:
?
|
Category | ⇒ | com_fields JavaScript |
Status | New | ⇒ | Information Required |
The display of fields (their ... ->value property) is always created twice
aka the layout will be loaded twice
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
You can create a js file include it through jhtml::script();. There problem solved
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
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]
@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.
You will be then CSP compliant. Another benefit, even for two lines.
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).
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
Status | Information Required | ⇒ | Discussion |
Labels |
Added:
J3 Issue
|
Status | Discussion | ⇒ | Expected Behaviour |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2020-03-18 02:53:14 |
Closed_By | ⇒ | jwaisner |
Status | Expected Behaviour | ⇒ | Closed |
Closed_By | jwaisner | ⇒ | joomla-cms-bot |
Set to "closed" on behalf of @jwaisner by The JTracker Application at issues.joomla.org/joomla-cms/21004
Closing as expected behavior.
@laoneo can you please have a Look?