[SOLVED] How to Include Third Party Libraries in Drupal 8 for Custom Modules?
In Drupal 8, third party libraries are downloaded and managed via composer. In Drupal each project must contain only one vendor folder. Composer Manager allows single vendor/directory shared across all modules which prevents code duplication and version mismatches by merging the requirements of all the found modules into the consolidated composer.json file.
In custom module, set dependency for composer manager module in .info file.
dependencies:
- composer_manager
Write composer.json file for your custom module to download php library dependency. Your composer.json file should contain at least name and requirement (your php library).
{
"name": "drupal/mymodule",
"require": {
"endroid/qrcode": "^1.6"
}
}
Following are the steps to work custom modules with composer.json :
1. First of all install Composer Manager module.
2. In your Drupal root directory, run the module's init.php script on the command line. For example,
username@locahost:/var/www/SITENAME$ php modules/contrib/composer_manager/scripts/init.php
3. In command line, you will see the message after step 2 as 'Composer Manager has been successfully initialized', once initialized properly. Now run composer drupal-update
from the root of your Drupal site directory. For example,
username@locahost:/var/www/SITENAME$ composer drupal-update
composer update
After the above three steps, the php library is downloaded to the vendor folder in Drupal root directory. Now you can see the composer manager module status in /admin/reports/composer. If you want to download the dependency library in a specified folder path, then set path in your composer.json file.
"config": {
"vendor-dir": "includes"
}
Hope this helps. Also, please feel free to get in touch with us for any queries.