2

I'm trying to create a custom Twitter Pull module in Drupal 8. I would like to include https://github.com/abraham/twitteroauth.

I can't seem to get it working adding it via composer in the root composer.json file, or by using Composer Manager and creating a composer.json file in my module root, so for the sake of this question, how else can I include and make use of the library?

Can I add a dependency to my custom module .yml file?
How would I handle the namespacing/autoloading for this library in my custom module?

  • There is no other good way of including dependencies other than using composer manager (or the root composer.json manually by hacking core). Loading your own composer / vendor library is a recipe for disaster as you will probably soon double the size of your code base. I would focus on providing details or error messages when you use composer manager. – mradcliffe Jan 29 '16 at 16:54
  • With the proper composer.json in a module, this works fine: see gist.github.com/mradcliffe/a4ff6219c39b1a26d472 – mradcliffe Jan 29 '16 at 17:23
  • I appreciate your help. Going down the composer route (in either case, hacking root composer.json, or using Composer Manager, I can get library to download to the vendor directory, but I keep getting the class not found error with my "use Abraham\TwitterOAuth\TwitterOauth"... I was intrigued by the idea of possibly an alternate method, because I thought maybe I could keep that library in my custom module's folder, and maybe I could figure out the namespacing bit easier – Greg Jan 29 '16 at 17:56
  • Hmm, I'm still not able to reproduce an issue. I made a test module with a phpunit test that tests if oauth object is instance of Abraham\TwitterOAuth\TwitterOAuth: github.com/mradcliffe/twitteroauth. – mradcliffe Jan 29 '16 at 20:46
  • I'm not user if this is answer worthy because it would be really long and pretty specific. I think that there are some other questions/answers that go over Composer Manager specifically. – mradcliffe Jan 29 '16 at 20:52
1

General: Composer Merge Plugin

In your Drupal's main composer.json you can use the Composer Merge Plugin to scan certain directories for composer.json files and merge them into your main composer.json.

You'd then have a composer.json in your custom module requiring the 3rd party library. In your root composer.json you require the merge plugin and configure it under the extra section to include certain directories.

{
    "require": {
        "wikimedia/composer-merge-plugin": "dev-master"
    },
    "extra": {
        "merge-plugin": {
            "include": [
                "web/modules/custom/*/composer.json"
            ]
        }
    }
}

Optional: Composer Custom Type Installer

If the 3rd party library in your custom module is available on GitHub and you want to define a location where the lib should to be downloaded to (extra helpful especially for all front-end libs) you can use Composer Custom Type Installer in your main composer.json.

You'd then require the 3rd party library in the repositories section in your custom module's composer.json assigning it a package type you pre-configured in the extra section of your main composer.json to define a location where the lib should be downloaded to.

"repositories": [
    {
        "type": "package",
        "package": {
            "name": "select2",
            "version": "4.0.3",
            "type": "theme-library",
            "source": {
                "url": "https://github.com/select2/select2.git",
                "type": "git",
                "reference": "tags/4.0.3"
            }
        }
    }
]

And the corresponding extra section in the main composer.json:

"extra": {
    "custom-installer": {
        "theme-library": "web/themes/custom/MYTHEME/libs/{$name}"
    }
}

Of course, for simplicity you could also do everything (composer-merge-plugin and custom-installer and requiring the GitHub repo) just from you main composer.json. I'd recommend that.

If you do so, then run at least once composer require PACKAGENAME (in my example composer require select2) after you manually incorporated the lib into the composer.json file.


Last but not least, don't forget to bind your library with MYMODULE.libraries.yml and hook_page_attachments (latter called from MYMODULE.module). See Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module.

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.