Correct Way in Adding JS/CSS to a Block in Drupal 7

In Drupal, drupal_add_js() and drupal_add_css() are great functions for adding JS/CSS to the module or theme layer. However, if you do this inside of the ‘#markup’ call in Drupal 7, you are doing it wrong.

This becomes obvious when you turn block caching or anonymous page caching on, because the JS/CSS won’t be included on the page. Here is the code in how to do it correctly.

 * Implements hook_block_info().
function mymodule_block_info() {
  $blocks['testblock'] = array(
    'info' => t('Testing Block'),

  return $blocks;

 * Implements hook_block_view().
function mymodule_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'testblock':
      $block['subject'] = t('Testing block');
      $block['content'] = array(
        '#markup' => mymodule_testblock_content(),
        '#attached' => array(
          'css' => array(
            drupal_get_path('module', 'mymodule') . '/css/mymodule.css',
          'js' => array(
            drupal_get_path('module', 'mymodule') . '/js/mymodule.js',
  return $block;

function mymodule_testblock_content() {
  return '<p>This is a testing block!</p>';

This code will ultimately call drupal_add_js() and drupal_add_css(), but it is hit even with block caching on. It makes sense when you think about it, because block caching grabs the html generated in ‘#markup’ once, and doesn’t call it again when it is cached.

Here is a great article that goes through another example in how to do this.

Options for Building a Website from a Developers Perspective

Over the years I’ve built many different types of websites. These range from being a few pages, to being very customized with advanced features. I’ve learned there is no clear definition in the best way to create a website. But I do think there are advantages and disadvantages to pursuing different methods. This article takes an analytical look at each option. Continue reading

Drupal 8: A Look Ahead

Since the release of Drupal 7, there has been much talk about what to expect for the next major release of Drupal. Drupal’s founder Dries Buytaert has outlined some of the goals for Drupal 8. These goals provide some insights into what we are likely to see in the next Drupal version. Continue reading

What does it mean to be an Advanced PHP Programmer?

On a previous post I had someone comment that they did not agree that the code implementation that was presented in the article was advanced (which was described in the title). They also claimed that I was not an advanced PHP programmer.

This made me think. Not because my programming skill or knowledge was challenged. But because I’m not sure what makes code or a programmer “advanced”. This article is meant to take a look at this from an analytical perspective. I’m less concerned about general labels, and more concerned in how to improve going things going forward. Continue reading

Advanced PHP Form Validation

Last year I wrote an article in how to implement basic validation for a form with PHP. I decided to re-look at this and improve what I did in that article. This time we are going to make a more advanced PHP form that is more responsive and effective.

This PHP sample code has many advantages over the previous article. Not only that, but for the most part it is easier to implement with more complexed forms where you need more than basic validation. Continue reading