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.
function mymodule_autoload_zend() {
$zend_path = realpath(drupal_get_path('module', 'mymodule'));
set_include_path(get_include_path() . PATH_SEPARATOR . $zend_path);
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
}
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.
function mymodule_generate_pdf($filename, $arguments, $template) {
// Include Zend
mymodule_autoload_zend();
// Get template Location
$module_dir = drupal_get_path('module', 'mymodule');
$template_file = realpath($module_dir . '/templates/' . $template);
if (!file_exists($template_file)) {
drupal_set_message('Template file "' . $template . '" not found in templates folder.', 'error');
return FALSE;
}
$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
// Login to LiveDocx
$phpLiveDocx->setUsername('username')->setPassword('password');
// Set template
$phpLiveDocx->setLocalTemplate($template_file);
// Assign data to template
foreach ($arguments as $key => $value) {
$phpLiveDocx->assign($key, $value);
}
$phpLiveDocx->createDocument();
$document = $phpLiveDocx->retrieveDocument('pdf');
// Save new document to file
$document_location = realpath(file_directory_path()) . '/pdfs';
file_put_contents($document_location . '/' . $filename . '.pdf', $document);
return $document_location . '/' . $filename . '.pdf';
}
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