Suppose i want to load facebook-php-sdk for facebook connect in my drupal website, how can i load that class file in my module, i tried with libraries module, with different function provided with that api, but everytimes i fail.


You can do the same way in Drupal for Facebook module and put these code in your custom module:

 $fb_lib_path = function_exists('libraries_get_path') ? libraries_get_path('facebook-php-sdk') : 'sites/all/libraries/facebook-php-sdk';
 $fb_platform = $fb_lib_path . '/src/facebook.php';
 try {
     if (!class_exists('Facebook') && !@include($fb_platform)) {
      //show error
      return NULL;
     $fb = new Facebook(array(
       'appId' => $appId,
       'secret' => $secret,
       'cookie' => $cookie));
 catch (Exception $e) {}

You can check the fb_api_init function of Drupal for Facebook module for more detail.

| improve this answer | |
  • I used same approach for a different library in drupal. In my case i need to use API class defined in /src/Api.php file. For me !(class_exists('Api')) is coming 1 i.e class is showing non-existed, though it exists. Please help. – neha Feb 11 '17 at 8:31

Libraries API is a module designed to take the burden of this from your shoulders. You probably have it already installed, as it is required by many other modules.

The common denominator for all Drupal modules/profiles/themes that integrate with external libraries.

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

When you put library where it needs to be, simply use:


You can also attach library to renderable array like that:

$form['myelement']['#attached']['libraries_load'][] = array('libraryName');

More detailed documentation here: Using Libraries API 2.x (as a module-developer). That's the source of code snippets I pasted here .

| improve this answer | |
  • sites//libraries doesn't look like a valid path – mbomb007 Jan 25 '19 at 15:51

You can also include custom libraries by loading and saving it in a directory such as sites/all/libraries/ and loading it through your template .inc file

| improve this answer | |

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.