Book Image

Drupal 8 Development Cookbook - Second Edition

By : Matt Glaman
Book Image

Drupal 8 Development Cookbook - Second Edition

By: Matt Glaman

Overview of this book

Began as a message board, Drupal today is open source software maintained and developed by a community of over 1,000,000 users and developers. Drupal is used by numerous local businesses to global corporations and diverse organizations all across the globe. With Drupal 8’s exciting features it brings, this book will be your go-to guide to experimenting with all of these features through helpful recipes. We’ll start by showing you how to customize and configure the Drupal environment as per your requirements, as well as how to install third-party libraries and then use them in the Drupal environment. Then we will move on to creating blocks and custom modules with the help of libraries. We will show you how to use the latest mobile-first feature of Drupal 8, which will help you make your apps responsive across all the major platforms. This book will also show you how to incorporate multilingual facilities in your sites, use web services and third-party plugins with your applications from inside Drupal 8, and test and deploy your apps.
Table of Contents (20 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Installing modules and themes


Drupal 8 provides more functionality out-of-the-box than previous versions of Drupal, allowing you to do more with less. However, one of the more appealing aspects of Drupal is the ability to extend and customize.

In this recipe, we will download and enable the Honeypot module (https://www.drupal.org/project/honeypot) and tell Drupal to use the Bootstrap theme (https://www.drupal.org/project/bootstrap). The Honeypot module provides Honeypot and timestamps antispam measures on Drupal sites. This module helps protect forms from spam submissions. The Bootstrap theme implements the Bootstrap frontend framework and supports using Bootswatch styles to theme your Drupal site.

Note

This chapter's recipe will use the standard way of installing modules, by downloading archives available on Drupal.org. As of Drupal 8.2.0, installing modules through Composer has been possible and is the required method for some modules. Installing modules and themes using Composer is covered in the There's more... section of this recipe and is highly recommended.

Getting ready

If you have used Drupal before, note that the folder structure has changed. Modules, themes, and profiles are now placed in their respective folders in the root directory and no longer under sites/all. For more information about the developer experience change, refer to https://www.drupal.org/node/22336.

Note

Downloading the example code: You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you have purchased this book from elsewhere, you can go to http://www.packtpub.com/support and register yourself to have the files emailed directly to you.

How to do it...

Let's install modules and themes:

  1. Visit https://www.drupal.org/project/honeypot and download the latest 8.x release for Honeypot.
  2. Extract the archive and place the honeypot folder inside the modules folder, which is inside of your Drupal core installation:

  1. In Drupal, log in and select the Extend option to access the list of available modules.
  2. Using the search text field, type in Honeypot. Check the checkbox and click on Install.

 

  1. Once enabled, search for it again. Clicking on the module's description will expand the row and expose links to configure permissions and module settings:

  1. Visit https://www.drupal.org/project/bootstrap and download the latest 8.x release for Bootstrap.
  2. Extract the archive and place the bootstrap folder inside the themes folder, which is inside your Drupal core installation:

  1. In Drupal, select the Appearance option to manage your Drupal themes.
  2. Scroll down the page and click on Install and set as default under Bootstrap to enable and set the theme as default:

How it works...

The following sections outline the procedure for installing a module or theme and how Drupal discovers these extensions.

Discovering modules and themes

Drupal scans specific folder locations to identify modules and themes defined by the .info.yml file in their directory. The following is the order in which projects will be discovered:

  • Their respective core folders (modules, or themes)
  • The currently installed profile
  • The root modules or themes folder
  • The current site directory (default or current domain)

Module installation

By placing the module inside the root modules folder, we are allowing Drupal to discover the module and allow it to be installed. When a module is installed, Drupal will register its code with the system through the module_installer service. The service will check for required dependencies and prompt them to be enabled if required. The configuration system will run any configuration definitions provided by the module on installation. If there are conflicting configuration items, the module will not be installed.

Theme installation

A theme is installed through the theme_installer service and sets any default configuration by the theme along with rebuilding the theme registry. Setting a theme to default is a configuration change in system.theme.default to the theme's machine name (in the recipe, it would be bootstrap).

There's more...

The following section outlines the procedure for installing a module or theme and includes some additional information for installing.

Installing a module or theme using Composer

Although it is not the required way to install an extension, this should become your default method. Why? Because each module is a dependency in your project, and each of those may have its own dependencies. Composer can manage dependencies for you, or you can manage them manually. Your time and capabilities probably will not grow to scale as well as Composer will. Not to mention, it also provides a standard way for PHP projects to interoperate and load classes.

You can get the Honeypot module and Bootstrap using the following two commands:

$ cd /path/to/drupal8
$ composer require drupal/honeypot$ composer require drupal/bootstrap

Here is an example of contributed projects, which require Composer for installation, because they leverage existing libraries in the PHP community at large:

  • Drupal Commerce
  • GeoIP
  • Search API Solr
  • Entity Print

As more and more modules integrate existing SDK libraries, the requirement to use Composer will increase.

Installing a module with Drush

Modules can be downloaded and enabled through the command line using drush. The command to replicate the recipe would resemble the following:

$ drush pm-download honeypot$ drush pm-enable honeypot

Note

As of Drush 9, which supports Drupal 8.3+, this section is deprecated. Using Drush to download Drupal core or contributed modules will throw a warning to use Composer instead.

 

It will prompt you to confirm your action. If there were dependencies for the module, it would ask whether you will like to enable those, too.

Note

Drush simply downloads the archive available from Drupal.org. If the module or theme requires third-party PHP library dependencies, these will not be downloaded or be available in Drupal's class autoloading process.

Uninstalling a module

One of the substantial changes in Drupal 8 is the module disable and uninstall process. Previously, modules were first disabled and then uninstalled once disabled. This created a confusing process, which would disable its features, but not clean up any database schema changes. In Drupal 8, modules cannot just be disabled but must be uninstalled. This ensures that when a module is uninstalled it can safely be removed from the code base.

A module can only be uninstalled if it is not a dependency of another module or does not have a configuration item in use--such as a field type--which could disrupt the installation's integrity.

Note

With a standard installation, the Comment module cannot be uninstalled until you delete all the Comment fields on the article content type. This is because the field type is in use.

See also

  • Refer to Chapter 4, Extending Drupal, to learn about setting defaults on enabling a module.
  • Refer to Chapter 9, Configuration Management - Deploying in Drupal 8.