Welcome, Guest
Username: Password: Remember me

TOPIC: [FIXED] function headerDeclarations loaded too many times

function headerDeclarations loaded too many times 12 Aug 2013 09:51 #10749

  • Tomaselli
  • Tomaselli's Avatar
  • Offline
  • Elite Member
  • Posts: 293
  • Thank you received: 87
  • Karma: 46
Sometimes there is the need to load the component header in several places, for example using a plugin or a module (or many modules), it's done callng the function:

headerDeclarations

you'll find it in the helper.php file.
Everything works, BUT if you take a look on the source code of the generated page, you'll find out that all the script declarations (for example those for the jquery validationengine) are loaded each time the headerDeclaration is called, so for example if you have 1 plugin and 4 modules, it's loaded 5 times (+1 if you are in a view of your component).

my workaround is to add a static variable $loaded to the function in this way:
	public static function headerDeclarations()
	{
		static $loaded = false;
		
		if(!$loaded){
			$app = JFactory::getApplication();
			$doc = JFactory::getDocument();

			$siteUrl = JURI::root(true);

			$baseSite = 'components' .DS. COM_ESTIMATOR;
			$baseAdmin = 'administrator' .DS. 'components' .DS. COM_ESTIMATOR;

			$componentUrl = $siteUrl . '/' . str_replace(DS, '/', $baseSite);
			$componentUrlAdmin = $siteUrl . '/' . str_replace(DS, '/', $baseAdmin);

			//Javascript
			//jQuery Loading : Abstraction to handle cross versions of Joomla
			JDom::_('framework.jquery');
			JDom::_('framework.jquery.chosen');
			JDom::_('framework.bootstrap');

			$doc->addScript($siteUrl . '/media/system/js/core.js');

			//Load the jQuery-Validation-Engine (MIT License, Copyright(c) 2011 Cedric Dugas http://www.position-absolute.com)
			self::addScript($doc, $baseAdmin, 'js' .DS. 'jquery.validationEngine.js');
			self::addStyleSheet($doc, $baseAdmin, 'css' .DS. 'validationEngine.jquery.css');
			EstimatorHelperHtmlValidator::loadLanguageScript();

			//CSS
			if ($app->isAdmin())
			{
				// Blue stork override
				$styles = "fieldset td.key label{display: block;}fieldset input, fieldset textarea, fieldset select, fieldset img, fieldset button{float: none;}fieldset label, fieldset span.faux-label{float: none;display: inline;min-width: inherit;}";
				$doc->addStyleDeclaration($styles);

				self::addStyleSheet($doc, $baseAdmin, 'css' .DS. 'estimator.css');
				self::addStyleSheet($doc, $baseAdmin, 'css' .DS. 'toolbar.css');
				$doc->addStyleSheet($componentUrlAdmin . '/css/jquery-ui-1.9.1.min.css');
			}
			else if ($app->isSite())
			{
				self::addStyleSheet($doc, $baseSite, 'css' .DS. 'estimator.css');
				self::addStyleSheet($doc, $baseSite, 'css' .DS. 'toolbar.css');
				$doc->addStyleSheet($componentUrl . '/css/jquery-ui-1.9.1.min.css');
			}
			
			$loaded = true;
		}
	}

this will solve the problem, but if you have custom js/css desclarations for a plugin or module, it's better if you move them out of the function headerDeclarations()
The administrator has disabled public write access.
The following user(s) said Thank You: admin

function headerDeclarations loaded too many times 12 Aug 2013 12:34 #10755

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Yes, good suggestion.

But this is strange because Joomla should handle this natively.

I think the only problem could come from :
XxxxHelperHtmlValidator::loadLanguageScript();

But, as you suggest, it is even better to avoid the loading when we know that anyway it has already been loaded.
I keep your suggestion.
K++
Coding is now a piece of cake
The administrator has disabled public write access.

function headerDeclarations loaded too many times 12 Aug 2013 12:40 #10756

  • Tomaselli
  • Tomaselli's Avatar
  • Offline
  • Elite Member
  • Posts: 293
  • Thank you received: 87
  • Karma: 46
I don't know why joomla doesn't manage that and as you know the joomla code is "untouchable" so I opted for another solution.
The administrator has disabled public write access.

function headerDeclarations loaded too many times 12 Aug 2013 13:41 #10761

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Fixed.
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: Tomaselli
Time to create page: 0.115 seconds

Get Started