-1

I'm having trouble loading classes in drupal7. Let me start with a WORKING EXAMPLE that doesn't have problems.

If I create a file called sites/all/modules/johnmodule/test.php with the following contents

require_once(dirname(__FILE__) . "/dompdf-0.5.1/dompdf_config.inc.php");
$obj = new DOMPDF();

Then I go to the url http://mysite.com/sites/all/modules/johnmodule/test.php, then I get successful page load which is simply a blank page with nothing in it.

Now I tried to include these two lines of code into a drupal module, but it breaks everything. So what I did was I created a sites/all/modules/johnmodule/johnmodule.info and a sites/all/modules/johnmodule/johnmodule.module. For the contents of johnmodule.module, I have the following:

function johnmodule_billing_block_info()
{
    $blocks['johnmodule_billing'] = array('info' => t('Billing'), 'cache' => DRUPAL_CACHE_PER_ROLE);
    return $blocks;
}

/**
* Implements hook_menu()
*/
function johnmodule_billing_menu()
{
    $items = array();
    $items['billing'] = array(
    'title'         => 'My Billing History',
        'page arguments'    => array(1),
    'page callback'     => 'johnmodule_billing_view',
        'access callback'   => TRUE,
        'type'      => MENU_CALLBACK
    );


    return $items;
}

function johnmodule_billing_view()
{
    require_once(dirname(__FILE__) . "/dompdf-0.5.1/dompdf_config.inc.php");
    $obj = new DOMPDF();
}

Then when I go to http://mysite.com/billing/, I get a server error. When I looked in the apache error logs, I see:

PHP Fatal error:  Class 'DOMPDF' not found in /var/www/dev/mysite.com/public_html/sites/all/modules/johnmodule/johnmodule.module on line ZZZ, referer: http://mysite.com/billing

If I comment out the line $obj = new DOMPDF(), then the page loads fine.

WHy is Drupal not loading classes?

Additional Info

I tried to modify my johnmodule_billing_view() by using DOMPDF's __autoload() function to force drupal to load the classes that DOMPDF needs like so:

function johnmodule_billing_view()
{
    require_once(dirname(__FILE__) . "/dompdf-0.5.1/dompdf_config.inc.php");

    $arrClassFix = array();
    if ($handle = opendir(dirname(__FILE__).'/dompdf-0.5.1/include')) {
    while (false !== ($entry = readdir($handle)))
        if(preg_match('/\\.cls\\.php$/',$entry))
        $arrClassFix[] = preg_replace('/\\.cls\\.php$/','',$entry);
    closedir($handle);
    }
    foreach($arrClassFix as $cls){ __autoload($cls); echo $cls;}

    $obj = new DOMPDF();
}

This made sure drupal loaded DOMPDF class and all dependent classes, but now when the dompdf object (within itself) needs to call one of it's dependent classes, it can't seem to access the classes I already loaded, and i get the following error in the apache logs:

PHP Fatal error:  Class 'Abstract_Renderer' not found in /var/www/dev/mysite.com/public_html/sites/all/modules/johnmodule/dompdf-0.5.1/include/inline_renderer.cls.php on line 48, referer: http://mysite.com/billing

But I double checked the arrClassFix and can confirm that Abstract_Rendered was loaded in my foreachloop!

How do I get drupal these DOMPDF classes?

1 Answer 1

0

Ok, I solved my problem. Here's what I did

  • I moved the folder dompdf-0.5.1 to the /sites/all/libraries folder
  • I edited dompdf_config.inc.php by replacing the DOMPDF_autoload() function with:

Code:

function DOMPDF_autoload($class) {
  /* Add this checking - START */
  if (mb_strtolower($class)== 'firephp'){
    return;
  }
  /* Add this checking - END */

  $filename = mb_strtolower($class) . ".cls.php";
  require_once(DOMPDF_INC_DIR . "/$filename");
}
if ( !function_exists("__autoload") ) {
  /**
   * Default __autoload() function
   *
   * @param string $class
   */
  function __autoload($class) {
    DOMPDF_autoload($class);
  }
}
  • Then I updated the johnmodule_billing_view() function like so

Code:

function johnmodule_billing_view()
{

    require_once(realpath('.')."/sites/all/libraries/dompdf-0.5.1/dompdf_config.inc.php");
    spl_autoload_register('DOMPDF_autoload'); 

    $obj = new DOMPDF();
}

This worked and I was able to use the DOMPDF object/class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.