Using hook_theme() when there is a preprocess We had a outside company help us with our Drupal version of our site. They created the code below.

Since then we have hired another company that seems to be more professional about there code and standards.

The code below is the entire code of the module. The new company suggested that we "Implement hook_theme and create a template for alerts, move markup from
hook_preprocess_page to the template."

I understand a bit about Drupal modules, but I'm unsure how to accomplish what they are suggesting.

Can someone help me in this regard?

&lt;?php

function foo_alerts_preprocess_page(&amp;$vars) {
  $vars['alerts'] = '';

  if ($nid = foo_alerts_check_alerts()) {
    $node = node_load($nid);

    if (isset($_COOKIE['alert'.$node-&gt;nid]) &amp;&amp; $_COOKIE['alert'.$node-&gt;nid] == 'closed') {
      return;
    }

    $html = '&lt;div id="alert-bar" rel="'.$node-&gt;nid.'"&gt;&lt;div class="inner"&gt;';
    $html .= '&lt;h2&gt;News Alerts:&lt;/h2&gt;';
    if (isset($node-&gt;field_link['und'][0]['url'])) {
      $html .= l($node-&gt;title, $node-&gt;field_link['und'][0]['url'], array('attributes' =&gt; array('title' =&gt; $node-&gt;field_link['und'][0]['title'], 'class' =&gt; array('alert-link'))));
    } else {
      $html .= '&lt;span class="alert-link"&gt;' . $node-&gt;title . '&lt;/span&gt;';
    }
    $html .= '&lt;a href="#" id="close-alert-bar"&gt;&lt;/a&gt;';

    $html .= '&lt;/div&gt;&lt;/div&gt;';
    $vars['alerts'] = $html;
  }
}

function foo_alerts_check_alerts() {
  return db_query("SELECT nid FROM {node} 
    LEFT JOIN {field_data_field_dates} ON entity_id=nid
    WHERE type='alert' AND field_dates_value &lt; NOW()
    AND field_dates_value2 &gt; NOW() AND status = 1 LIMIT 1")-&gt;fetchField();
}
So, hook_theme is where you define your theme implementations and templates to use for them. In your case, that would look something like:

foo_alerts_theme() {
  $module_path = drupal_get_path('module', 'foo_alerts');
  return array(
    'alerts_theme_name' =&gt; array(
      'path' =&gt; $module_path . '/templates',
      'template' =&gt; 'alerts-theme-template-name',
      'variables' =&gt; array(
        'node' =&gt; NULL,
      );
    ),
  );
}


Your template file, named alerts-theme-template-name.tpl.php goes in the templates folder in your foo_alerts module folder. In your page preprocess you're going to pass in the node loaded from the alerts function to the theme function, so that would look like this:

function foo_alerts_preprocess_page(&amp;$vars) {
  $vars['alerts'] = '';

  if ($nid = foo_alerts_check_alerts()) {
    $node = node_load($nid);

    if (isset($_COOKIE['alert'.$node-&gt;nid]) &amp;&amp; $_COOKIE['alert'.$node-&gt;nid] == 'closed') {
      return;
    }

    $vars['alerts'] = theme('alerts_theme_name', array('node' =&gt; $node));
  }
}


That leaves all of the non-template logic still in the preprocess function, and returns the output of theme(), which will be the html from your template. Your template would then be something like this:

&lt;div id="alert-bar" rel="&lt;?php print $node-&gt;nid; ?&gt;"&gt;
  &lt;div class="inner"&gt;
    &lt;h2&gt;News Alerts:&lt;/h2&gt;
    &lt;?php if (isset($node-&gt;field_link['und'][0]['url'])) : ?&gt;
      &lt;?php print l($node-&gt;title, $node-&gt;field_link['und'][0]['url'], array(
        'attributes' =&gt; array(
          'title' -&gt; $node-&gt;field_link['und'][0]['title'],
          'class' =&gt; array('alert-link'),
        ),
      )); ?&gt;
    &lt;?php else : ?&gt;
      &lt;span class="alert-link"&gt;&lt;?php print $node-&gt;title; ?&gt;&lt;/span&gt;
    &lt;?php endif; ?&gt;
    &lt;a href="#" id="close-alert-bar"&gt;&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;


Does that all make sense? Do you see how the code the old dev team left you with got turned into this? If you have any questions, let me know and I'll try to explain better what I did.