Joomla! Discussion Forums



It is currently Wed Nov 25, 2009 3:46 am (All times are UTC )

 




Post new topic Reply to topic  [ 23 posts ] 
Author Message
Posted: Tue Sep 05, 2006 10:29 pm 
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Tue Sep 05, 2006 10:04 pm
Posts: 33
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)


Top
  E-mail  
 
Posted: Tue Sep 05, 2006 10:35 pm 
User avatar
Joomla! Guru
Joomla! Guru
Offline

Joined: Sat Sep 10, 2005 10:31 pm
Posts: 823
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.

_________________
We may not be able to control the wind, but we can always adjust our sails


Top
   
 
Posted: Tue Sep 05, 2006 10:48 pm 
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Tue Sep 05, 2006 10:04 pm
Posts: 33
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


Top
  E-mail  
 
Posted: Mon Sep 18, 2006 7:43 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Sun Apr 23, 2006 4:21 pm
Posts: 135
Location: Washington, DC
did you happen to find a solution???


Top
   
 
Posted: Mon Jan 22, 2007 9:35 am 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast
Offline

Joined: Tue Aug 23, 2005 7:17 pm
Posts: 178
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

_________________
http://www.sintagespareas.gr :  Recipes in Greek


Top
  E-mail  
 
Posted: Thu Mar 08, 2007 12:31 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Mon Jun 12, 2006 11:23 pm
Posts: 22
Location: Iceland
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  :-\


Top
  E-mail  
 
Posted: Mon Mar 12, 2007 9:44 pm 
User avatar
Joomla! Intern
Joomla! Intern
Offline

Joined: Fri Aug 19, 2005 5:50 am
Posts: 74
Location: London / Los Angeles
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...


Top
  E-mail  
 
Posted: Fri May 04, 2007 12:26 pm 
Joomla! Explorer
Joomla! Explorer
Offline

Joined: Tue Oct 04, 2005 3:56 pm
Posts: 428
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.


Top
  E-mail  
 
Posted: Sat May 05, 2007 12:15 pm 
Joomla! Fledgling
Joomla! Fledgling
Offline

Joined: Fri May 04, 2007 7:57 am
Posts: 3
Hi there,
I posted a solution to this problem in this thread:
http://forum.joomla.org/index.php/topic,139615.0.html

-BoR


Top
  E-mail  
 
Posted: Sat May 05, 2007 12:22 pm 
Joomla! Explorer
Joomla! Explorer
Offline

Joined: Tue Oct 04, 2005 3:56 pm
Posts: 428
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.


Top
  E-mail  
 
Posted: Sat May 05, 2007 12:42 pm 
Joomla! Fledgling
Joomla! Fledgling
Offline

Joined: Fri May 04, 2007 7:57 am
Posts: 3
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


Top
  E-mail  
 
Posted: Sat May 05, 2007 12:55 pm 
Joomla! Explorer
Joomla! Explorer
Offline

Joined: Tue Oct 04, 2005 3:56 pm
Posts: 428
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.


Top
  E-mail  
 
Posted: Sat May 05, 2007 5:34 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Sat May 05, 2007 4:12 pm
Posts: 22
Location: Fiji
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...

_________________
- F I J I -


Top
  E-mail  
 
Posted: Sat May 05, 2007 6:17 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Sat May 05, 2007 4:12 pm
Posts: 22
Location: Fiji
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..

_________________
- F I J I -


Top
  E-mail  
 
Posted: Mon May 07, 2007 6:19 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Sat May 05, 2007 4:12 pm
Posts: 22
Location: Fiji
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;
}

_________________
- F I J I -


Last edited by kaiviti on Mon May 07, 2007 6:23 pm, edited 1 time in total.

Top
  E-mail  
 
Posted: Thu Sep 13, 2007 10:16 am 
User avatar
Joomla! Ace
Joomla! Ace
Offline

Joined: Mon Sep 12, 2005 7:41 pm
Posts: 1374
Location: Glasgow - Scotland
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

_________________
http://www.bulletprooftemplates.com/ - New Joomla 1.5 templates from an old Joomla head
If you don't know the answer don't be afraid to ask someone who does


Top
  E-mail  
 
Posted: Thu Sep 13, 2007 9:15 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Sat May 05, 2007 4:12 pm
Posts: 22
Location: Fiji
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.

_________________
- F I J I -


Top
  E-mail  
 
Posted: Sat Sep 15, 2007 7:54 pm 
User avatar
Joomla! Ace
Joomla! Ace
Offline

Joined: Mon Sep 12, 2005 7:41 pm
Posts: 1374
Location: Glasgow - Scotland
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

_________________
http://www.bulletprooftemplates.com/ - New Joomla 1.5 templates from an old Joomla head
If you don't know the answer don't be afraid to ask someone who does


Top
  E-mail  
 
Posted: Sat Sep 15, 2007 8:21 pm 
User avatar
Joomla! Ace
Joomla! Ace
Offline

Joined: Mon Sep 12, 2005 7:41 pm
Posts: 1374
Location: Glasgow - Scotland
It worked :)

Thanks again Kaiviti!

_________________
http://www.bulletprooftemplates.com/ - New Joomla 1.5 templates from an old Joomla head
If you don't know the answer don't be afraid to ask someone who does


Top
  E-mail  
 
Posted: Sat Sep 15, 2007 11:41 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Sat May 05, 2007 4:12 pm
Posts: 22
Location: Fiji
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.

_________________
- F I J I -


Top
  E-mail  
 
Posted: Tue Jan 29, 2008 2:51 pm 
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Thu Aug 16, 2007 12:40 pm
Posts: 34
I just wanted to thank you for your code snippet!

Solved a lot of issues and headaches!


Top
  E-mail  
 
Posted: Thu Jun 11, 2009 8:25 pm 
User avatar
Joomla! Guru
Joomla! Guru
Offline

Joined: Tue Aug 23, 2005 6:41 am
Posts: 519
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.

_________________
TY2U.com Designs http://www.ty2u.com - Free Joomla! Templates


Top
  E-mail  
 
Posted: Thu Jun 11, 2009 8:40 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice
Offline

Joined: Fri Oct 14, 2005 9:12 pm
Posts: 43
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.

_________________
Beta our open source Instant Messaging Extensions for Joomla.


Top
  E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ] 

Quick reply

 



Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group