5

I'm using a custom plugin extension with Joomla 2.5. This plug-in is targeted for a single page on my site. But for some reason I noticed it loads the css file on every single page.

The code for the plugin loads this css file:

$doc->addStyleSheet($pluginAssetsUrl . '/css/aecuserpage.css');

I don't assume this is common but I'm not sure about plugins. Can I make it only apply the the page I want instead of every single page. Any thoughts on how to debug this would be appreciated.

The plugin prints a simple table on an article page and uses the plugin function onContentPrepare

Tom
  • 2,604
  • 11
  • 57
  • 96
  • What is the plugin doing? For example if it's targeting a specific class/id you could search for the class/id and if it exists then include the css file. – George Wilson Dec 10 '12 at 12:00

4 Answers4

5

Use these snippets to manage your problem:

1) Load CSS by checking the active menu item

$menu = &JSite::getMenu();
$menuItem = $menu->getActive();
$Itemid = $menuItem->id;
if($Itemid!=1)
{
 $doc->addStyleSheet($pluginAssetsUrl . '/css/aecuserpage.css');
}

2)Load CSS upon component

if (JRequest::getCmd( 'option' ) == 'com_k2'){
        $doc->addStyleSheet($pluginAssetsUrl . '/css/aecuserpage.css');
}

3)Load CSS only in home page

<?php if(JRequest::getInt('Itemid') == $menu->getDefault()) {
   $doc->addStyleSheet($pluginAssetsUrl . '/css/aecuserpage.css');
}?>

4)Load CSS upon active menu id

<?php
$menuID = JSite::getMenu()->getActive()->id ;
  if ($menuID == '6')
  {
   $doc->addStyleSheet($pluginAssetsUrl . '/css/aecuserpage.css');
  }
?>

5)Load CSS upon active language:

$lang =& JFactory::getLanguage();
switch ($lang) {
case 'en-gb':
$doc->addStyleSheet($pluginAssetsUrl . '/css/aecuserpage.css');
break;
}

6)Load CSS upon users

$user =& JFactory::getUser();
if($user->get('id')==0){
//user is logged in
 $doc->addStyleSheet($pluginAssetsUrl . '/css/aecuserpage.css');
}

7)Load CSS upon URL

$u =& JFactory::getURI();
if($u=="http://www.example.com/joomla/index.php?task=view&id=12&Itemid=29")
{
$doc->addStyleSheet($pluginAssetsUrl . '/css/aecuserpage.css');
}
vorillaz
  • 6,098
  • 2
  • 30
  • 46
0

Try using something like the following:

$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem(1);
if ($item == 1){
    $doc = JFactory::getDocument(); //Remove it you already have this line
    $doc->addStyleSheet($pluginAssetsUrl . '/css/aecuserpage.css');
}

Change the number "1" to whatever menu item the page belongs to.

Please note I haven't tested this so let me know if it works. If it doesn't, I'll see what i can dig up.

Lodder
  • 19,758
  • 10
  • 59
  • 100
  • Close, thanks. This code definitely stops it from mostly loading (oddly it still shows up on the root of the site). I'm playing around with the menu item id as it also doesn't now work on the page I want it to work with...sigh. I'll research more if `getMenu` or `getItem` are doing what they are supposed to. – Tom Nov 25 '12 at 04:15
  • Tried everything, with no luck. Tried `getItems('menutype','mymenu')` which at least returns something. `getItem` does not. Any other ideas? – Tom Nov 25 '12 at 19:17
0

I do not think that getting Itemid inside plugin will work. I have a suggestion that you move the css code in template index.php and check the itemid condition it'll work there.

Irfan
  • 7,029
  • 3
  • 42
  • 57
0

What event is the plugin responding to? What group is it in? For many events you can check the $context of the page. If you know that it will only load on a certain page use those identifiers.

e.g. here is a line from the pagination plugin:

if ($params->get('show_item_navigation') && ($context == 'com_content.article') && ($view == 'article')) {

or this from loadmodule

    $canProceed = $context == 'com_content.article';
    if (!$canProceed) {
        return;
    }
Elin
  • 6,507
  • 3
  • 25
  • 47
  • The event is: `onContentPrepare` – Tom Dec 10 '12 at 16:13
  • Right so that happens on every single page. So what you want to do is do an if ($context != whatever) {return;} look at some of the other content plugins you will see that basically you are checking the url. – Elin Dec 11 '12 at 00:56
  • I'm not sure I understand what the "whatever" part is? Other plugins have some stuff as you put in your answer but I'm not sure what they mean. For example loadmodule I have no idea what `com_content.article` is. – Tom Dec 11 '12 at 03:11
  • The url for an article would be index.php?option=com_content&view=article&id=2 (for article 2). In other words that is asking what is in the url and if the url does not indicate that we are in an article then don't continue with running the plugin. – Elin Dec 11 '12 at 10:44
  • Okay, getting there. If I do the same as loadmodule it does only load for articles. But now to be more specific how would I specify article id 30 for example? – Tom Dec 12 '12 at 16:51
  • Get the id from the request using jinput (assuming you can't just add it to the context com_content.article.30. – Elin Dec 14 '12 at 11:55