Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
1  
libraries_get_path also doesn't exist in Drupal 7 - it comes from the contrib Libraries module </nitpick> – Clive Aug 19 '14 at 17:02
up vote 4 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 '14 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 '14 at 12:46

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.

share|improve this answer

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 '14 at 18:53
    
This is not for PHP libraries; it is for JavaScript and CSS. – kiamlaluno May 3 at 21:53
    
Yes, for external php libraries (containing composer.json) one can use the Composer Manager module. – Aneek Mukhopadhyay May 8 at 5:43

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');
share|improve this answer

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.