1

I have an external script / php application that I now have to implement into a running D7 site. So far DRUPAL_BOOTSTRAP_FULL seems to do the job just fine – I can read the session, check the user etc.

But I am currently stuck trying to load the $styles and $scripts into the head of that external application. I tried drupal_get_css();, also checked the $variables and also tried template_preprocess_html(); to preprocess the variables –– but D7 won't return anything.

What I have so far:

define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
$base_url = 'http://'.$_SERVER['HTTP_HOST'];
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

I hope somebody can direct me in some direction – any pointers appreciated. Thank you!

Update: The solution suggested by oksana-c works quite well if you 'only' need to load the top css and js files. The site I'm working on however caches and aggregates all js and css files into one – including various files provided by different modules… So what I would actually need to find out is how to link to those aggregated js and css files. Anybody?

Update2: I could 'crawl' the folders where the aggregated css and js are stored – however currently I can see 4 different js files and 4 different css files in the head. Also according to the files' timestamps sometimes only 2 files were generated and loaded in the past – so I have basically no indication of how many of the most recent files I should include. Probably some hook would help that would let me list whatever cached/aggregated files are loaded at this time – but I still have no idea where/how to hook into the caching/aggregating functions…

3

Here's the gist of it (w/ commentary).

<?php
// ... your code 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// get current theme and it's variables
$current_theme = variable_get('theme_default','none');
$themes = list_themes();
$theme_object = $themes[$current_theme];

// uncomment to display structured information about $theme_object
// var_dump($theme_object);

// get current theme path
$theme_path = dirname($theme_object->filename);

    // get path of each stylesheet from current theme
  $final_stylesheets = array();
    if (!empty($theme_object->stylesheets)) {
    foreach ($theme_object->stylesheets as $media => $stylesheets) {
      foreach ($stylesheets as $name => $stylesheet) {
        $final_stylesheets[$media][$name] = $stylesheet;
      }
    }
  }
  foreach ($final_stylesheets as $media => $stylesheets) {
    foreach ($stylesheets as $stylesheet) {
            // will print relative path to the stylesheet
            // here you can wrap each stylesheet in <link type="text/css" ....> tags
            // and compose the output for hte head section of your script
            print $stylesheet;
    }
  }

    // Do same for scripts
  if (!empty($theme_object->scripts)) {
    foreach ($theme_object->scripts as $name => $script) {
      $final_scripts[$name] = $script;
    }
  }
  foreach ($final_scripts as $script) {
    print $script;
  }
?>

all you have left to do is to wrap each $stylesheet and $script in appropriate tags and insert that into the head.

| improve this answer | |
  • Thank you very much! I finally managed to implement the solution you suggested –– and it does basically load the basic stylesheets and the basic js files. But there seem to be still some files missing – I guess it's the module's css and js files… The site I'm working on also aggregates / caches the js and css files into one. So what I would actually need is to get the path to those files… :/ – tillinberlin Mar 16 '16 at 16:21
  • aggregated css & js files are usually placed in folders sites/default/files/public/css, sites/default/files/public/js. If you need to load those, then white a script that will find all files in those folders and include them. This sounds a bit overkill though if core aggregation is used. You might want to consider using the AdvAgg module that @mikeytown2 suggested. – oksana-c Mar 17 '16 at 10:22
  • Thank you for your suggestion! The problem is: there is about 50 js and css files generated in the last 6 weeks. Sometimes 2 files are generated at a time, sometimes 3, sometimes 4. At the moment i see 4 js files and 4 css files loaded in the header. I was hoping that there would be some function I could hook in to load whatever is currently needed. Also I can actually not install additional modules since a different contractor is taking care of everything concerning dp7. I can only use whatever is there… – tillinberlin Mar 17 '16 at 10:51
  • 1
    you see 50 or so files because drupal generates different sets(combinations) of files according to what modules need to load on pages. that's why you see only 4 files in the page header. If you load another page that utilizes features from other modules - you'll see different combination of files. The questions here are, why your external script should load all of those files, and_ if it should load only those that are needed - how is your ext. script will define what files are really needed to run successfully? Your objective is kind of vague. – oksana-c Mar 17 '16 at 14:27
  • I finally came back to the project – and found that the 'missing' details were just placed in another stylesheet called "dirty.css" … (Wow!) So I just added those missing styles to my stylesheet and load the default css the way you described above. Thank you @oksama-c !! – tillinberlin Apr 20 '16 at 11:51

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.