0

I am working on a drupal-7 project that have many CSS/JS files that need to be added dynamically. This is the code which i am using in mymodule_init()

function doLookup($baseRootDir)
    {
    if (variable_get('xmldata')) {
        $xmldata = variable_get('xmldata');
    } else {
        $xmldata = '';
    }
    $rootDir = realpath($baseRootDir . DIRECTORY_SEPARATOR . $xmldata);
    $di = new RecursiveDirectoryIterator($rootDir);
    foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
        $fileInfo = new SplFileInfo($filename);
        if (strtolower($fileInfo->getExtension()) == "css") {
            // Include this CSS file in drupal
            drupal_add_css($cssFileName, array('type' => "file", 'group' => CSS_DEFAULT, 'every_page' => TRUE));
        } elseif (strtolower($fileInfo->getExtension()) == "js") {
            // Include this JS file in drupal
        }
    }
}

I am calling this function in hook_init() which doesn't seem to be working. I have tried clearing drupal cache as well.
Please help me out i need to complete this asap :(

4
  • Make sure that function is actually being called, drop a drupal_set_message in it. After that your directory file scan looks suspect and unnecessarily slow. Commented Oct 8, 2014 at 5:27
  • Yes i have tried that. Function is being called and control is going in inner most if condition as well. Also what is suspicious in this dir scan please let me know if i am doing something wrong. Commented Oct 8, 2014 at 5:31
  • 1
    Personally I would hardcode the location of the CSS/JS files. If that's not possible try using api.drupal.org/api/drupal/includes%21file.inc/function/… Commented Oct 8, 2014 at 5:49
  • That i have just implemented but still CSS are not loading up in hook_init(). Commented Oct 10, 2014 at 5:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.