Managing dependencies with Composer Manager


  • Drupal 8
  • Composer

Use Case:

  • You want to add dependencies to your custom module using composer update at the root of your site directory.
  • Updating Drupal core removes any custom dependencies added to the root composer.json, potentially breaking any modules that require them.

Quick run-down:

First, download composer manager to your site’s modules directory and run the init.php command to register the ‘drupal-update’ command. This command is what will look through your module’s composer.json files and update the main composer.json file at your site’s docroot.

cd mysiteroot
drush dl composer_manager
php modules/composer_manager/scripts/init.php
composer drupal-update
In your sites directory, add a composer.json file and make sure you have both the name and requirements:
  "name": "drupal/mymodule",
  "require": {
    "mailchimp/mailchimp": "2.0.6"

Now, run composer drupal-update again and you should see your dependencies in the vendor directory.

See also:

Drupal 8 Console: Building a Twitter feed module

I took this tutorial, Tutorial for Converting a Module from Drupal 7 to Drupal 8, and worked my way through it but used Drupal Console for many of the scaffolding, which makes things a lot easier. So thanks to Unleashed Technologies for the bulk of the work.

In your Drupal 8 root, generate a custom module:

drupal generate:module

This will prompt you for details about the module and build an info and module file.

Our first step will be to create a settings page that will have a form for our Twitter API Oauth credentials. You’ll need to create an application in your twitter developer console. Create a new app. If you want to use this for local testing, use ‘‘ for the URL.

Now, we’ll create a new form:

drupal generate:form:config

This will build out your form class, your input fields, and update your router file. Your settings page is built, however, we also need to build a menu link for it to live, and we do this in a YAML file!

Add {yourmodule} to the root of your module directory.

  title: 'Twitter settings'
  description: 'Twitter settings for your site'
  parent: system.admin_config_services
  route_name: twitter_pull.twitter_settings_form
  weight: 100

Now if you navigate to /admin/config/, you’ll see your new Twitter Settings link under the Web Services menu.

From here, you have options: Where do you want your Twitter feed to display. For this example, I’m building a block.

drupal generate:plugin:block --module=twitter_pull

Add the block class name, and any input fields you may wish to include and generate the plugin.

Now you have the luxury of OOP at your disclosure to pull in your Twitter feed and spit it out to a Twig template for rendering.

Loading your configuration is as easy as:

$config = \Drupal::config('twitter_pull.twittersettings_config');

Just pull in your form id into Drupal’s config method and you have access to your settings. At the top of our block file, we’ll autoload the Twitter OAuth PHP library, but first we have to install that library.

One option here is to add our own composer.json file to our module, declare a dependency for a Twitter OAuth library, and then use Composer Manager to compile our dependencies to our composer.json at root. I’ll save this for another post, but for now, I’ll simply include the Twitter OAuth library at our root composer.json manually.

"require": {
  "composer/installers": "^1.0.21",
  "wikimedia/composer-merge-plugin": "^1.3.0",
  "abraham/twitteroauth": "^0.6.1"

Be sure to run composer update at your docroot after making this edit. Also note that if you update Drupal, it will override this file and your dependency declaration will be lost, hence the need for Composer Manager. Then we can autoload the library in our block file.

You can download my example here. OOP is your oyster.