7

I would like to use a 3rd party PHP library in my module and I wanted to ask what I need to do to autoload the classes?

Example: Drupal\my_module\MyClass wants to instantiate ThirdPartyNamespace\Something\Else\Class1

The official Drupal 8 documentation doesn't really deal with more complex namespacing scenarios.

  • 1
  • That other question is a year old, a lot can change in a year in Drupal 8, so not sure where to answer. Composer manager is one way, you can also avoid a hardcoded dependency by shipping with your own autoloader like github.com/amateescu/search_api_solr. Or you could even go full composer and build Drupal and the modules with composer, kind of like drush make. I can't find a good resource right now, but people are working on making that happen. The advantage is that it picks up their composer.json files automatically and combines everything. – Berdir Jan 5 '15 at 20:49
  • @Berdir or @Clive can you tell me where are we with this in 8.1. It says here that it supposed to support it in my custom modules but I can't find a way to make this work. I put the require statement in my composer.json file in my module folder, then composer install but Drupal still says that he can't find my lib. – Guillaume Bois Jun 3 '16 at 19:24
  • You need to install your module with composer too so that it knows about your dependencies. See drupal.org/node/2404989. If it is a custom module, you need to add them to the main composer.json yourself. – Berdir Jun 3 '16 at 20:27
  • @berdir if I am not mistaken, composer doesn't install a module in drupal, it simply downloads the libs. I can't see why I need to add my needed libs in the module's composer.json AND the one from the root. And if I need to put it in the root composer.json then I am modifying the Drupal's git and I need to manage another branch of the Drupal's git just for that file. Drupal should be able to include all libs from all composer.json not only the one from the root. – Guillaume Bois Jun 6 '16 at 12:47
4

Drupal use composer to manage the 3rd party libraries (Symfony, guzzle, etc...). The problem is Drupal has versioned the vendor folder into the core directory; it is a problem because, if you added a new library in the root composer, you change the vendor directory and change the composer.json/composer.lock. So, if Drupal releases a new version, your changes will be deleted.

The Composer Manager module is created to solve the problem. You need to include a composer.json file in your module; the Composer Manager module downloads the library and registers a new autoload file (the second autoload).
The problem with this is only when you need use a middle-ware library, since the Composer Manager module registers a new autoload in the Request Kernel Event. This event is called after the middle-ware so this library is not available in the middle-ware layer.

In most cases, the Composer Manager module is the best solution to add new vendors.

There is the issue Remove external dependencies from the core repo and let Composer manage the dependencies instead.

  • Please note as of drupal 8.1 composer manager is no longer needed. The reccomended way is to modify the composer.json that ships with drupal in drupal root. Checkout github.com/drupal-composer/drupal-scaffold. – pwaterz Dec 21 '16 at 20:08
  • It is not recommended to initiate compose within a module directory. It is better to change the root composer file to include the dependency. And since recent it is even possible to require Drupal modules in the root composer file, which will install all dependencies of these module as well. – Neograph734 Jan 4 '17 at 13:56
2

I edited the composer.json in the root of my Drupal project and in the module that relies on the library. Then I loaded the main class using a fixed path:

require_once (\Drupal::root() . '/vendor/campaignmonitor/createsend-php/csrest_general.php');

Then I noticed having an issue with instantiating an object of the class:

$wrap = new CS_REST_General($auth);

So apparently you need to add a '\' eg:

$wrap = new \CS_REST_General($auth);
  • 1
    I think you only needed to do this because campaignmonitor/createsend-php does not abide by the PSR-0 or PSR-4 standards because it's so old it was written before those standards were adopted, and thus you cannot use the autoloader. This answer is fairly specific to this library, but you should explain in your answer more about why you needed to added `` (because of namespaces) and why it may be necessary for libraries that are this old. – mradcliffe Aug 9 '16 at 13:35
0

Seems that the current answers are all outdated. When you use Composer to manage your project's dependencies, preferably with drupal-composer/drupal-project, all you need to do is to require a package in your project-root's composer.json and then load it's classes wherever you need them.

$ cd MYPROJECT
$ composer require ramsey/uuid

Then in your custom module just use it.

use Ramsey\Uuid\Uuid;

To let your custom module provide its own composer.json you can add wikimedia/composer-merge-plugin to your project-root's composer.json and in the "extra": {} section configure it to include other composer.json located at a certain directory pattern.

"merge-plugin": {
    "include": [
        "web/modules/custom/*/composer.json"
    ]
},

Everything else is taken care of by Drupal.

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.