Tom
Tom

Reputation: 2634

joomla plugin extension loads css file on every page

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

Upvotes: 5

Views: 916

Answers (4)

vorillaz
vorillaz

Reputation: 6266

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');
}

Upvotes: 5

Elin
Elin

Reputation: 6755

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;
    }

Upvotes: 0

Irfan
Irfan

Reputation: 7059

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.

Upvotes: 0

Lodder
Lodder

Reputation: 19733

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.

Upvotes: 0

Related Questions