0

I'm trying to add a css to a view, through the preprocess view as below

function mytheme_preprocess_views_view(&$vars) {
 $view = &$vars['view'];
if ($view->name == 'gallery_view') {
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/assets/js/responsive-image-gallery/gallery.js');
drupal_add_css(drupal_get_path('theme', 'mytheme') .'/assets/css/responsive-image-gallery/style.css');
 }
}

The Following code above is inserting the js file but not the css. Is there a reason why? Or other way to insert css file in certain view?

I have tried the following and its being inserted:

drupal_add_js(drupal_get_path('theme', 'zenon') .'/assets/css/responsive-image-gallery/style.css');

So is there something missing in the drupal_add_css function or it doesnt have read access on the server, is that possible?

1

the path is wrong!

drupal_get_path('theme', 'mytheme') already provides the path to your theme folder. no need for '../../../...'

you should probably use

drupal_get_path('theme', 'zenon') . '/css/image-gallery/css/style.css'.

  • i tried your path, but it did not work. If the path was wrong, it would have given me a 404 in the console, but its not being inserted at all. Whereas the js is being inserted. – Hicham Nov 18 '15 at 8:15
  • There's no reason why one would work and another not when they're one line apart - your path's are definitely wrong though, the return from drupal_get_path is the path to the theme, no need to traverse up and then back down again (plus you're missing a leading /). Make sure those are fixed first so you can rule out silly errors – Clive Nov 18 '15 at 8:51
  • Yes you're right about the paths, I have fixed them, but still its not working. – Hicham Nov 18 '15 at 8:57
1

I agree with Trudog, you should not use a relative path when using drupal_get_path.

Also, I'm surprised the javascript gets inserted because both paths are incorrect. The function drupal_get_path does not contain a trailing slash so what you get now is something like this (assuming your themes folder is in sites/all):

drupal_add_css('sites/all/themes/mytheme../../../themes/mytheme/assets/css/image-gallery/css/style.css');

The folder mytheme.. probably does not exist. Adding a trailing slash would result in:

sites/all/themes/mytheme/../../../themes/mytheme/assets/css/image-gallery/css/style.css

Which is the same as:

sites/themes/mytheme/assets/css/image-gallery/css/style.css

That location probably does not exist either.

I think the correct call would be:

drupal_add_css(drupal_get_path('theme', 'mytheme') .'/assets/css/image-gallery/css/style.css');

The same goes for your javascript call.

  • I have already fixed the path as suggested by Trudog and you, please check updated post above. And even if path i wrong it would have given a 404 in console, but its not being inserted at all using drupal_add_css – Hicham Nov 18 '15 at 9:18
  • Right, sorry, I was still composing my answer. Can you reach the CSS in the browser? – Patrick Scheffer Nov 18 '15 at 9:21
  • Its all right. Yes I can reach it through browser. – Hicham Nov 18 '15 at 9:24
  • If you print drupal_get_path('theme', 'mytheme') .'/assets/css/responsive-image-gallery/style.css' to the screen and paste the result in your browser, does it still work? – Patrick Scheffer Nov 18 '15 at 9:33
1

then try this:

function mytheme_preprocess_views_view(&$vars) {
 ... everything you already have there +
 $css_path = drupal_get_path('theme', 'mytheme') .'/assets/css/responsive-image-gallery/style.css';
 var_dump($css_path);
 }
}

make sure that the string printed on the screen is a valid path to your css file by copying the printed string and putting it in browser's address bar + enter. is should display the css content. if 404, something's wrong with the path... maybe spelling or something.

0

Trudog way below is a workaround for the css file to be inserted, and it works.

But after further debugging my case, I found the main cause of the problem was in the file permission. Apache did not have read access to the responsive-image-gallery folder. Resolved it with the following command on the server:

chown -R apache:apache /var/www/Demo/sites/all/themes/mytheme/assets/css/responsive-image-gallerry

Then restarted apache just incase it did not take affect, and it worked like a charm

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.