Joomla!
http://forum.joomla.org/

$mainframe->addCustomHeadTag() does not work in module
http://forum.joomla.org/viewtopic.php?f=126&t=92494
Page 1 of 1

Author:  impactmedia [ Tue Sep 05, 2006 10:29 pm ]
Post subject:  $mainframe->addCustomHeadTag() does not work in module

hi there!

i call the function $mainframe->addCustomHeadTag() from a component and it works to add a css or js file.

i call it from a module (even when displayed on the same page like the component) and it does not work ? does anyone know why ?

greetings

ps: joomla 1.0.10, it is not called from a function (so don't need "global $mainframe"), mosshowhead() is applied to the template (from component it works)

Author:  friesengeist [ Tue Sep 05, 2006 10:35 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

impactmedia wrote:
i call it from a module (even when displayed on the same page like the component) and it does not work ? does anyone know why ?


The modules are loaded after the HTML header has been sent. The component is loaded before anything from the template (including headers) is sent. That's why you can't use addCustomHeadTag() in a module.

Author:  impactmedia [ Tue Sep 05, 2006 10:48 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Thank you for your quick reply ... well bad news ;)

Hm so how about a clever solution to call overlib or a custom css ?
Just "echo" ... not a nice solution ... especially when you have multiple instances of the module on one page (like me).

Any ideas ? When I have one I will post it ;)

Greetings

Author:  cgarvis [ Mon Sep 18, 2006 7:43 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

did you happen to find a solution???

Author:  koyan [ Mon Jan 22, 2007 9:35 am ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

I am looking for this as well. If someone finds a solution I would love to hear it.
My problem is not for avoiding multiple copies of the same css, but for making the sites xml compliant.

Konstantinos

Author:  jomar [ Thu Mar 08, 2007 12:31 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

I realy think that loading the modules in Array instead of executing them should do the trick.
When the components are executed, the modules should be executed at the same time, using the array.

But this requires a Core change in the Mosmainframe and I dont have a clue how its done. I´ve made a template class where
I had this problem and solved it with this sulition.

Hope it helps someone  :-\

Author:  anetus [ Mon Mar 12, 2007 9:44 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Hi.

One can add custom CSS (or JS) to module using Javascript:
Code:
<script type = "text/javascript">
      <!--
      var link = document.createElement('link');
      link.setAttribute('href', <?php echo $mosConfig_live_site; ?>.'/modules/xxxxxx.css');
      link.setAttribute('rel', 'stylesheet');
      link.setAttribute('type', 'text/css');
      var head = document.getElementsByTagName('head').item(0);
      head.appendChild(link);
      //-->
</script>


Of course it will not work if someone has Javascript disabled, so accessibility may be an issue, and if the module will be used more than once per page, then CSS file would be loaded more than once ...

Hope this helps...

Author:  mehdi [ Fri May 04, 2007 12:26 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Hi,
the topic is a bit old, but just wanted to insist that I got the same problem.
What I wanted to do is to add a javascript to header only when it was  detected to be necessary,
but because of the limitation (and possibly other bugs with addCustomHeadTag),
I'll force inclusion of that script.

Author:  bestofrisk [ Sat May 05, 2007 12:15 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Hi there,
I posted a solution to this problem in this thread:
http://forum.joomla.org/index.php/topic,139615.0.html

-BoR

Author:  mehdi [ Sat May 05, 2007 12:22 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

hi bestofrisk ,
thanks for your suggestion.
Unfortunately it's not a viable solution for everyone,
as I'm distributing a mambot that automatically add the the required header,
and people just have to install it and activate it, in order to let it work.
Asking them to edit the template complicate uselessly things.

Author:  bestofrisk [ Sat May 05, 2007 12:42 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Hi Medhi,
I don't see anything complicated in this workaround.
It'll just take you a minute to reorganise your index.php file and make it work !
How does your bot work? Do you have a link?

Thanks,
-BoR

Author:  mehdi [ Sat May 05, 2007 12:55 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Quote:
I don't see anything complicated in this workaround.

That's not the point. I'm not willing to force everyone to use such workaround.
I've updated my bot to always include the required javascript header.
But someone also complained that  there's also a problem when joomla's caching is enabled  (will study this later).
Since you asked here's my bot: FBoxBot . The bot only need to include the javascript,
when at least one image is reduced to thumbnail for instance.

Author:  kaiviti [ Sat May 05, 2007 5:34 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Going on what bestofrisk has.

If you have output buffering available then you can try injecting your custom child tags.

A rudimentary function...

Code:
function injectCustomHeadTags($html) {

$buf = ob_get_contents();
ob_clean();

$buf = preg_replace("/<head(.*?)>(.*?)<\/head>/i", "<head$1>$2".$html."</head>", $buf);
echo $buf;

}


you can then call the function in your module. It will only work with output buffering enabled.

medhi: the problem with caching I believe is with content bots only. I had the same problem. I don't think bots triggered by onStart() etc. will have the problem since this content is not cached. The problem with the content bot is the content is cached assuming the bot only writes to content, but instead it also writes to the . So when the cache it turned on, nothing gets inserted into the head.
The solution would be quite simple, just allow content bots to register "onBeforeStart" or "onStart".. whatever the event is called, and actually run the bot even when the cache is on. But I haven't seen the change made in the core...

Author:  kaiviti [ Sat May 05, 2007 6:17 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Just tested the code above and realized i forgot the "s" modifier in the regex epression in preg_replace().

Code:
function injectCustomHeadTags($html) {
   $buf = ob_get_contents();
   ob_clean();
   $buf = preg_replace("/<head(.*?)>(.*?)<\/head>/is", "<head$1>$2".$html."</head>", $buf);
   echo $buf;
}


Note, If output buffering is not available on the server the code is on, then things would go crazy. Maybe someone could modify the code to check for output buffering support..

Author:  kaiviti [ Mon May 07, 2007 6:19 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Updated code. You can now make sure the insert into the HEAD section works or have a fallback such as echoing the HTML into the BODY section.

Code:
function injectCustomHeadTags($html) {
   $buf = ob_get_contents();
   if (!empty($buf) && !headers_sent()) {
      $buf = preg_replace("/<head(| .*?)>(.*?)<\/head>/is", "<head$1>$2".$html."</head>", $buf, 1, $count);
      if ($count == 1) {
         ob_clean();
         echo $buf;
         return true;
      }
   }
   return false;
}


example use:

Code:

if (!injectCustomHeadTags($html)) {
   echo $html;
}

Author:  Waseem Sadiq [ Thu Sep 13, 2007 10:16 am ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Any Idea why this doesn't work in Joomla 1.0.13?

I'm getting this warning:
Code:
Warning: Wrong parameter count for preg_replace() in /home/../../../modules/mod_slider/slider-func.php on line 39

Author:  kaiviti [ Thu Sep 13, 2007 9:15 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

The problem may be your PHP version. preg_replace() does not support the 5th or so parameter in PHP4 or less I think.
So you'll just have to use the older version of the function:

Code:
function injectCustomHeadTags($html) {
   $buf = ob_get_contents();
   ob_clean();
   $buf = preg_replace("/<head(.*?)>(.*?)<\/head>/is", "<head$1>$2".$html."</head>", $buf);
}


On another note:

In Mambots/Plugins you do not need to use this function. You can use the Joomla's API function equivalent to the old: $mainframe->addCustomHeadTag()

You just need to make the call in the global scope of the mambot/plugin, while registering the mambot/plugin events. The event registration if evaluated before the HTML is sent to HTTP but some event functions like the content events are triggered after HTML output is sent.

Author:  Waseem Sadiq [ Sat Sep 15, 2007 7:54 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Hi Kaiviti,

Thanks for that :) as you'll have no doubt guesses I'm not a programmer but I'm learning lol

I'm not actually using this function in a mambot but rather in a module, hence I can't use the code in the same way as a mambot.

I'll give this a whirl and see if it works ,

Waseem

Author:  Waseem Sadiq [ Sat Sep 15, 2007 8:21 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

It worked :)

Thanks again Kaiviti!

Author:  kaiviti [ Sat Sep 15, 2007 11:41 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

You're welcome.

Here's an updated function for PHP4 so you can check if output buffering will work. It will return false if the output buffer is already sent or has not been started.

Code:
function injectCustomHeadTags($html) {
   $buf = ob_get_contents();
   if (!empty($buf) && !headers_sent()) {
      ob_clean();
      echo preg_replace("/<head(.*?)>(.*?)<\/head>/is", "<head$1>$2".$html."</head>", $buf);
      return true;
   }
   return false;
}


I haven't tested it but it should work.

Now you can use it as such:

Code:
if (!injectCustomHeadTags($html)) {
   echo $html;
}


for PHP4.

Author:  henrik_h [ Tue Jan 29, 2008 2:51 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

I just wanted to thank you for your code snippet!

Solved a lot of issues and headaches!

Author:  Ty2u [ Thu Jun 11, 2009 8:25 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

What if you are trying to load multiple instances of a module and you only want the line injected into the head tag once? Does this detect if the line already exists? I don't think it does.

Author:  bucabay [ Thu Jun 11, 2009 8:40 pm ]
Post subject:  Re: $mainframe->addCustomHeadTag() does not work in module

Ty2u wrote:
What if you are trying to load multiple instances of a module and you only want the line injected into the head tag once? Does this detect if the line already exists? I don't think it does.


The above function should be used only for Joomla1.0.

In J1.5 you use the methods of JDocument.

http://api.joomla.org/Joomla-Framework/ ... ument.html

For example to add JavaScript:

Code:
$document =& JFactory::getDocument();
$document->addScript('modules/mymodule/js/script.js');


If you want something loaded only once, you could define a constant as a flag.

eg:

Code:
if (!defined('my_js_loaded')) {
  $document =& JFactory::getDocument();
  $document->addScript('modules/mymodule/js/script.js');

  define('my_js_loaded', true);
}


The same can be used for the Joomla1.0 code.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/