6

I have a third party php library which I would like to use in a module. How do I do that in Drupal 8? In drupal 7, I have done things like the following code, but libraries_get_path() doesn't exist in Drupal 8, and I'm not exactly sure how things change, not that we use namespaces (of which I am a beginner).

require(libraries_get_path('twitter-api-php') . '/TwitterAPIExchange.php');

And, where should I place my library? Should I create a new libraries directory and put it in the root, just like modules and themes are now in the root?

1
  • 1
    libraries_get_path also doesn't exist in Drupal 7 - it comes from the contrib Libraries module </nitpick>
    – Clive
    Aug 19, 2014 at 17:02

4 Answers 4

4

The 8.x-3.x branch of the Libraries module still provides the libraries_get_path() function. And (as of now) it looks pretty much unchanged. So you should be able to use it in the same way.

As far as where to put your library, the libraries_get_libraries() function has been updated with the following addition to the $searchdir array:

// Always search the root 'libraries' directory.
$searchdir[] = 'libraries';

Which indicates that you can place your libraries in a directory named "libraries" at your site root.

2
  • Ok, I was able to use libraries_get_path and it returned the correct path. But when I tried to require it, I got this error: Fatal error: Class 'Drupal\twitter_pull\Plugin\Block\TwitterAPIExchange'. My code is: require(libraries_get_path('twitter-api-php') . '/' . 'TwitterAPIExchange.php'); Whats wrong? Sep 5, 2014 at 13:17
  • @user1015214 Did you get solution for your error? As i am getting similar error on using require.
    – neha
    Feb 13, 2017 at 12:19
3

Composer:

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

plus the Composer Manager module:

Composer Manager provides a gateway to the larger PHP community by enabling Drupal modules to more easily use best-in-breed libraries that are managed by Composer.

can handle this.

0
1

If you use the libraries module and you put your library files in /libraries/library (which is where mine end up using drush make) you can simply call library_load('library') and that will do the require_once for you.

1
  • Library_load() function does not exists in drupal 8 even after installing libraries module. Error is : PHP Fatal error: Call to undefined function .
    – neha
    Feb 13, 2017 at 9:12
0

Using Composer Manager is good solution but it need access to command line to init Also you can use module_load_include like we use it in Drupal7. Put your Php file in you module directory and use

  module_load_include('php', 'modulename','filename');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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