Using Zend Framework In Drupal

14th August 2012 - 4 minutes read time

If you want to use Zend Framework in Drupal then most of the time you can use the Zend module. This takes a little configuration but will include the framework and instantiate the Zend_Loader_Autoloader class so that everything is ready to run.

The Zend module has a number of different strategies to including the framework, which is handy if you do or don't want to use the Libraries module. The module uses the hook_init() hook to include and instantiate the Zend_Loader_Autoloader object, which meant that this was done on every page load; even if the framework isn't being used.

For my particular use-case this wasn't acceptable as I was only using a single component of Zend Framework to call a third party API when needed. I therefore needed an alternative method, which lead me to create a function that would autoload the framework autoloader on demand. I chose to include Zend Framework inside the module directory so that the module I was working on was self-contained. This can easily be changed to be another directory if needed.

  1. function mymodule_autoload_zend() {
  2. $zend_path = realpath(drupal_get_path('module', 'mymodule'));
  3. set_include_path(get_include_path() . PATH_SEPARATOR . $zend_path);
  5. require_once 'Zend/Loader/Autoloader.php';
  6. Zend_Loader_Autoloader::getInstance();
  7. }

This meant that when I needed to run some Zend Framework specific code I could just call this function and continue as normal. Here is an example using the LiveDocx API to create a PDF document based on a given template file.

  1. function mymodule_generate_pdf($filename, $arguments, $template) {
  2. // Include Zend
  3. mymodule_autoload_zend();
  5. // Get template Location
  6. $module_dir = drupal_get_path('module', 'mymodule');
  7. $template_file = realpath($module_dir . '/templates/' . $template);
  9. if (!file_exists($template_file)) {
  10. drupal_set_message('Template file "' . $template . '" not found in templates folder.', 'error');
  11. return FALSE;
  12. }
  14. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  16. // Login to LiveDocx
  17. $phpLiveDocx->setUsername('username')->setPassword('password');
  19. // Set template
  20. $phpLiveDocx->setLocalTemplate($template_file);
  22. // Assign data to template
  23. foreach ($arguments as $key => $value) {
  24. $phpLiveDocx->assign($key, $value);
  25. }
  27. $phpLiveDocx->createDocument();
  28. $document = $phpLiveDocx->retrieveDocument('pdf');
  30. // Save new document to file
  31. $document_location = realpath(file_directory_path()) . '/pdfs';
  32. file_put_contents($document_location . '/' . $filename . '.pdf', $document);
  33. return $document_location . '/' . $filename . '.pdf';
  34. }

More could be done to this script to improve things like filename clashing, but it is only intended to provide a simple example. You can run it in the following way, assuming you have already set up a template.

mymodule_generate_pdf('the_file', array('key' => 'value'), 'template.docx');

Zend Framework has lots of different components that can be used as drop in classes in the same way as LiveDocx. Using just a few lines of code means that you can pull in any class you need and start using it straight away. Whether you use the Zend module or the mymodule_autoload_zend() method depends on your requirements but they both use essentially the same code and can therefore be swapped if needed.

This article is specifically written with Drupal 7 in mind, but the principles should apply to Drupal 6 and 8 as well.

Add new comment

The content of this field is kept private and will not be shown publicly.