CiviCRM hook_page for drupal

While I'm not overly competent with CiviCRM I recently needed to work on the content of a page from within a drupal module. The hook system seems to be in active development but as of v2.1.2 it hasn't got to the point of exposing the template to the hook api. I added a few lines of code to enable this. I also files a feature request for version 2.2 so hopefully it'll be added for that.

During the meanwhilst here's my effort:

edit CRM/Core/Page.php

first add

<?php
require_once 'CRM/Utils/Hook.php';
?>
to the top of the doc.
next alter the run() method.

<?php
function run( )
    {
        if (
$this->_embedded ) {
            return;
        }

       
self::$_template->assign( 'mode'   , $this->_mode );
       
self::$_template->assign( 'tplFile', $this->getTemplateFileName() );

        if (
$this->_print ) {
            if (
$this->_print == CRM_Core_Smarty::PRINT_SNIPPET ||
                
$this->_print == CRM_Core_Smarty::PRINT_PDF ) {
               
$content = self::$_template->fetch( 'CRM/common/snippet.tpl' );
            } else {
               
$content = self::$_template->fetch( 'CRM/common/print.tpl' );
            }
            if (
$this->_print == CRM_Core_Smarty::PRINT_PDF ) {
                require_once
'CRM/Utils/PDF/Utils.php';
               
CRM_Utils_PDF_Utils::domlib( $content, "{$this->_name}.pdf" );
            } else {
                echo
$content;
            }
            exit( );
        }
       
$config =& CRM_Core_Config::singleton();
       
       
// Adding hook to drupal
       
CRM_Utils_Hook::pageProcess(self::$_template);
       
       
$content = self::$_template->fetch( 'CRM/common/'. strtolower($config->userFramework) .'.tpl' );
        echo
CRM_Utils_System::theme( 'page', $content, true, $this->_print );
        return;
    }
?>

Now we add a corresponding hook function in CRM/Utils/Hook.php
Add this before the end of the class.

<?php
static function pageProcess( &$page ) {
       
$config =& CRM_Core_Config::singleton( );
        require_once(
str_replace( '_', DIRECTORY_SEPARATOR, $config->userHookClass ) . '.php' );
       
$null =& CRM_Core_DAO::$_nullObject;
        return  
            eval(
'return ' .
                 
$config->userHookClass .
                 
'::invoke( 1, $page, $null, $null, $null, $null, \'civicrm_page\' );' );
    }
?>

And now in your module you can write your hook.

<?php
function MODULE_NAME_civicrm_page(&$page) {
   
$someVar = "Hello World";
   
$page->assign("someVar", $someValue);
}
?>

The only thing remaining is to add this to the template which will look something like {$someVar}

IMPORTANT:
At this point we are not checking what page we are using. I suggest looking at the smartyDebug=1 info to find out what makes your page unique and check for it OR just use the arg() from drupal to get your url. This method will add the var to all pages of your site although it will not be displayed unless it is added to you template.