1

I'm creating a new menu module, but I keep running into the same problem.

I'm trying to make my stylesheet variable, by taking params that people can fill in in the admin panel, and putting them in my stylesheet. To do so, the stylesheet is actually a PHP file. However, when I try to define JPATH_BASE, this is how it results:

C:\xampp\htdocs\sandbox\joomlaStands\modules\mod_improvedmenu\assets\css/../../../

here's the code:

<?php header("Content type: text/css; charset: ISO-8859-1");

define( 'DS', DIRECTORY_SEPARATOR );
define( '_JEXEC', 1 );
define('_VALID_MOS',1);
define('JPATH_BASE', dirname(__FILE__). '/../../../');
define('JPATH_PLATFORM', dirname(__FILE__). '/../../../'. 'libraries');

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$user =& JFactory::getUser();
$session =& JFactory::getSession();

jimport('joomla.application.module.helper');

$module = JModuleHelper::getModule('mod_improvedmenu');
$params = new JRegistry();
$params->loadString($module->params);

?>

.main-menu-bar {

    background-color: <?php  
        if($params->get('entireMenuBgColor') != "") {
            echo "".$params->get('entireMenuBgColor')."";
        }
    ?>;

    <?php 
        if($params->get('backgroundImage') != "-1") {
            echo "background-image: url('". JURI::ROOT(false, "")."images/menu/".$params->get('backgroundImage').";";
        }

    ?>

    background-repeat: repeat-x;

    <?php if($params->get('font-style') != "-1") {
        echo $params->get('font-style');    
    } else { 
        echo "normal"; 
    }   ?>;

    <?php if($params->get('font-family') != "") {
        echo "font-family: ".$params->get('font-family')."";
    }   ?>

}

Thing is, the code used to work in an older version of the module. What am I doing wrong and how can I get it to define properly?

2

You've gone about this in completely the wrong way. There is no need to import the Joomla API for a file that is going to be used for styling.

All your styling that is based on parameters should go into the default.php file and will be 10x easier to maintain.

In this file, you can make full use of Joomla's addStyleDeclaration() function.

So step 1, completely remove your separate PHP file. Abandon it and never look back at it.

Step 2, assuming you already have a default.php file (if not, create one), add the following:

$doc = JFactory::getDocument();

$css = '
    .main-menu-bar {    
        background-color: ' . $params->get("entireMenuBgColor", "DEFAULT_VALUE") . ';
        background-image: url(' JUri::root() . 'images/menu/' . $params->get("backgroundImage", "DEFAULT_VALUE") . ') ;
        background-repeat: repeat-x;        
        font-style: ' . $params->get("font-style", "DEFAULT_VALUE") . ';        
        font-family: ' . $params->get("font-family", "DEFAULT_VALUE") . ';
    }
';

$doc->addStyleDeclaration($css);

I've added in DEFAULT_VALUE which you can replace. This will set a default value if there is no value for the parameter.

As you can see, much nice coding methods, much smaller, and no need to import the API which will simply decrease page loading speeds.

Hope this helps

| improve this answer | |
  • it's a rather huge stylesheet, so maybe it'd be wise to put it in a seperate file but import it with these functions? what do you think? thank you so much for this answer, I had no idea. this'll help; a lot! – Hocho Nov 4 '14 at 10:59
  • also I'd still like to know why I get such a weird result when I use the define. it literally returns it as /../../.. rather than going back a few folders... – Hocho Nov 4 '14 at 11:07
  • I would simply keep the code in my answer in the default.php or you can add it to your mod_improvedmenu.php file. It's entirely up to you. As for JPATH_BASE, try adding echo JPATH_BASE; in your default.php file and see what the output is. DO NOT redefine it though. It has been defined already by Joomla ;) – Lodder Nov 4 '14 at 11:09
  • it's fine in my default file. it still leaves me with the question why, when I define a constant with a directory, it doesn't let me go back a few folders. I need this information for other things as well. this: define('COOLIO', dirname(FILE). '/../../../'); echo COOLIO; returns: C:\xampp\htdocs\sandbox\joomlaStands\modules\mod_improvedmenu\tmpl/../../.. – Hocho Nov 4 '14 at 11:11
  • 1
    It wasn't working for you because you didn't define it correctly. You need to use realpath so for you, it would be define('JPATH_BASE', realpath(dirname(__FILE__) . '../../../'));. The way you were try to define it, PHP was interpreting ../../../ as a string. Hope this helps – Lodder Nov 4 '14 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.