Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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 :(

share|improve this question
    
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. –  mikeytown2 Oct 8 '14 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. –  Raja Dushyant Oct 8 '14 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/… –  mikeytown2 Oct 8 '14 at 5:49
    
That i have just implemented but still CSS are not loading up in hook_init(). –  Raja Dushyant Oct 10 '14 at 5:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.