- Drupal 8
La gestione di un progetto Drupal 8 con Composer, Drush e GIT
The new and best practices for Drupal 8 indicate the combined use of Composer and Drush for implementing the most convenient workflow of web application administration, both in its phase of installation, management and subsequent development under "Continuous Integration".
This tutorial outlines a procedure based on the above guidelines, consolidated from my best work experience and based on other valuables web contributions from Drupal guidelines and experts,
all failry mentioned in the embeded links.
It is assumed the prior installation and availability of the following tools on your development environment:
- Composer (PHP dependency manager): https://getcomposer.org/download/
- Drush (Drupal Shell): https://www.drupal.org/node/2603018
- Git (Code Versioning): https://git-scm.com/downloads
Build Application Package and Drupal 8 Code base
With references to the Composer template for Drupal projects
To create a new Drupal project using Composer, type the following on the command line,
$ cd /sites $ sudo composer create-project drupal-composer/drupal-project:~8.0 my-drupal-site --stability dev --no-interaction
where:
~8.0 indicates the version of Drupal to be installed (according to Composer versions conventions)
/sites/my-drupal-site is the desired code location.
After this process we will end up with a project structure that hosts a composer.json file at its root level, that will guide and manage all the Drupal application structure.
The application structure itself will contains root accessorial root folders (drush, scripts, vendor) and the "web" folder containing the Drupal 8 code base.
Install Drupal 8 Mysql Database
After creating an empty database you can then install Drupal using the right version of Drush for Drupal 8 that comes embedded into the installed package (or the right Drush version globally installed in your system):
$ cd my-drupal-site/web $ ../vendor/bin/drush site-install --db-url=mysql://{username}:{password}@localhost/{database}
At the end of the process, a confirmation message will output the username and password for my-drupal-site administrator
---------------------
NOTE: To reset the admin password at this stage
follow this intructions (rif: http://enzolutions.com/articles/2014/12/07/resetting-the-administrator-password-with-sql-query-in-drupal-8/)
$ php core/scripts/password-hash.sh 'admin' password: admin hash: $S$E8DpyG1FEHFhGx1FaWdT3a3jx41oqoVsCAtn9HBOSd6spOr6ouHP
Now you need to update the user password, in our case we need update the Administrator password, fortunately the UID for Administrator is 1 equal to previous versions of Drupal,
so, from your mysql run the following:
UPDATE users_field_data SET pass='$S$E8DpyG1FEHFhGx1FaWdT3a3jx41oqoVsCAtn9HBOSd6spOr6ouHP' WHERE uid = 1; DELETE FROM cache_entity WHERE cid = 'values:user:1';
------------------
Install and manage Contributed Modules
Adding contributed modules in this Drupa 8 project (with composer) is done differently from using the previous (magic) drush command “drush dl”,
as they will be fetched by Composer from the “repository” index indicated in the composer.json file.
Until official Package Repository from Drupal.org won’t be completely ready and operational,
it will be possibile to grab contributed Modules from Drupal Packagist
running composer commands from the project root (one level above the Drupal root represented by the "web" folder):
For instance:
$ composer require drupal/migrate_upgrade 8.1.*@dev $ composer require drupal/migrate_plus 8.1.*@dev
Note: To understand versions conventions & symbols visit the Composer Versions page.
Commit Code base Files to the GIT Repo
The files downloaded by Composer do not need to be added to the repo.
You’ll see a .gitignore file that keeps them out (this was added as a part of the drupal-composer/drupal-project composer create-project process).
Only composer.json, .gitignore, code in /web/modules/custom and the content of the /web/sites subdirectory (other than the "files" directory) will be stored in the git repository.
Set up Drupal Configuration Managment Versioning with GIT
Out of the box, the first Drupal install places the “sync” configurations files directory into an hashed based name folder inside the “web/sites/default/files” directory, that wouldn’t be versioned (as under .gitignore)
It is a best practice to make the configuration management files versioned instead, to properly start tracking configuration changes and implement Drupal Continous Integration.
Thus it is appropriate to re-set Drupal's config (sync) directory at the package root level doing the following.
In the settings.php file add the following lines of code (commenting/erasing the corresponding existing ones):
# Changing The Storage Location of the Sync Directory $config_directories[CONFIG_SYNC_DIRECTORY] = '../config';
That will setup the project_root_folder/config folder as the destination of every export of configuration yaml files.
From now on it will be effective to track yaml configuration files changes via GIT, using the following command to perform config export via drush:
$ drush config-export (or: $ drush cex)
(see the https://drushcommands.com/drush-8x/config/config-export for all command options)
Similarly, the corresponding configuration yaml files, pushed in the GIT repo to be deployed, may be imported into Drupal application destination through following and complementary Drush command:
$ drush config-import (or: $ drush cim)