In Drupal 7, if you try adding CSS and/or JS using drupal_add_cs() and/or drupal_add_js() inside hook_block_view(), you will soon find that your CSS and JS files are not included when that block is cached. This of course is no good, as blocks often require the CSS to look right, and the javascript to behave as desired.

Here is an example of what doesn't work:

<?php
function my_module_block_view($delta '')
{
  if(
$delta == 'my_module_delta')
  {
    
drupal_add_js('path/to/file.js');
    
drupal_add_css('path/to/file.css');
    
$block = array
    (
      
'subject' => t('My block title'),
      
'content' => '<p>' t('This is some text that will go in the block.') . '</p>',
    );
    return 
$block;
  }
}
?>

The above may initially work, but if you turn on caching, or if you have defined your block to be cached in hook_block_info(), the CSS and JS files will fail to be included.

The secret to this is to create the block as a render array, and use the drupal 7 #attached attribute to attach the CSS and JS files. The above example, which directly inserts the content without passing it through a themeing function, would be written as follows:

<?php
function my_module_block_view($delta '')
{
  if(
$delta == 'my_module_delta')
  {
    
$block = array
    (
      
'subject' => t('My block title'),
      
'content' => array
      (
        
'#markup' => '<p>' t('This is some text that will go in the block.') . '</p>',
        
'#attached' => array
        (
          
'js' => array('path/to/file.js'),
          
'css' => array('path/to/file.css'),
        ),
      ),
    );
    return 
$block;
  }
}
?>

The above function passes the contents of the 'content' element as a render array that has two elements - #markup, which passes through the content as-is, and #attached, which attaches the CSS and JS.

Hopefully this can help someone who is having a difficult time understanding why the CSS and/or JS they have tried to include in their block in Drupal 7 isn't being attached.