Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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:

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

but libraries_get_path doesn't exist in D8, and I'm not exactly sure how things change, not that we use namespaces (of which I am a beginner). 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?

share|improve this question
    
libraries_get_path also doesn't exist in Drupal 7 - it comes from the contrib Libraries module </nitpick> –  Clive Aug 19 at 17:02

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
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? –  user1015214 Sep 5 at 13:17

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.

share|improve this answer
    
I will take a look at this. Thanks. –  user1015214 Aug 20 at 12:46

As clive said, libraries_get_path is not a function that is shipped with Drupal core. Just for your information as of the change log #2169605, any libraries that you need to add to a page is by following,

$example = array(
  '#attached' => array(
    'library' => array(
      'example/drag-and-drop',
    ),
  ),
);

Also have a look at the Replace hook_library_info() by *.libraries.yml file link.

share|improve this answer
    
I actually saw that change log, but it seemed that it was only talking about adding a library to a form. I am not creating a form, just want to import the library –  user1015214 Aug 19 at 18:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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