7

I am new to using Composer.

How can I install a theme, say https://www.drupal.org/project/bootstrap using composer

I've tried:

composer require drupal/bootstrap

But didn't work.

I also tried:

composer require twbs/bootstrap

Installation was completed successfully but I couldn't see the theme appearing at Drupal admin page.

What is the standard way to extract the name of the theme or module to be used with composer require?

2 Answers 2

8

That is because every Drupal package is at drupal/ - twbs is not in the Drupal packagist. You have to add that to composer to get that to work as Clive said.

It is not in the composer.json file out of the box.

You need to add that, and then do:

composer remove twbs/bootstrap because this is NOT a Drupal ready theme.

After adding the repository to composer.json, then you can do:

composer require drupal/bootstrap

Also, not every module or theme has a composer.json file yet. Bootstrap for Drupal does, and thats where you can determine what the package name is if you are unsure.

http://cgit.drupalcode.org/bootstrap/tree/composer.json

I believe modules/themes have to provide this file in order to work with composer (including the repository addition in Clives answer).

Here is one of my projects composer.json, for example. Note the addition of the repositories and Drupal packages URL.

{
    "name": "drupal/drupal",
    "description": "Drupal is an open source content management platform powering millions of websites and applications.",
    "type": "project",
    "license": "GPL-2.0+",
    "require": {
        "composer/installers": "^1.0.21",
        "wikimedia/composer-merge-plugin": "~1.3",
        "drupal/search_api_solr": "1.0.0-beta1",
        "drupal/search_api": "^1.0@beta"
    },
    "replace": {
        "drupal/core": "~8.2"
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "config": {
        "preferred-install": "dist",
        "autoloader-suffix": "Drupal8"
    },
    "repositories": {
        "drupal": {
            "type": "composer",
            "url":  "https://packages.drupal.org/8"
        }
    },
    "extra": {
        "_readme": [
            "By default Drupal loads the autoloader from ./vendor/autoload.php.",
            "To change the autoloader you can edit ./autoload.php."
        ],
        "merge-plugin": {
            "include": [
                "core/composer.json"
            ],
            "recurse": false,
            "replace": false,
            "merge-extra": false
        },
        "installer-paths": {
            "modules/contrib/{$name}": ["type:drupal-module"],
            "themes/contrib/{$name}": ["type:drupal-theme"]
        }
    },
    "autoload": {
        "psr-4": {
            "Drupal\\Core\\Composer\\": "core/lib/Drupal/Core/Composer"
        }
    },
    "scripts": {
        "pre-autoload-dump": "Drupal\\Core\\Composer\\Composer::preAutoloadDump",
        "post-autoload-dump": "Drupal\\Core\\Composer\\Composer::ensureHtaccess",
        "post-package-install": "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup",
        "post-package-update": "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup"
    }
}

The lack of the packages repository in composer.json is a small, easily missed and often overlooked thing. Hopefully it will be added in a future release.

7
  • Thanks for the detailed answer, I've noticed after composer successfully installed the theme, it created a folder for the theme in /themes/. My understanding is that composer should manage everything into vendor folder. My question is how should I manage this using git when composer is adding it to the themes folder. Should I add themes folder to .gitignore?
    – Bishoy
    Commented Jan 17, 2017 at 14:40
  • Drupal packages are different. They will install to modules and themes respectively. Non Drupal projects will go into the vendor directory.
    – Kevin
    Commented Jan 17, 2017 at 14:41
  • OK got it, but still, should I commit this to git? then will I need to use composer again on staging and production?
    – Bishoy
    Commented Jan 17, 2017 at 14:43
  • 1
    As for version control thats totally up to you. Some people are using Drupal Composer and Acquia BLT to manage their codebase, which come OOTB with gitignore rules for Drupal core and contrib modules and themes. This requires you to learn how to build and deploy those projects, though. See: github.com/drupal-composer/drupal-project
    – Kevin
    Commented Jan 17, 2017 at 14:45
  • 1
    That said, if you are not using any build/deployment process, yes you should commit it so you can push it to your remote environments (unless you have Jenkins or other processes that can run composer and build your Drupal stack).
    – Kevin
    Commented Jan 17, 2017 at 14:46
4

You need to add the Drupal repository location to your composer.json file, e.g.

"repositories": [
    {
        "type": "composer",
        "url": "https://packages.drupal.org/8"
    }
]
3
  • Can you please elaborate a bit? My current situation is that I got twbs/bootstrap installed. Do I need to uninstall that? Is the repository entry you are referring to to be able to run composer require drupal/bootstrap? Is that all and I should start seeing the theme in the list view at the appearance admin page?
    – Bishoy
    Commented Jan 17, 2017 at 14:13
  • 1
    @Bishoy please see my extended explanation.
    – Kevin
    Commented Jan 17, 2017 at 14:21
  • 1
    @Bishoy Yes, yes and yes!
    – Clive
    Commented Jan 17, 2017 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.