Hi!

I'm learning to develop Drupal modules. So far everything goes far, except problem with theming block. In fact my only problem is function drupal_add_css().

I have this simple block of code


function theme_blogesn_block_theme($data){
	//add CSS for theming
	$module_path = drupal_get_path('module','blogesn');
	$css_path = $module_path.'/blogesn_theme.css';
	drupal_add_css($css_path); //this seems to not work properly but I don't know what's wrong
	
	$date = date("d F Y H:i", $data['node_created']);
	
	
	$output = '<li class="blogesn-block-theme-li">';
	$output .= '<div class="blogesn-block-theme-li-date">'.$date.'</div>';
	$output .= '<div class="blogesn-block-theme-li-title">'.l($data['title'], 'node/'.$data['nid']).'</div>';
	$output .= '<div class="blogesn-block-theme-li-author">'.t('author: !user_name', array('!user_name' => $data['user_name'])).'</div>';
	$output .= '</li>';
	
	return $output;
}

It seems that CSS is not added. Could you tell me why? I was searching Internet for some answers but found nothing useful so far.
While writing this module I am basing on content_complete module and book "Learning Drupal 6 Module Development". It's just like in the book, but doesn't work! Any ideas?

Regards,
ventus

Comments

prakashp’s picture

I think drupal_add_css is expecting a path relative to base path whereas drupal_get_path does not return the path relative to base path.

This is what you need to do

$module_path = base_path() . drupal_get_path('module','blogesn');
$css_path = $module_path . '/blogesn_theme.css';
drupal_add_css($css_path);
ventus’s picture

Thanks for replay!

Unfortunately it doesn't work either :/ I'm confused now...

gbarrigap’s picture

Hi!

Have you tried to load the CSS with hook_init?

function blogesn_init() {
  drupal_add_css(drupal_get_path('module', 'blogesn') . '/blogesn_theme.css');
}

Regards!

http://api.drupal.org/api/function/hook_init/6

ventus’s picture

I've tried that... doesn't work either :(

jaypan’s picture

drupal_get_path() does give the path relative to the root.

The first thing to do is view the source of the page, and compare the path in the source to the actual file location.

You should also go to admin/settings/performance and see if you are aggregating CSS files. If you are, then you will need to clear the cache before any changes you make are reflected. Also, if you are using the 'color' module, or changing the color of your theme at all through the drupal interface, you will probably need to clear that out of the cache as well.

Contact me to contract me for D7 -> D10/11 migrations.

ventus’s picture

Thank you all for replays!

I've solved the problem. Of course everything is my fault, drupal_add_css works fine. The problem was my .css file, not drupal's function.
Thank you once again :)

Best regards,
ventus