12

In Drupal 7, there are hook_library() and drupal_add_library() that can be used to manage JavaScript and CSS libraries. I am looking for how to include the PHPExel library (now moved on GitHub) in my distribution.

How can I achieve this?

1
8

hook_library() is thought only for Javascript and CSS files; for PHP libraries, you should consider the Libraries API module.

This module introduces a common repository for libraries in sites/all/libraries resp. sites//libraries for contributed modules.

External libraries

Denotes libraries ("plugins") that are neither shipped nor packaged with a project on drupal.org. We do not want to host third-party libraries on drupal.org for a multitude of reasons, starting with licensing, proceeding to different release cycles, and not necessarily ending with fatal errors due to conflicts of having the same library installed in multiple versions. Drupal 7 only has built-in support for non-external libraries via hook_library(). But it is only suitable for drupal.org projects that bundle their own library; i.e., the module author is the creator and vendor of the library. Libraries API should be used for externally developed and distributed libraries. A simple example would be a third-party jQuery plugin.

The Libraries API module implements an API that can be used from third-party modules to load libraries installed on a Drupal site; the pro of using Library API is that it handles dependencies between libraries, and versioning.

The documentation explains which files can be loaded from a library defined using the Libraries API hooks. In particular, the documentation for hook_libraries_info() explains that, if $libraries is the value returned from the hook implementation, $libraries['files']['php'] is an array containing a list of PHP files that will be loaded when the library is loaded.
The Grammar Parser Library module defines a PHP library in gplib_libraries_info().

function gplib_libraries_info() {
  $libraries['grammar_parser'] = array(
    'title' => 'Grammar Parser Library',
    'vendor url' => 'http://drupal.org/project/grammar_parser',
    'download url' => 'http://drupal.org/project/grammar_parser',
    'version arguments' => array(
      'file' => 'CHANGELOG.txt',
      'pattern' => '/pgp \(grammar parser\) (\d+\.x-\d+\.x)/',
    ),
    'versions' => array(
      '7.x-1.x' => array(),
    ),
    'files' => array(
      'php' => array(
        'engine/parser.inc',
        'engine/editor.inc',
        'engine/list.inc',
        'engine/object.inc',
        'engine/reader.inc',
        'engine/writer.inc',
      ),
    ),
  );

  return $libraries;
}
2
  • Pretty sure Libraries module is only for JS libraries; not PHP.
    – liquidcms
    Jul 7 at 17:40
  • 1
    It's for PHP libraries too. The module documentation explains how to define a PHP library with hook_libraries_info().
    – apaderno
    Jul 7 at 19:07
1

I am not too sure if this operation is going to work for custom php libraries but I have used this procedure for importing custom css and js files into my project.

  1. Download and install the libraries module from here https://drupal.org/project/libraries and enable it (do not forget to download, install and enable all dependencies as needed)
  2. A directory sites/all/libraries should have been created. If not, just create the libraries directory in sites/all/
  3. now create your php includes director in the sites/all/libraries directory.
  4. Copy all your custom php files into the sites/all/libraries/includes directory.

In summary, I guess the libraries module should enable you to automatically import custom php, css and js libraries and so on into your project

0

Since now there is a package for the PHPExcel on packagist.org, the alternative to using the Libraries API module is creating a composer.json file describing all the dependencies the module has. Supposing the only dependency for the module is the PHPExcel library, the content of the composer.json file could simply be the following one. (I only show the dependencies part.)

{
    "require": {
       "phpoffice/phpexcel": "^1.8.2"
    }
}

I am using the release 1.8.2 because previous releases are affected by a XXE vulnerability reported in CVE-2018-19277.

Since the PHPExcel library is deprecated, the module should use the PHPSpreadsheet library.

{
    "require": {
       "phpoffice/phpspreadsheet": "^1.18"
    }
}

The latest version is 1.18.0, but any version higher than 1.15.0 is fine. Previous versions are affected by a XSS vulnerability described in CVE-2020-7776.

Once the module has its composer.json file, the Composer Manager module (required for this approach) will be able to save its dependencies in the directory defined in the Composer Manager settings.

In its documentation, the Libraries module suggests to use the Composer approach for Drupal 8.

For feature parity with the Drupal 7 version of this module, a PHP file library type is provided, that can load a list of PHP files on demand.
Generally, it is encouraged to use Composer instead of this library type and avoid Libraries API altogether for PHP libraries. See PhpFileLibraryInterface for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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