0

I am trying to create a custom module that programmatically creates a LESS file.

I created a menu callback that is a file. Here is hook_menu()...

$items['sites/all/themes/universal/less/dynamic.css.less'] = array(
  'page callback' => 'jobs_universal_lessvars',
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK
);

Here is the callback.

function jobs_universal_lessvars(){ 
  header('Content-type: text/css');
  $output = '
    @color: #4D926F;
    body.notaclass {
      color:@color;
    }
  ';
  print $output;
}

And I call this to add it.

function universal_preprocess_html(&$variables) {
  drupal_add_css(drupal_get_path('theme','universal').'/less/dynamic.css.less');
}

I uploaded a static file to the above ^^ path, disabled the menu callback, and it processed/applied correctly. When I try to run this module, it's not included and I get an error saying

LESS error: load error: failed to find sites/all/themes/universal/less/dynamic.css.less

Before you answer...

  1. I have the LESS module installed
  2. I have the LESS library uploaded correctly
  3. I have tested that I can include a static .less file and have it process
  4. I have tested the menu callback by manually going to the URL
  5. I have tried it with and without calling header('Content-type: text/css');

I get it... it's not a file. But it "is", right? I could just call this function and have it create a new CSS file that's added, but it doesn't fulfil the requirements of the module. Help me? :) Programmatically creating files is very new to me!

  • I believe LESS needs the full server side path to the file. you want something like drupal_add_css(DRUPAL_ROOT . drupal_get_path('theme', 'universal') . '/less/dynamic.css.less');. I'm confused why you need to do this if you already have the LESS module installed; what are you achieving with this ? – tenken Dec 19 '13 at 22:54
  • the issue is drupal_add_css() is looking for an actual file in the filesystem of your host/server, while drupal and its menu_hook() send information out via the host's webserver giving the impression that the file exists. – Jimajamma Dec 19 '13 at 23:27
2

It may appear like a file to the browser if you were to directly request that path in the browser, but drupal_add_css() and the LESS module deal with the file system directly and does not attempt to resolve file paths through Drupal's menu system.

drupal_add_css() will not add files that do not exist on the file system.

If you are attempting to have programmatically assigned variables within a .less file I would suggest checking out the Variables functionality that is available within the latest version of the LESS CSS Preprocessor module, documentation here: https://drupal.org/node/2139603

If you are truly trying to create a dynamically composed .less file, you first need to create the file in the files directory of the drupal install and then add that file with drupal_add_css().

  • Thank you so much!! Perfect... I had no idea there were less hooks. Thank you :) I would 1 up you if I could! – Taylor Evanson Dec 19 '13 at 23:06

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.