{"id":42488,"date":"2016-11-29T09:53:50","date_gmt":"2016-11-29T04:23:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=42488"},"modified":"2017-03-20T17:05:06","modified_gmt":"2017-03-20T11:35:06","slug":"composer-dependency-manager-for-drupal-8","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/composer-dependency-manager-for-drupal-8\/","title":{"rendered":"How to Use Composer to Manage Dependencies in Drupal 8?"},"content":{"rendered":"<p>Even before I start, I would like to shed some light on what is Composer and how we can build a website using Drupal 8. Let&#8217;s start with point to point introduction and usage of each key component while managing\/developing <a title=\"Drupal Development\" href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\" target=\"_blank\">Drupal 8 Website<\/a>:<\/p>\n<ul>\n<li style=\"font-weight: 400;\"><strong>What is Composer?<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>What is Packagist?<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>Composer and Drupal 8<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>Commit composer.json and composer.lock to versioning tool<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>Should I use \u201ccomposer install\u201d OR \u201ccomposer update\u201d command?<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>Apply Patch using Composer<\/strong><\/li>\n<li style=\"font-weight: 400;\"><strong>Composer.json in custom module<\/strong><\/li>\n<\/ul>\n<p><strong>What is Composer?<\/strong><\/p>\n<p><span style=\"font-weight: 400;\">Composer is\u00a0a tool\/program that\u00a0downloads all described libraries\/packages\u00a0automatically which are needed while developing a website. These packages can be loaded for individual project or globally using \u201cglobal\u201d command. That\u2019s why we also call it \u201cDependency Manager\u201d. Composer works well with all major Operating systems like Windows, Linux and OSX but requires minimum PHP version 5.3.2+ to run.<\/span><\/p>\n<p><strong>What is Packagist?<\/strong><\/p>\n<p><span style=\"font-weight: 400;\">It&#8217;s a Composer Repository from where Composer requests for the libraries\/packages(code files). It can also be referred to as\u00a0PHP Package Repository. We can add additional repositories like we are going to add Drupal Packagist. Drupal Packagist works in both Drupal 8 and Drupal 7 projects. <\/span><\/p>\n<ul>\n<li><strong>Drupal Packagist:\u00a0<\/strong>This Drupal Packagist is scheduled to be deprecated in January 2017 (or when the official Package Repository from Drupal.org is ready). But by that time we would either be able to use this for <a title=\"Custom Drupal Development\" href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\" target=\"_blank\">Drupal 7 or Drupal 8 projects.<\/a><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">In order to include repository for Drupal projects we need to add below code snippet in Default \u201ccomposer.json\u201d file.<\/span><\/p>\n<p><em>{<\/em><br \/>\n<em>&#8220;type&#8221;: &#8220;composer&#8221;,<\/em><br \/>\n<em>&#8220;url&#8221;: &#8220;http:\/\/packagist.drupal-composer.org\/&#8221;<\/em><br \/>\n<em>},<\/em><\/p>\n<p><span style=\"font-weight: 400;\">Now the question is where is composer.json file? I will explain creation of \u201ccomposer.json\u201d later in next section.<\/span><\/p>\n<div id=\"attachment_42756\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-42756\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-42756 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/11\/Drupal-Packagist1-1024x226.png\" alt=\"Drupal Packagist\" width=\"625\" height=\"137\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/11\/Drupal-Packagist1-1024x226.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/11\/Drupal-Packagist1-300x66.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/11\/Drupal-Packagist1-624x138.png 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/11\/Drupal-Packagist1.png 1275w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-42756\" class=\"wp-caption-text\">Drupal Packagist<\/p><\/div>\n<p><b>Composer and Drupal 8<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s discuss the use of Composer with the Drupal 8.\u00a0<\/span>This article is about introducing you with composer and Drupal 8. You can check if the composer is already installed on your machine or not by using \u201ccomposer\u201d command.<\/p>\n<p><i><span style=\"font-weight: 400;\">$ composer<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">If above commands show composer along with version then you are good to go. Else, In order to install composer on your machine please read the Installation Guide: (<\/span><a href=\"https:\/\/getcomposer.org\/doc\/00-intro.md#installation-linux-unix-osx\"><span style=\"font-weight: 400;\">https:\/\/getcomposer.org\/doc\/00-intro.md#installation-linux-unix-osx<\/span><\/a><span style=\"font-weight: 400;\"> )<\/span><\/p>\n<p><span style=\"font-weight: 400;\">At the time of writing this blog I am using: <\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\"><strong>Operating System:<\/strong> Ubuntu 14.04 LTS<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>Drupal version:<\/strong> Drupal 8.2.2 (<\/span><a href=\"https:\/\/www.drupal.org\/project\/drupal\/releases\/8.2.2\"><span style=\"font-weight: 400;\">https:\/\/www.drupal.org\/project\/drupal\/releases\/8.2.2<\/span><\/a><span style=\"font-weight: 400;\">)<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>Composer Version:<\/strong> 1.1.2<\/span><\/li>\n<\/ul>\n<p>How to Manage Drupal site dependencies using Composer?<\/p>\n<p><span style=\"font-weight: 400;\">We have two options to build our new Drupal 8 websites using composer (in simple language),<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"> As we install Drupal from drupal.org (<\/span><a href=\"https:\/\/www.drupal.org\/download\"><span style=\"font-weight: 400;\">https:\/\/www.drupal.org\/download<\/span><\/a><span style=\"font-weight: 400;\">)<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"> As a kickstart for managing site dependencies where \u201cvendor\u201d directory is not included in Drupal folder itself (preferred way).<\/span><\/li>\n<\/ol>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">As we install Drupal from drupal.org<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">In first way, composer uses DRUPAL itself as a template and two main auto generated composer files \u201ccomposer.json\u201d and \u201ccomposer.lock\u201d resides in root folder.<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">$ mkdir d8drupa<\/span><\/i><span style=\"font-weight: 400;\">l<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">$ composer create-project drupal\/drupal d8drupal 8.2.*@dev &#8211;no-dev<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Above command will download the latest Drupal release dev codebase for version \u201c8.2.*-dev\u201d in \u201cd8drupal\u201d directory. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">If we chose this way, then we need to do some manual changes in \u201ccomposer.json\u201d file for:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Drupal.org projects as a Composer Repository<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">By default, composer uses Packagist (<\/span><span style=\"font-weight: 400;\">https:\/\/packagist.org\/<\/span><span style=\"font-weight: 400;\">). Packagist is a repository which manages PHP and PHP-related projects but lacks Drupal projects (modules,themes, libraries etc). So Drupal.org provides its own repository of Drupal projects for Composer. In order to include new repository, we need to edit composer.json file. Open \u201ccomposer.json\u201d in any editor and add below lines after \u201clicense&#8221;: &#8220;GPL-2.0+\u201d:<\/span><\/p>\n<p style=\"text-align: left;\"><em> &#8220;repositories&#8221;: {<\/em><br \/>\n<em> &#8220;drupal&#8221;: {<\/em><br \/>\n<em> &#8220;type&#8221;: &#8220;composer&#8221;,<\/em><br \/>\n<em> &#8220;url&#8221;: &#8220;https:\/\/packages.drupal.org\/8&#8221;<\/em><br \/>\n<em> }<\/em><br \/>\n<em> },<\/em><\/p>\n<p><span style=\"font-weight: 400;\">This will associate composer to download Drupal projects from Drupal.org. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">If we are working on D7 websites, then URL for repository will be \u201chttps:\/\/packages.drupal.org\/7\u201d <\/span><\/p>\n<p><span style=\"font-weight: 400;\">OR we can achieve above requirement using terminal:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">$ \u00a0composer config repositories.drupal composer https:\/\/packages.drupal.org\/8<\/span><\/i><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Download contributed projects like modules, themes, libraries or profiles in desired directories rather than default \u201cvendor\u201d directory.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">When we build a website in Drupal 8, we need to download required contributed modules, themes, libraries and we want to maintain a directory structure for modules, themes and libraries separately. This means all contributes Modules must be downloaded into \u201cmodules\/contrib\/\u201d directory and Themes in \u201cthemes\/contrib\u201d. Then below lines in \u201ccomposer.json\u201d file should be added.<\/span><\/p>\n<p><em>&#8220;extra&#8221;: {<\/em><br \/>\n<em> &#8220;installer-paths&#8221;: {<\/em><br \/>\n<em> &#8220;modules\/contrib\/{$name}&#8221;: [&#8220;type:drupal-module&#8221;],<\/em><br \/>\n<em> &#8220;modules\/custom\/{$name}&#8221;: [&#8220;type:drupal-custom-module&#8221;],<\/em><br \/>\n<em> &#8220;profiles\/contrib\/{$name}&#8221;: [&#8220;type:drupal-profile&#8221;],<\/em><br \/>\n<em> &#8220;themes\/contrib\/{$name}&#8221;: [&#8220;type:drupal-theme&#8221;],<\/em><br \/>\n<em> &#8220;themes\/custom\/{$name}&#8221;: [&#8220;type:drupal-custom-theme&#8221;]<\/em><br \/>\n<em> }<\/em><br \/>\n<em>}<\/em><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">No Drush and Drupal Console tools:<\/span><\/li>\n<\/ol>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">This way of using composer doesn\u2019t include Drush and Drupal-console by default. <\/span><\/li>\n<\/ul>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400;\"> \u00a0As a kickstart for managing site dependencies<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">$mkdir d8composer<\/span><\/p>\n<p><span style=\"font-weight: 400;\">$composer create-project drupal-composer\/drupal-project:8.x-dev d8composer &#8211;stability dev &#8211;no-interaction<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If we choose this way, then we have so many benefits which auto cover above mentioned manual changes and features along with updating the Drupal core with Scaffold files. (like .htaccess, robots.txt, index.php, update.php, etc). <\/span><\/p>\n<p><strong>Download Drupal Projects:<\/strong> Regardless the way we choose to use composer with <a title=\"Drupal Development\" href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\" target=\"_blank\">Drupal 8<\/a>. We can use below commands to download Drupal projects as we have configured Drupal projects repository and defined the path where we want to download modules, themes, libraries etc.<\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s start with downloading a basic module \u201cGoogle Analytics\u201d using composer. \u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>General Command:<\/strong>\u00a0 Composer requires drupal\/&lt;modulename&gt; <\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Go to: <\/span><span style=\"font-weight: 400;\"><\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Search for the module\/theme you want to include in your project. In our case its \u201cGoogle Analytics\u201d, so search for the same. <\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">You will be redirected to dedicated page of google analytic project. (<\/span><span style=\"font-weight: 400;\"><\/span><span style=\"font-weight: 400;\">)<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Go back to terminal and hit the below command: <\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0<\/span> <i><span style=\"font-weight: 400;\">$ composer require drupal\/google_analytics<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">OR <\/span><\/p>\n<p><span style=\"font-weight: 400;\">There is better option to look for the projects you want to include using <\/span><i><span style=\"font-weight: 400;\">\u201cComposer search DRUPAL_PROJECT\u201d<\/span><\/i><span style=\"font-weight: 400;\"> command.<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">$ composer search pathauto<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">This command will automatically rewrite your composer.json file with a require statement for this new project. In this case, it added \u201cdrupal\/google_analytics&#8221;: &#8220;^2.1\u201d. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Composer will automatically download all dependent projects as well. Try the same command for \u201cPath Auto\u201d module, it will automatically add dependent modules like \u201cctools\u201d &amp; \u201ctoken\u201d modules as well. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can specify the version while downloading the Drupal packages:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">$ composer require drupal\/&lt;modulename&gt;:&lt;version&gt; <\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\"><strong>For example:<\/strong> If we want to include the 8.x-1.0-beta1 version of pathauto, then command will be: <\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">$ composer require drupal\/pathauto:1.0-beta1<\/span><\/i><\/p>\n<p><b>Commit <\/b><b><i>composer.json<\/i><\/b><b> and <\/b><b><i>composer.lock<\/i><\/b><b> to Versioning Tool<\/b><\/p>\n<p><span style=\"font-weight: 400;\">\u201cComposer.lock\u201d is also an important file which maintains the exact version of project to download\/use. This file helps in ensuring that all developers are using the same version. Some developers do not push this file to their versioning tool like GIT, SVN, but I will recommend to push this file too along with \u201ccomposer.json\u201d . <\/span><\/p>\n<p><b>Should I use \u201ccomposer install\u201d OR \u201ccomposer update\u201d command?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">If \u201ccomposer.lock\u201d file already exists and you want to download exact version of projects which are running on other developers systems or on servers then you should use \u201cinstall\u201d command.<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">$ composer install<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Use \u201cupdate\u201d command If you have :<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"> Used the another version of project or<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"> Included the new project or<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"> Composer.lock does not exist<\/span><\/li>\n<\/ol>\n<p><i><span style=\"font-weight: 400;\">$ composer update<\/span><\/i><\/p>\n<p><b>Apply Patch using Composer<\/b><\/p>\n<p><span style=\"font-weight: 400;\">If you want to apply patches to composer-built dependencies \u00a0using composer, then you need to add \u201ccweagans\/composer-patches&#8221; package. In order to do so please add below mentioned code in \u201crequire\u201d array of \u201ccomposer.json\u201d file:<\/span><\/p>\n<p><em> &#8220;require&#8221;: {<\/em><br \/>\n<em> &#8220;composer\/installers&#8221;: &#8220;^1.2&#8221;,<\/em><br \/>\n<em> &#8220;drupal-composer\/drupal-scaffold&#8221;: &#8220;^2.2&#8221;,<\/em><br \/>\n<strong><em> &#8220;cweagans\/composer-patches&#8221;: &#8220;~1.0&#8221;,<\/em><\/strong><br \/>\n<em> &#8220;drupal\/core&#8221;: &#8220;~8.0&#8221;,<\/em><br \/>\n<em> &#8220;drush\/drush&#8221;: &#8220;~8.0&#8221;,<\/em><br \/>\n<em> &#8220;drupal\/console&#8221;: &#8220;~1.0&#8221;,<\/em><br \/>\n<em> &#8220;drupal\/pathauto&#8221;: &#8220;1.x-dev&#8221;,<\/em><br \/>\n<em> &#8220;drupal\/google_analytics&#8221;: &#8220;^2.1&#8221;<\/em><br \/>\n<em> },<\/em><\/p>\n<p><span style=\"font-weight: 400;\">After adding this, you need to run \u201ccomposer update\u201d command which will create a directory called \u201ccweagans\u201d in \u201cvendor\u201d directory. That\u2019s it. Now you can apply patches to all the composer-built packages. For this you need to modify the \u201ccomposer.json\u201d file again. <\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Suppose:<\/strong>\u00a0We have downloaded the \u201cpathauto\u201d project using \u201ccomposer require drupal\/pathauto\u201d command. Now we want to apply a patch <\/span><\/p>\n<p><span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">https:\/\/www.drupal.org\/files\/issues\/pathauto-2810531-2.patch<\/span><span style=\"font-weight: 400;\"> ) to this module.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This time you need to add \u201cpatches\u201d array in \u201cextra\u201d. Please find below sample code of \u201ccomposer.json\u201d file:<\/span><\/p>\n<p><em> &#8220;extra&#8221;: {<\/em><br \/>\n<em> &#8220;installer-paths&#8221;: {<\/em><br \/>\n<em> &#8220;web\/core&#8221;: [&#8220;type:drupal-core&#8221;],<\/em><br \/>\n<em> &#8220;web\/libraries\/{$name}&#8221;: [&#8220;type:drupal-library&#8221;],<\/em><br \/>\n<em> &#8220;web\/modules\/contrib\/{$name}&#8221;: [&#8220;type:drupal-module&#8221;],<\/em><br \/>\n<em> &#8220;web\/profiles\/contrib\/{$name}&#8221;: [&#8220;type:drupal-profile&#8221;],<\/em><br \/>\n<em> &#8220;web\/themes\/contrib\/{$name}&#8221;: [&#8220;type:drupal-theme&#8221;],<\/em><br \/>\n<em> &#8220;drush\/contrib\/{$name}&#8221;: [&#8220;type:drupal-drush&#8221;]<\/em><br \/>\n<em> },<\/em><br \/>\n<strong><em> &#8220;patches&#8221;: {<\/em><\/strong><br \/>\n<strong><em> &#8220;drupal\/pathauto&#8221;: {<\/em><\/strong><br \/>\n<strong><em> &#8220;Pathauto string parameter with default null&#8221;: &#8220;https:\/\/www.drupal.org\/files\/issues\/pathauto-2810531-2.patch&#8221;<\/em><\/strong><br \/>\n<strong><em> }<\/em><\/strong><br \/>\n<strong><em> }<\/em><\/strong><br \/>\n<em> }<\/em><\/p>\n<p><i><span style=\"font-weight: 400;\">Here: <\/span><\/i><\/p>\n<p><b><i>KEY &#8212; <\/i><\/b><i><span style=\"font-weight: 400;\">&#8220;Pathauto string parameter with default null&#8221; is the description to the patch. This can be anything.<\/span><\/i><\/p>\n<p><b><i>Value<\/i><\/b><i><span style=\"font-weight: 400;\"> &#8212; \u201c<\/span><\/i><i><span style=\"font-weight: 400;\">https:\/\/www.drupal.org\/files\/issues\/pathauto-2810531-2.patch<\/span><\/i><i><span style=\"font-weight: 400;\">\u201d source path to the patch<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">To apply a patch, you need to hit the \u201ccomposer update\u201d command then patch will be automatically applied to our targeted project (which is \u201cpathauto\u201d in this case) \u00a0and command prompt will show a message:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">Gathering patches for root package.<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">Gathering patches for dependencies. This might take a minute.<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"> \u00a0&#8211; Installing drupal\/pathauto (dev-1.x 13fe7a7)<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0Cloning 13fe7a78788d848dd78ed88b6305c53d8cdcd9b2<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"> \u00a0&#8211; Applying patches for drupal\/pathauto<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0https:\/\/www.drupal.org\/files\/issues\/pathauto-2810531-2.patch (Pathauto string parameter with default null)<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">Writing lock file<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">Similarly, you can define as many patches to any number of projects by adding a key-value pair in patches array. <\/span><\/p>\n<p><b>Composer.json in custom module<\/b><span style=\"font-weight: 400;\">: We can add \u201ccomposer.json\u201d to our custom module and download all the required projects like libraries for it. Let&#8217;s start by a creating a custom module named \u201ctest_composer\u201d in \u201cweb\/modules\/custom\u201d directory which will simply download a \u201carchwisp\/php-encrypt-data\u201d project from <\/span><span style=\"font-weight: 400;\">https:\/\/packagist.org\/packages\/archwisp\/php-encrypt-data.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">(This package simply encrypts the text).<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> Create a \u201ctest_composer.info.yml\u201d file:<\/span><\/li>\n<\/ol>\n<p><i><span style=\"font-weight: 400;\">name: Test Composer<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">type: module<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">description: &#8216;Will download a package using composer&#8217;<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">package: Custom<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">core: &#8216;8.x&#8217;<\/span><\/i><\/p>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400;\"> \u00a0Create \u201ccomposer.json\u201d file in this custom module which is \u201ctest_composer\u201d directory and paste the below code:<\/span><\/li>\n<\/ol>\n<p><em>{<\/em><br \/>\n<em> &#8220;name&#8221;: &#8220;testing\/composer&#8221;,<\/em><br \/>\n<em> &#8220;description&#8221;: &#8220;custom composer json to play with string encryption&#8221;,<\/em><br \/>\n<em> &#8220;type&#8221;: &#8220;drupal-module&#8221;,<\/em><br \/>\n<em> &#8220;require&#8221;: {<\/em><br \/>\n<em> &#8220;archwisp\/php-encrypt-data&#8221; : &#8220;1.1.0&#8221;<\/em><br \/>\n<em> }<\/em><br \/>\n<em>}<\/em><\/p>\n<ol start=\"3\">\n<li><span style=\"font-weight: 400;\"> Create \u201ctest_composer.routing.yml\u201d file in order to \u00a0view the data.<\/span><\/li>\n<\/ol>\n<p><em>test.hello:<\/em><br \/>\n<em> path: &#8216;\/test\/composer&#8217;<\/em><br \/>\n<em> defaults:<\/em><br \/>\n<em> _controller: &#8216;\\Drupal\\test_composer\\Controller\\DefaultController::general&#8217;<\/em><br \/>\n<em> _title: &#8216;Testing Composer with a sample package&#8217;<\/em><br \/>\n<em> requirements:<\/em><br \/>\n<em> _permission: &#8216;administer blocks&#8217;<\/em><\/p>\n<ol start=\"4\">\n<li><span style=\"font-weight: 400;\"> Create an \u201cDefaultController.php\u201d file under the \u201c\/web\/modules\/custom\/test_composer\/src\/Controller\u201d directory. <\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">In order to generate our own \u201c<\/span><b>$encryptionKey<\/b><span style=\"font-weight: 400;\">\u201d and \u201c<\/span><b><i> $macKey<\/i><\/b><span style=\"font-weight: 400;\">\u201d, open the terminal and hit the below mentioned commands:<\/span><\/p>\n<p><em>*Generate your encryption key:<\/em><br \/>\n<em>$ head -c 32 \/dev\/urandom | base64<\/em><br \/>\n<em>pTUgV9Qx09EuJ+GcleRU5aD9i5ge2mdriCLkH8xTfV0= (my case)<\/em><\/p>\n<p><em>*Generate your MAC key:<\/em><br \/>\n<em>$ head -c 32 \/dev\/urandom | base64<\/em><br \/>\n<em>uanShOJZ6YV7j0jD0iCZodrOmmaqMS+aPzi3BluhkM0= (my case)<\/em><\/p>\n<p><em>&lt;?php<\/em><\/p>\n<p><em>namespace Drupal\\test_composer\\Controller;<\/em><\/p>\n<p><em>use Drupal\\Core\\Controller\\ControllerBase;<\/em><br \/>\n<em>use Nayjest\\StrCaseConverter\\Str;<\/em><\/p>\n<p><em>\/**<\/em><br \/>\n<em>* Controller for building the block instance add form.<\/em><br \/>\n<em>*\/<\/em><br \/>\n<em>class DefaultController extends ControllerBase {<\/em><\/p>\n<p><em>\/**<\/em><br \/>\n<em> * Build the block instance add form.<\/em><br \/>\n<em> *<\/em><br \/>\n<em> * @param string $plugin_id<\/em><br \/>\n<em> * The plugin ID for the block instance.<\/em><br \/>\n<em> * @param string $theme<\/em><br \/>\n<em> * The name of the theme for the block instance.<\/em><br \/>\n<em> *<\/em><br \/>\n<em> * @return array<\/em><br \/>\n<em> * The block instance edit form.<\/em><br \/>\n<em> *\/<\/em><br \/>\n<em> public function general() {<\/em><br \/>\n<em> \/\/ These keys won&#8217;t actually work&#8230; on purpose. Create your OWN!<\/em><br \/>\n<em> $encryptionKey = &#8216;pTUgV9Qx09EuJ+GcleRU5aD9i5ge2mdriCLkH8xTfV0=&#8217;;<\/em><\/p>\n<p><em>$macKey = &#8216;uanShOJZ6YV7j0jD0iCZodrOmmaqMS+aPzi3BluhkM0=&#8217;; <\/em><br \/>\n<em> $phpcrypt = new \\PHPEncryptData\\Simple($encryptionKey, $macKey);<\/em><br \/>\n<em> $ciphertext = $phpcrypt-&gt;encrypt(&#8216;Testing Package using composer&#8217;);<\/em><br \/>\n<em> $decrypted = $phpcrypt-&gt;decrypt($ciphertext);<\/em><br \/>\n<em> return [<\/em><br \/>\n<em> &#8216;#markup&#8217; =&gt; &#8220;Original Text: Testing Package using composer &lt;br&gt; Cipher text: $ciphertext &lt;br&gt; Decrypted text: $decrypted&#8221;,<\/em><br \/>\n<em> ];<\/em><br \/>\n<em> }<\/em><br \/>\n<em>}<\/em><br \/>\nOur custom module is ready to serve our purpose. But we need to manually add a &#8216;require statement&#8217; in our root\u2019s composer.json file. In this case, its:<br \/>\n&#8220;require&#8221;: {<br \/>\n\u2026..<br \/>\n&#8220;archwisp\/php-encrypt-data&#8221; : &#8220;1.1.0&#8221;<br \/>\n\u2026.<br \/>\n}<\/p>\n<p><span style=\"font-weight: 400;\">And that\u2019s it, we need to run \u201c$ composer update\u201d command, this will download a project called \u201carchwisp\u201d in our vendor directory. Now we can see the output at \u201c\/d8composer\/web\/test\/composer\u201d page, which should be like this.<\/span><\/p>\n<div id=\"attachment_42486\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-42486\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-42486 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/11\/Testing-Composer-with-a-sample-package-D8Composer-1024x228.png\" alt=\"Testing Composer with a sample package  D8Composer \" width=\"625\" height=\"139\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/11\/Testing-Composer-with-a-sample-package-D8Composer-1024x228.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2016\/11\/Testing-Composer-with-a-sample-package-D8Composer-300x66.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/11\/Testing-Composer-with-a-sample-package-D8Composer-624x138.png 624w, \/blog\/wp-ttn-blog\/uploads\/2016\/11\/Testing-Composer-with-a-sample-package-D8Composer.png 1244w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-42486\" class=\"wp-caption-text\">D8Composer<\/p><\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\"><strong>Note:<\/strong>\u00a0In above described use case, we can\u2019t use \u201ccomposer_manager\u201d contributed module because we are maintaining our whole website using composer. &#8220;Composer Manager\u201d is already deprecated due to improvements in Drupal 8.1 onwards.<\/span><\/p>\n<p><strong><span style=\"font-weight: 400;\">Let me conclude by saying, managing\/developing any website using composer provides more control over website. We should start using this amazing feature as project build process. I hope you liked this blog and do not forget to like and share this.<\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Even before I start, I would like to shed some light on what is Composer and how we can build a website using Drupal 8. Let&#8217;s start with point to point introduction and usage of each key component while managing\/developing Drupal 8 Website: What is Composer? What is Packagist? Composer and Drupal 8 Commit composer.json [&hellip;]<\/p>\n","protected":false},"author":1024,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[3602,1],"tags":[4229,4862,3707,3601,1173,4250],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42488"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1024"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=42488"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42488\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=42488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=42488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=42488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}