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.

up vote 6 down vote accepted

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.

  • 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:

libraries_load($name);

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 .

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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