0

I have a PHP class and it's files in the folder:

/var/www/include_php/**the_folder**

How should I include this into Drupal?

ln -sf /var/www/include_php/the_folder /var/www/drupalSite/sites/all/libraries/the_folder

and then do drupal_add_library('the_folder') or something else in my block module?

2

If your PHP Library is a class or set of classes, you can add them to the registry. This is what I am doing in the Mobile Detect module. I got the idea from another module, but I don't recall which one:

/**
 * Implements hook_registry_files_alter().
 * 
 * Adds the Mobile_Detect() class to the registry so that autoloading works.
 * See http://drupal.stackexchange.com/questions/42266 for a discussion.
 */
function mobile_detect_registry_files_alter(&$files, $modules) {
  $library_path = _mobile_detect_get_library_path();

  $files[$library_path . '/Mobile_Detect.php'] = array(
    'module' => 'mobile_detect',
    'weight' => 0,
  );
}

This adds the class to registry so it will be autoloaded if instantiated as $detect = new Mobile_Detect();. You can edit the $library_path = ... line to suit your situation.

2

drupal_add_library() is for Javascript/CSS libraries; Drupal doesn't have a similar registry for PHP libraries/classes.

To include a file inside a module in your installation, use module_load_include() e.g.

module_load_include('inc', 'mymodule', 'mymodule.pages');

For all others just defer to the standard PHP functions (include/require, etc.)

0

In your module symlink library's folder

ln -sf /var/www/include_php/the_folder /var/www/drupalSite/sites/all/mymodule/the_folder

Then in you mymodule.info add series of lines:

files[] = the_folder/first.inc
files[] = the_folder/second.inc

and so on. That way your module will know about them and Drupal will be able to autoload them.

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.