Colorfield logo

Drupalicious

Published on

Install CiviCRM 5 with Drupal 8 using Lando

Authors
CiviCRM and Drupal logos

CiviCRM is an old Drupal 7 friend and its relationships with Drupal 8 are in a pretty good way. Let's make them meet the Composer template for Drupal projects, a widespread setup that stores the vendor directory outside of the docroot.
Lando will host the party by spinning up a local environment, however this method should be generalizable with any other development environment.

Refer to this previous post to set up Lando.

Get Drupal and install it

Create a Drupal project

composer create-project drupal-composer/drupal-project:8.x-dev civi-drupal --stability dev --no-interaction

Initialize Lando

cd civi-drupal
lando init --recipe drupal8 --webroot web

Give your app the CiviDrupal name then start Lando

lando start

Finally, install Drupal

lando drush site-install standard --account-name=admin --account-pass=admin --db-url='mysql://drupal8:drupal8@database/drupal8' --site-name=CiviDrupal

Your site should be available at http://cividrupal.lndo.site:8000

Get CiviCRM for Drupal 8

We will use the method provided by David Snopek in this gist with CiviCRM 5.6.0.

Run the following commands at the root of the civi-drupal directory.

# Set CiviCRM  version
CIVICRM_VERSION=5.6.0

# Repositories aren't inherited from requirements, so we have to put this one from civicrm-core/composer.json again
composer config repositories.zetacomponents-mail vcs https://github.com/civicrm/zetacomponents-mail.git

# Require civicrm-core at the requested version.
composer require "civicrm/civicrm-core:$CIVICRM_VERSION"

# Run 'bower install'
(cd vendor/civicrm/civicrm-core && bower install)

# Download the latest Drupal package to copy some stuff that's
# generated by the build and included in release tarballs.
wget -O /tmp/civicrm.tar.gz https://download.civicrm.org/civicrm-$CIVICRM_VERSION-drupal.tar.gz
tar -xzf /tmp/civicrm.tar.gz -C /tmp
# Copy the 3rd party dependencies. Should be done via Composer at a later stage.
cp -r /tmp/civicrm/packages vendor/civicrm/civicrm-core/
# Setup the civicrm-version.php
cat /tmp/civicrm/civicrm-version.php | sed -e 's/Drupal/Drupal8/' > vendor/civicrm/civicrm-core/civicrm-version.php
# Copy sql
cp -r /tmp/civicrm/sql vendor/civicrm/civicrm-core/
# Copy some misc other things
cp /tmp/civicrm/civicrm.config.php vendor/civicrm/civicrm-core/
cp /tmp/civicrm/CRM/Core/I18n/SchemaStructure.php vendor/civicrm/civicrm-core/CRM/Core/I18n/
cp /tmp/civicrm/install/langs.php vendor/civicrm/civicrm-core/install/
# Clean-up
rm -rf /tmp/civicrm.tar.gz /tmp/civicrm

Get Drupal 8 CiviCRM module

Get the module from GitHub. We will use the 5.7.x-dev that requires CiviCRM Core >=5.4.0

composer config repositories.civicrm-drupal vcs https://github.com/civicrm/civicrm-drupal-8.git
composer require civicrm/civicrm-drupal-8:5.7.x-dev

Note that we will not use the https://github.com/civicrm/civicrm-drupal repository but this one https://github.com/civicrm/civicrm-drupal-8

Install CiviCRM

Go to /admin/modules and enable the CiviCRM Core module.

CiviCRM Core Drupal module

You will have this error while trying to access /civicrm in the browser.

Sorry, due to an error, we are unable to fulfill your request at the moment. You may want to contact your administrator or service provider with more details about what action you were performing when this occurred. DB Constraint Violation - contact_id should possibly be marked as mandatory for DashboardContact,create API. If so, please raise a bug report.

To get rid of it, go back to Drupal, logout and login again. See CRM-19878.

Allow CiviCRM to be accessed from the docroot

CiviCRM will need to access its Javascript/CSS assets, so we will symlink the civicrm-core directory in the Drupal libraries directory.

mkdir web/libraries
(cd web/libraries/ && ln -s ../../vendor/civicrm/civicrm-core civicrm)

Configure then the CiviCRM Resource URL at /civicrm/admin/setting/url?reset=1, set the following value:

[cms.root]/libraries/civicrm/

Thanks to klaas for the cms.root token!

Allow CiviCRM to access the extern directory

The scripts from this directory have to bee accessed directly, they are used for e.g. the CiviMail click through counters (url.php). Direct access to these scripts will be prevented by the default .htaccess directives.

CiviCRM extern directory

Make the scripts available by including this .htaccess override in the /libraries/civicrm/extern/ directory:

<IfModule mod_rewrite.c>
  RewriteEngine Off
</IfModule>

Let then know civicrm.config.php where to find the civicrm.settings.php file (/app/web/sites/default/civicrm.settings.php in this example).

Execute scheduled Jobs via the cron

Install CV (the CiviCRM CLI) then execute this command via a system cron, in the docroot.

cv api Job.execute

Happy CiviCRM hacking!