Skip to main content

Using Composer in Drupal Project

While developing the Drupal project with Composer, you can install modules and themes with Composer commands, you can also update these modules and themes to the new version with composer commands.
Taşkın Maksim
Taşkın Maksim
10 min. read
drupal-de-composer-kullanimi

In this article, we will examine the following topic:

  • Use of frequently used Composer commands to be applied step by step for Drupal project
  • Installation and logging of Drupal project (core, module, theme, profile, etc.) with Composer
  • Convert an existing application into a Composer-aware application

By the end of this article, you should know how to install and update Drupal modules and themes with Composer.

Target

Creating a Composer-based Drupal project and Drupal module and theme installation.

Prerequisites

  • The latest version of Composer.. 

    composer self-update 
  • You can update Composer with the command.
  • Create the composer.json file for the current Drupal Project.
  • Run all commands for the current Drupal Project in the main path of the project.

Creating a New Drupal Project

The easiest way to create a Drupal project with Composer is to use a template. We will use Composer's create-project command for this. When you run composer create-project some/project, this command creates your new project by cloning the some/project template.

Your project will be based on some/project. For example, the composer.json file in some/project will be the start of your project. After that, some/project will not have any effect on your project.

Drupal.org You can find some popular templates for creating a Drupal project with Composer on

NOTdrupal/drupal project has been discontinued and is not recommended.

In this article we will use the most recommended and widely used drupal-composer/drupal-project project. This template provides enough configuration to develop a Drupal project with Composer.

drupal-composer/drupal-project Let's run the following command to develop a Drupal project using the template:

$ composer create-project drupal-composer/drupal-project:8.x-dev my-project --stability dev --no-interaction
$ cd my-project

For more information drupal-composer/drupal-project documentation of the project.

Let's start using Composer to manage an existing Drupal application

Starting to manage a Drupal project with Composer that is not managed with Composer can be a bit difficult at first. The exact method of managing a Drupal project with Composer depends on the folder structure of the Drupal application and may require some additional steps.

There are a number of tools that automate the process of converting a Drupal project to Composer:

If you fail with the above tools, you can do it with the following steps.

  • Switch to the folder with the .git folder with the terminal. It is extremely important that you execute commands from the correct directory. For some projects, the folder may contain the Drupal project directly. For other projects, the Drupal project may be under docroot/core and web/core.
  • Delete the composer.json file and the vendor folder. There should be only one composer.json file and vendor folder in your project. If there is an old or unused composer.json or vendor folder, delete it from the project. 
$ cd drupal/project/site
$ find . -name "composer.json" -exec rm -rf {} \;
$ find . -name "composer.lock" -exec rm -rf {} \;
$ find . -name "vendor" -exec rm -rf {} \;

Create a new composer.json file using the following template:

{
 "name": "drupalprojem",
 "license": "proprietary",
 "type": "project",
 "repositories": {
   "drupal": {
     "type": "composer",
     "url": "https://packages.drupal.org/8"
   },
   "asset-packagist": {
     "type": "composer",
     "url": "https://asset-packagist.org"
   }
 },
 "require": {
   "composer/installers": "^1.2.0",
   "cweagans/composer-patches": "^1.6.4",
   "drupal/composerize": "1.x-dev",
   "drupal-composer/drupal-scaffold": "^2.4",
   "oomphinc/composer-installers-extender": "^1.1",
   "wikimedia/composer-merge-plugin": "^1.4.1"
 },
 "require-dev": {
   "drupal/console": "^1.0.2",
   "drush/drush": "^9.0.0"
 },
 "extra": {
   "enable-patching": true,
   "installer-types": ["bower-asset", "npm-asset"],
   "installer-paths": {
     "docroot/core": ["type:drupal-core"],
     "docroot/modules/contrib/{$name}": ["type:drupal-module"],
     "docroot/modules/custom/{$name}": ["type:drupal-custom-module"],
     "docroot/profiles/contrib/{$name}": ["type:drupal-profile"],
     "docroot/profiles/custom/{$name}": ["type:drupal-custom-profile"],
     "docroot/themes/contrib/{$name}": ["type:drupal-theme"],
     "docroot/themes/custom/{$name}": ["type:drupal-custom-theme"],
     "docroot/libraries/{$name}": ["type:drupal-library", "type:bower-asset", "type:npm-asset"],
     "drush/contrib/{$name}": ["type:drupal-drush"],
     "special/package/dir/": ["my/package"]
   },
   "merge-plugin": {
     "include": [
       "docroot/modules/custom/*/composer.json"
     ],
     "replace": false,
     "ignore-duplicates": true
   },
   "patches": {}
 },
 "minimum-stability": "dev",
 "prefer-stable": true
}
  • The example above assumes that Drupal resides in a docroot subdirectory. To correctly match your directory structure, replace all strings containing "docroot".
  • Replace "name" with the name of your project and add Drupal Core to your project with the commandcomposer require drupal/core 8.6.0.

    You now have a functional composer.json file, which conveniently requires Drupal Core and the various plugins needed to manage a Drupal site with Composer.

Now you can install modules, themes, profiles and so on with Composer. This is a manual process that requires your review.

To add other modules to the project composer require drupal/[module-name]. This installs the new stable version or upgrades the version if it is already installed. If you want to specify the version, you can do this with the composer require drupal/[module-name] [version] command.

Run this command for all contrib modules of Drupal. You can also run it to install other modules. You can list all modules installed with drush. Drush must be installed in your local area. Drush queries the Drupal database and lists the installed modules:

 $ ./vendor/bin/drush pml --no-core --status=enabled
---------------- --------------------------------------- --------- ----------------
 Package          Name                                    Status       Version
---------------- --------------------------------------- --------- ----------------
 Administration   Admin Toolbar (admin_toolbar)           Enabled      8.x-1.27
---------------- --------------------------------------- --------- ----------------

For best standard work, we delete contrib modules from version control (git).

Add the following lines to a new .gitignore or existing .gitignore file:

docroot/core
docroot/modules/contrib
docroot/themes/contrib
docroot/profiles/contrib
docroot/libraries

After adding, delete the following files from version control:

git rm --cached docroot/core
git rm --cached docroot/modules/contrib
git rm --cached docroot/themes/contrib
git rm --cached docroot/profiles/contrib
git rm --cached docroot/libraries
git rm --cached vendor
 

When you run these commands fatal: pathspec 'vendor' did not match any files error because these files are not in the folder. This is normal.

Thus, we have changed the Drupal project that was not created with Composer to Composer structure.

Creating a New Drupal Project with Composer

To install the Drupal package, first run the following command using the composer.json template above. At least the composer.json file should have repositories entry and installer-paths configuration.

$ composer require drupal/[proje]

For example to install the pathauto module:

$ composer require drupal/pathauto

By default this installs the latest version, to install the specific version:

$ composer require drupal/pathauto 1.0.0

This command may give an error if the dependencies of other modules do not match, in this case you can install it by changing the version until it goes to sleep or you can install the latest version. Also in Composer language "install" means to install the code, update the composer.lock file and make the code available with autoloader. You still need to install the Drupal project from the interface or with Drush to the Drupal database.

Updating Dependent Modules

To update any module:

composer update [vendor]/[package]

For example to update drupal/pathauto:

$ composer update drupal/pathauto

This only updates the drupal/pathauto module, even though it is a dependent module of this module.

To update the module with its dependent modules:

$ composer update drupal/pathauto --with-all-dependencies

To update by specifying the minimum version:

$ composer require drupal/pathauto:^1.1.0 --update-with-all-dependencies

To update all modules:

$ composer update

As with updating any Drupal project, you should perform database updates after downloading the new module. If you are using Drupal Configuration Management, you should re-export the configurations after database updates.

Update Drupal Core with Composer

Drupal Core can be updated with Composer like any other module. As in the steps above, you can update drupal/core by specifying drupal/core instead of the module name and running the commands. It is a good idea to set a new minimum version for drupal/core so that a downgrade is never performed accidentally.

As with updating any Drupal project, you should perform database updates after downloading the new module.

Loading dependencies of a module with Composer

Let's say there is a module that we have not installed with Composer. How do we install the dependencies listed in the composer.json file of this module? If you are asking this question, you are actually on the wrong track. You must first use Composer to manage the entire Drupal project and install the module with Composer. Then, its dependencies will be automatically installed for you.

As an alternative to using Composer, if the module supports Ludwig, you can use Ludwig to install the module's dependencies.

Our Offices

Drupart Locations

Our Officess

London

151 West Green Road, London, England

442038156478

[email protected]

Drupart R&D

GOSB Teknopark Hi-Tech Bina 3.Kat B3 Gebze - KOCAELİ

+90 262 678 8872

[email protected]

Newark

112 Capitol Trail Suite, A437 Newark DE, 19711

+17406666255

[email protected]

Wiesbaden

Hinterbergstraße 27
65207 Wiesbaden
Deutschland

+49 (0) 6151 – 492 70 23

[email protected]