Drupal 8 Development Cookbook - Matt Glaman - E-Book

Drupal 8 Development Cookbook E-Book

Matt Glaman

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 386

Veröffentlichungsjahr: 2017

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Drupal 8 Development Cookbook

Second Edition

Harness the power of Drupal 8 with this recipe-based practical guide

 

Matt Glaman

BIRMINGHAM - MUMBAI

 

Drupal 8 Development Cookbook

Second Edition

 

Copyright © 2017 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

 

First published: March 2016

Second edition: September 2017

 

Production reference: 1210917

 

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78829-040-1

www.packtpub.com

Credits

Author

Matt Glaman

Copy Editor

Dhanya Baburaj

Reviewer

Tracy Charles Smith

Project Coordinator

Ritika Manoj

Commissioning Editor

Amarabha Banerjee

Proofreader

Safis Editing

Acquisition Editor

Nigel Fernandes

Indexer

Rekha Nair

ContentDevelopmentEditor

Mohammed Yusuf Imaratwale

Graphics

Jason Monteiro

Technical Editors

Ankur Ghiye

Murtaza Tinwala

Production Coordinator

Shantanu Zagade

About the Author

Matt Glaman is a Senior Drupal Consultant at Commerce Guys and co-maintainer of Drupal Commerce. He is an open source developer who has been working with Drupal since 2013. Since then, he has contributed to over 60 community project.

I would like to thank, and I am grateful to, my beautiful and loving wife for putting up with the late nights split between work, spending time contributing to the Drupal community, and writing this book. I would also like to thank my two sons; thank you for giving up your playtime so that Daddy could write this book.
Thank you, Andy Giles, for helping me get to my first Drupal camp and kicking off my Drupal career. I would also like to thank my mentors Bojan Živanović, David Snopek, Ryan Szrama, and everyone else in the Drupal community!

About the Reviewer

Tracy Charles Smith began working with computers at the age of 10. His background includes network support, web development, customer service, project management, and financial management.

Tracy's entrepreneurial spirit is a key component to his success in interacting with clients and team members on business and user-experience related technology solutions. In fact, he used that passion to found his own technology-consulting firm called Alpha Geek Tech, LLC. He also served as Technology Director for Quiddities Dev., Inc., in Santa Cruz, CA, before moving back to the DC area to join Phase2 in 2010 as a Senior Programmer. Tracy now works as a Senior Project Manager at Phase2 supporting Growth & Support clients in government and private enterprise. His diverse development background complements his project management skills.

Tracy was also the lead programmer and architect for 12seconds.tv in 2007 (a video messaging platform), which leveraged Drupal. He also authored Drupal Intranets with Open Atrium. He earned a BS in Computer Information Systems and Business Administration from Wingate University.

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe?

Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

Customer Feedback

Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1788290402

If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

Table of Contents

Preface

What this book covers

What you need for this book

Who this book is for

Sections

Getting ready

How to do it…

How it works…

There's more…

See also

Conventions

Reader feedback

Customer support

Downloading the example code

Errata

Piracy

Questions

Up and Running with Drupal 8

Introduction

Installing Drupal

Getting ready

How to do it...

How it works...

There's more...

Creating a database user and a database

Database prefixes

Downloading and installing with Drush

Using Composer to create a Drupal site

Security updates

See also

Using a distribution with Drupal

How to do it...

How it works...

There's more...

Makefiles

Installing with Drush

Using Composer

See also...

Installing modules and themes

Getting ready

How to do it...

How it works...

Discovering modules and themes

Module installation

Theme installation

There's more...

Installing a module or theme using Composer

Installing a module with Drush

Uninstalling a module

See also

Using multisites in Drupal 8

Getting ready

How to do it...

How it works...

There's more...

Security concerns

Domain aliases

See also...

Tools for setting up an environment

Getting ready

How to do it...

How it works...

There's more...

Acquia Dev Desktop

XAMPP + Bitnami

Kalabox

See also

Running tests - Simpletest and PHPUnit

Getting ready

How to do it...

How it works...

There's more...

Is run-tests a shell script?

Running tests without Drupal installed

Running specific tests

PhpStorm - Drupal Test Runner

DrupalCI

See also...

The Content Authoring Experience

Introduction

Configuring the WYSIWYG editor

Getting ready

How to do it...

How it works...

There's more...

Filter module

Improved links

CKEditor plugins

See also

Adding and editing content

Getting ready

How to do it...

How it works...

There's more...

Save as draft

Pathauto

Bulk moderation

See also

Creating a menu and linking content

Getting ready

How to do it...

How it works...

There's more...

Managing a contents menu link from its form

Providing inline editing

How to do it...

How it works...

There's more...

The outside-in approach

Creating a custom content type

How to do it...

How it works...

Applying new Drupal 8 core field types

Getting ready

How to do it...

Link

The Email field

The Telephone field

The Date field

The Entity Reference field

How it works...

There's more...

Upcoming updates

Views and Entity Reference

See also

Customizing the form display of a node

How to do it...

How it works...

There's more...

Managing form display modes

Programmatically providing a default to hidden form items

See also

Customizing the display output of a node

How to do it...

How it works...

Displaying Content through Views

Introduction

Listing content

Getting ready

How to do it...

How it works...

There's more...

Views in Drupal core initiative

Views and displays

Format style plugins - style and row

Using the Embed display

See also

Editing the default admin interfaces

How to do it...

How it works...

There's more...

Exposed versus non-exposed

Filter identifiers

Overriding routes with Views

Creating a block from a View

Getting ready

How to do it...

How it works...

There's more...

Exposed forms as blocks

See also

Utilizing dynamic arguments

How to do it...

How it works...

There's more...

Previewing with contextual filters

Displaying as a tab on the user page

Altering the page title

Validation

Multiple and exclusion

Adding a relationship in a View

How to do it...

How it works...

There's more...

Relationships provided by entity reference fields

Relationships provided through custom code

Using aggregation and views.

Providing an Entity Reference result View

How to do it...

How it works...

See also

Extending Drupal

Introduction

Creating a module

How to do it...

How it works...

There's more...

Module namespaces

Module discovery locations

Defining a package group

Module dependencies

Specifying the module's version

See also...

Defining a custom page

Getting ready

How to do it...

How it works...

There's more...

Parameters in routes

Validating parameters in routes

Route requirements

Providing dynamic routes

Altering existing routes

See also

Defining permissions

Getting ready

How to do it...

How it works...

There's more...

Restrict access flag for permissions

Defining permissions programmatically

Checking whether a user has permissions

Providing the configuration on the installation or update

Getting ready

How to do it...

How it works...

There's more...

Configuration subdirectories

Modifying the existing configuration on installation

See also

Creating an event subscriber

How to do it...

How it works...

There's more...

Using dependency injection

See also

Using Features 3.0

How to do it...

How it works...

There's more...

Suggested feature modules

Features bundles

Managing the configuration state of Features

See also

Frontend for the Win

Introduction

Creating a custom theme based on Classy

How to do it...

How it works...

There's more...

Theme screenshots

Themes, logos, and favicons

Base themes and shared resources

CKEditor style sheets

See also

Using the new asset management system

Getting ready

How to do it...

How it works...

There's more...

CSS groups

Library asset options

Library dependencies

Overriding and extending other libraries

Using a CDN or external resource as a library

Manipulating libraries from hooks

Placing JavaScript in the header

See also

Twig templating

Getting ready

How to do it...

How it works...

There's more...

Security first

Theme hook suggestions

Debugging template file selection and hook suggestions

The Twig logic and operators

See also

Using the Breakpoint module

Getting ready

How to do it...

How it works...

There's more...

Caveat for providing breakpoints from themes

Accessing breakpoints programmatically

Multipliers

See also

Using the Responsive Image module

Getting ready

How to do it...

How it works...

There's more...

Performance first delivery

Removing picturefill polyfill

See also

Creating Forms with the Form API

Introduction

Creating a form

Getting ready

How to do it...

How it works...

There's more...

Form element definitions

The form state

The form cache

See also

Using new HTML5 elements

Getting ready

How to do it...

How it works...

There's more...

Specific element properties

Creating new elements

See also

Validating form data

Getting ready

How to do it...

How it works...

There's more...

Multiple validation handlers

Accessing multidimensional array values

Element validation methods

Processing submitted form data

Getting ready

How to do it...

How it works...

There's more...

Multiple submit handlers

See also

Altering other forms

Getting ready

How to do it...

How it works...

There's more...

Adding additional validate handlers

Adding additional submit handlers

Plug and Play with Plugins

Introduction

Creating blocks using plugins

Getting ready

How to do it...

How it works...

There's more...

Altering blocks

Block settings forms

Defining access to a block

See also

Creating a custom field type

Getting ready

How to do it...

How it works...

There's more...

Altering field types

Defining whether a field is empty

See also

Creating a custom field widget

Getting ready

How to do it...

How it works...

There's more...

Field widget settings and summary

See also

Creating a custom field formatter

Getting ready

How to do it...

How it works...

There's more...

Formatter settings and summary

See also

Creating a custom plugin type

Getting ready

How to do it...

How it works...

There's more...

Specifying an alter hook

Using a cache backend

Accessing plugins through the manager

See also

Multilingual and Internationalization

Introduction

Translating administrative interfaces

Getting ready

How to do it...

How it works...

There's more...

Manually installing language files

Checking translation status

Exporting translations

Interface translation permissions

Using interface translation to customize default English strings

Interface text language detection

Providing translations for a custom module

See also

Translating configuration

Getting ready

How to do it...

How it works...

There's more...

Altering configuration translation info definitions

Translating views

See also

Translating content

Getting ready

How to do it...

How it works

There's more...

Flagging translations as outdated

Translating content links

Defining translation handlers for entities

See also

Creating multilingual views

Getting ready

How to do it...

How it works...

There's more...

Translating exposed form items and filters

Translating display and row format items

Translating page display menu items

See also

Configuration Management - Deploying in Drupal 8

Introduction

Importing and exporting configurations

Getting ready

How to do it...

How it works...

There's more...

Configuration dependencies

Saving to a YAML file for a module's configuration installation

Configuration schemas

See also

Synchronizing site configurations

Getting ready

How to do it...

How it works...

There's more...

Universally Unique Identifier

A synchronization folder

Installing a configuration from a new site

Using command-line workflow processes

Getting ready...

How to do it...

How it works...

There's more...

Drush config-pull

Using the Drupal Console

Editing the configuration from the command line

Exporting a single configuration item

Using version control and command-line workflow

See also

Updating and installing new module configurations

How to do it...

How it works...

There's more...

The Configuration Development module

See also

The Entity API

Introduction

Creating a configuration entity type

Getting ready

How to do it...

How it works...

There's more...

Available data types for schema definitions

See also

Creating a content entity type

Getting ready

How to do it...

How it works...

There's more...

Using the AdminHtmlRouteProvider provider

Making the collection route a local task tab

See also

Creating a bundle for a content entity type

Getting ready

How to do it...

How it works...

There's more...

Provide action links for adding new bundles

See also

Implementing custom access control for an entity

Getting ready

How to do it...

How it works...

There's more...

Controlling access to entity fields

See also

Providing a custom storage handler

Getting ready

How to do it...

How it works...

There's more...

Utilizing a different storage backend for an entity

See also

Creating a route provider

Getting ready

How to do it...

How it works...

There's more...

The Entity API module provides additional providers

See also

Off the Drupalicon Island

Introduction

Implementing and using a third-party JavaScript library

Getting ready

How to do it...

How it works...

There's more...

Best practices for handling external libraries

See also

Implementing and using a third-party CSS library

Getting ready

How to do it...

How it works...

See also

Implementing and using a third-party PHP library

Getting ready

How to do it...

How it works...

See also

Web Services

Introduction

Enabling RESTful interfaces

Getting ready

How to do it...

How it works...

There's more...

Using _format instead of the Accept header

RestResource plugin to expose data through RESTful Web Services

Rate limiting your API

Using the HAL format

See also

Using POST to create data

Getting ready

How to do it...

How it works...

There's more...

Using HAL and understanding _links requirements

Working with images

Using Cross-Site Request Forgery tokens

See also

Using PATCH to update data

Getting ready

How to do it...

How it works…

Using Views to provide custom data sources

How to do it…

How it works

There's more...

Controlling the key name in JSON output

Controlling access to RESTful Views

Authentication

Getting ready

How to do it

How it works

There's more…

Authentication provider services

Page cache request policies and authenticated Web service requests

The IP Authentication provider

See also

Using JSON API

Getting ready

How to do it

How it works...

There's more...

Paginating, filtering, and sorting requests

Installing the JSON API Extras module

Changing the API path prefix

Disabling and enhancing returned entity fields

Contenta CMS

See also

The Drupal CLI

Introduction

Rebuilding cache in Drupal Console or Drush

How to do it…

How it works…

See also

Using Drush to interact with the database

Getting ready

How to do it...

How it works...

There's more…

Using gzip with sql-dump

Using Console to interact with the database

Using Drush to manage users

How to do it…

How it works…

There's more…

Advanced user-login use cases

Using Drupal Console

Scaffolding code through Console

Getting ready

How to do it…

How it works…

Making a Drush command

Getting ready

How to do it…

How it works…

There's more…

Specifying the level of Drupal's bootstrap

See also

Making a Console command

Getting ready

How to do it…

How it works…

There's more…

Using a Console command to create entities

See also

Preface

Drupal is a content management system used to build websites for small businesses, e-commerce, enterprise systems, and much more. Created by over 4,500 contributors, Drupal 8 provides many new features to Drupal. Whether you are new to Drupal, or an experienced Drupalista, Drupal 8 Development Cookbook contains recipes to dive into what Drupal 8 has to offer.

What this book covers

Chapter 1, Up and Running with Drupal 8, begins by covering the requirements for running Drupal 8 and going through the installation process and extending Drupal.

Chapter 2, The Content Authoring Experience, dives into the content management experience in Drupal, including working with the newly bundled CKEditor.

Chapter 3, Displaying Content through Views, explores how to use Views to create different ways to list and display your content in Drupal.

Chapter 4,Extending Drupal, introduces how to write a module for Drupal, the building blocks of functionality in Drupal.

Chapter 5, Frontend for the Win, covers how to create a theme, work with the new templating system Twig, and harness Drupal’s responsive design features.

Chapter 6, Creating Forms with the Form API, explains how to work with Drupal’s Form API to create custom forms for collecting data.

Chapter 7, Plug and Play with Plugins, introduces plugins, one of the newest components in Drupal. This chapter walks through developing the plugin system to work with fields.

Chapter 8, Multilingual and Internationalization, introduces the features provided by Drupal 8 to create an internationalized website, supporting multiple languages for content and administration.

Chapter 9, Configuration Management - Deploying in Drupal 8, explains the configuration management system, new to Drupal 8, and how to import and export site configurations.

Chapter 10, The Entity API, dives into the Entity API in Drupal, allowing you to create custom configuration and content entities.

Chapter 11, Off the Drupalicon Island, explains how Drupal allows embracing the mantra of "proudly built elsewhere" and including third-party libraries with your Drupal site.

Chapter 12, Web Services, shows how to turn your Drupal 8 site into a web services API provider through a RESTful interface.

Chapter 13, The Drupal CLI, explores working with Drupal 8 through two command-line tools created by the Drupal community: Drush and Drupal Console.

What you need for this book

In order to work with Drupal 8, and to run the code examples found in this book, the following software will be required:

Web server software stack:

Web server: Apache (recommended), Nginx, or Microsoft IIS

Database: MySQL 5.5 or MariaDB 5.5.20 or higher

PHP: PHP 5.5.9 or higher

The first chapter details all of these requirements, and includes a recipe highlighting an out-of-the-box development server setup.

You will also need a text editor; the following is a suggestion of popular editors and IDEs:

Atom.io editor,

https://atom.io/

Visual Code Studio,

https://code.visualstudio.com/

PHPStorm (specific Drupal integration),

https://www.jetbrains.com/phpstorm/

Vim with Drupal configuration,

https://www.drupal.org/project/vimrc

Your operating system’s default text editor or command-line file editors

Who this book is for

This book is for those have been working with Drupal, such as site builders, backend and frontend developers, and who are eager to see what awaits them when they start using Drupal 8.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors .

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.

Hover the mouse pointer on the

SUPPORT

tab at the top.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box.

Select the book for which you're looking to download the code files.

Choose from the drop-down menu where you purchased this book from.

Click on

Code Download

.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for Windows

Zipeg / iZip / UnRarX for Mac

7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Drupal-8-Development-Cookbook-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

Up and Running with Drupal 8

In this chapter, we will get introduced to Drupal 8 and cover the following recipes:

Installing Drupal

Using a distribution with Drupal

Installing modules and themes

Using multisites in Drupal 8

Tools for setting up an environment

Running tests: Simpletest and PHPUnit

Introduction

This chapter will kick off with an introduction to installing a Drupal 8 site. We will walk through Drupal's interactive installer. We will cover installing Drupal using a command-line tool called Drush. Drupal provides two installation types: standard and minimal. Throughout this book, we will use the standard installation.

Once we have installed our Drupal 8 site, we will cover the basics of extending Drupal. We will discuss using distributions and installing contributed projects, such as modules and themes. We will also include uninstalling modules, as the process for uninstalling modules has changed in Drupal 8.

This book will involve a hands-on example for working with Drupal 8, and this chapter will provide information on setting up a local development environment. This chapter will also provide recipes on how to set up a Multisite installation in Drupal 8 and run the available test suites.

Before we get started, you should install Composer. Composer is the de facto package management tool for PHP. In case you are unfamiliar with Composer, it is just like using Gems for Ruby, npm for Node.js, and Bower for frontend libraries. Go to the Composer documentation to learn how to install Composer globally on your system:

Linux / Unix / macOS:

https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx

Windows:

https://getcomposer.org/doc/00-intro.md#installation-windows

Installing Drupal

There are many different methods to download Drupal and install it. In this recipe, we will focus on downloading Drupal from https://www.drupal.org/ and setting it up on a basic Linux, Apache, MySQL, or PHP (LAMP) server.

In this recipe, we will set up the files for Drupal 8 and step through the installation process.

Getting ready

Before we start, you will need a development environment that meets the new system requirements for Drupal 8:

Apache 2.0 (or higher) or Nginx 1.1 (or higher) web server

PHP 5.5.9 or higher, but PHP 5.6 or PHP 7 is recommended, as PHP 5.5 has reached its end-of-life support

MySQL 5.5 or MariaDB 5.5.20 for your database

You will need a user with privileges to create databases or a created database with a user who has privileges to make tables in that database.

Access to upload or move files to the server

While a default installation of PHP

will

work with Drupal, it does require certain PHP extensions, such as mbstring. Check out

https://www.drupal.org/requirements/php

for up-to-date

requirement

information.

Drupal 8 ships with Symfony (https://symfony.com/) components. One of the new dependencies in Drupal 8, to support the Symfony routing system, is the Drupal Clean URL functionality. If the server is using Apache, ensure that mod_rewrite is enabled. If the server is using Nginx, the ngx_http_rewrite_module must be enabled.

We will download Drupal 8 and place its files in your web server's document root. This is the /var/www folder. If you used a tool, such as XAMPP, WAMP, or MAPP, consult the proper documentation to know your document root.

For full system requirements for Drupal 8, check out https://www.drupal.org/docs/8/system-requirements/. The Drupal.org documentation is currently being migrated. Also, review the Drupal 7 requirements page on https://www.drupal.org/docs/7/system-requirements/overview, which highlights Drupal 8 items, as well.

How to do it...

We need to follow these steps to install Drupal 8:

First, we will need to navigate to

https://www.drupal.org/download

and download the latest release of Drupal 8.x. You can find the most recent and recommended release on the

https://www.drupal.org/project/drupal

page for Drupal 8 (8.3.1, 8.4.0, and so on). Extract the archive and place the files in your document root as the

drupal8

folder

:

Open your browser and visit your web server, for example,

http://localhost/drupal8

, which will then take you to the Drupal installation wizard. You will land on the new multilingual options install screen. Select your language and click on

Save and continue

:

On the next screen, select the default

Standard

option for the installation profile. This will provide us with a standard configuration with the most commonly used modules installed.

The next step will verify your system requirements. If your system does not have any reportable issues, the screen will be skipped. If you do have any requirement conflicts, you can resolve them and click on the button to try again.

If you have requirement issues, the installer will report the specific issues. Nearly every requirement will link to aDrupal.orghandbook page with solution steps.

Enter the database information for Drupal. In most cases, you will only need to supply the username, password, and database name and leave others as defaults. If your database does not exist, the installer will attempt to create the database:

See the There's more... section of this recipe for information on setting up your database.

Your Drupal 8 site will begin installing. When it is done installing the base modules, you will be taken to a site configuration screen.

 

The configure site form provides the base configuration for your Drupal site. Enter your site name and the email address for the site. The site email will be used to send administrative notifications and has the originating email for outgoing emails from the Drupal site. This form allows you to set regional information regarding the country and time zone of the site. Setting the timezone ensures that time values display correctly.

Fill in the site maintenance account information, also known as user 1, which acts in a similar way to the root on Unix-based systems. The site maintenance account is crucial. As stated, this acts as the first user and is given the user ID of 1. In Drupal, the user with the user ID of 1 often can bypass permission checks automatically and have global access.

Enter the site's regional information, and check whether the site should check for updates available for modules enabled and Drupal itself. By checking for updates automatically, your site will report anonymous usage statistics to

Drupal

along with providing a summary of your version status. You have the option to also opt-in for the site to email you notifications of new releases, including security releases.

When the information is satisfied, click on

Save and continue

, and congratulations, you installed Drupal! The next screen will provide you a link to your installed Drupal site.

How it works...

The Drupal installation process will provide a Drupal installation for the selected language and install modules and configuration based on the installation profile (standard or minimal in this recipe.)

When you visit the installer, it reads the language code from the browser. With this language code, it will then select a supported language. If you choose a non-English installation, the translation files will be automatically downloaded from https://localize.drupal.org/. Previous versions of Drupal did not support automated multilingual installs. More on multilingual will be covered in Chapter 8, Multilingual and Internationalization.

The installation profile instructs Drupal what modules to install by default. Contributed install profiles are termed distributions; we will discuss this more in the next recipe.

When verifying requirements, Drupal checks application versions and PHP configurations. For example, if your server has the PHP Xdebug (https://xdebug.org) extension installed, the minimum max_nesting_level must be 256 or else Drupal will not be installed (https://www.drupal.org/node/2393531).

There's more...

The Drupal installation process is straightforward, but there are a few things worth discussing.

Creating a database user and a database

As mentioned earlier, to install Drupal, you will need to have access to a database server (or the ability to create one) and an existing database (or the ability to create one). This process will depend on your environment setup.

If you are working with a hosting provider, there is more than likely a web-based control panel. This should allow you to create databases and users. Refer to your hosting provider's documentation for more information on this topic.

If you are using phpMyAdmin (https://www.phpmyadmin.net/) on your server, often installed by MAMP, WAMP, and XAMPP, and have root access, you can create your databases and users by following these steps:

Sign in to phpMyAdmin as the root user.

Click on

Add a new User

from the bottom of the privileges page.

Fill in the user's information.

Select to create a database for the user with all privileges granted.

You can now use that user's information to connect Drupal to your database.

If you do not have a user interface but have a command-line access, you can set up your database and user using the MySQL command line. These instructions can be found in the core/INSTALL.mysql.txt file. From the command line of your site, perform the following:

Log in to MySQL:

$ mysql -u username -p

Create the database; you will use the following command to create the

my_database

database:

$ CREATE DATABASE my_database CHARACTER SET utf8 COLLATE utf8_general_ci;

Create a new user to access the database:

$ CREATE USER username@localhost IDENTIFIED BY 'password';

Grant the new user permissions on the database, as follows:

$ GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES ON databasename.* TO 'username'@'localhost' IDENTIFIED BY 'password';

If you are installing Drupal with a PostgreSQL or SQLite database, check out the appropriate installation instructions, either INSTALL.pgsql.txt or INSTALL.sqlite.txt.

Database prefixes

Drupal, like other content management systems, allows you to prefix its database tables from the database set-up form. This prefix will be placed before table names to help make them unique. Although it is not recommended, this would allow multiple installations to share one database. Utilizing table prefixes can, however, provide some level of security through obscurity since the tables will not be their default names:

Downloading and installing with Drush

You may also install Drupal using the PHP command-line tool, Drush. Drush is a command-line tool created by the Drupal community and must be installed if you wish to use it. Drush is covered in Chapter 13, The Drupal CLI.

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.

The pm-download command will download packages from Drupal.org. The site-install command will allow you to specify an installation profile and other options to install a Drupal site. The installation steps in this recipe could be run through Drush, as follows:

$ cd /path/to/web

$ drush pm-download drupal-8 drupal8

$ cd drupal8

$ drush site-install standard -locale=en-US --account-name=admin --account-pass=admin [email protected] -db-url=mysql://user:pass@localhost/database

We used Drush to download the latest Drupal 8 and place it in a folder named drupal8. Then, the site-install command instructs Drush to use the standard install profile, configure the maintenance account, and provide a database URI string so that Drupal can connect to its database.

Using Composer to create a Drupal site

You can download Drupal using Composer, the de facto PHP package manager. The preferred method is to use the Drupal Composer project template provided by the community.

To build your Drupal 8 site, run the following commands:

$ cd /path/to/document/root

$ composer create-project drupal-composer/drupal-project drupal8 --stability dev

Wait for the commands to finish--it may take some time, as it downloads all the required dependencies. You can feel free to grab a coffee (the first time takes a while; it primes caches. Have faith, it will be much faster the next time.)

When finished, you will find a different directory structure inside your drupal8 directory. The vendor directory contains all third-party PHP libraries, and the web directory contains your Drupal 8 site. You will need to modify your web server to use the web directory as the new docroot within your drupal8 directory.

The project and its details can be found at https://github.com/drupal-composer/drupal-project, along with its full documentation.

Security updates

If you choose to disable the update options, you will have to check manually for module upgrades. While most upgrades are for bug fixes or features, some are for security updates. It is highly recommended that you subscribe to the Drupal security team's updates. These updates are available on Twitter at @drupalsecurity (https://twitter.com/drupalsecurity) or the feeds onhttps://www.drupal.org/security.

See also

For more on multilingual, check out

Chapter 8

,

Multilingual and Internationalization

For more on using the command line and Drupal,

check out

Chapter 13

,

The Drupal CLI

Check out

the

Drupal.org

handbook on installing Drupal at

https://www.drupal.org/documentation/install

Check out

the Drupal.org handbook that discusses Drupal 8 and Composer at

https://www.drupal.org/docs/develop/using-composer/using-composer-with-drupa

l

Check out more information on Drush site install at

https://drushcommands.com/drush-8x/core/site-install/

Using a distribution with Drupal

Why would you want to use a distribution? A distribution is a contributed installation profile that is not provided by Drupal core. Distributions provide a specialized version of Drupal with specific installed modules and themes along with specific configurations (content types, and blocks.) On Drupal.org, when you download an installation profile, it not only includes the profile and its modules but a version of Drupal core, hence the name distribution. You can find a list of all Drupal distributions at https://www.drupal.org/project/project_distribution.

How to do it...

We will follow these steps to download a distribution to use as a customized version of Drupal 8:

Download a distribution from

Drupal.org

. For this recipe, let's use the Demo Framework provided by Acquia at

https://www.drupal.org/project/df

.

Select the recommended version for the 8.x branch.

Extract the folder contents to your web server's document root--you'll note that there is Drupal core; within the

profiles

folder, there's the installation profile's folder--

df

.

Due to current Drupal.org packaging limitations, there is a manual step that you will need to run in order to install additional dependencies. Run the following command using your terminal inside of the extracted contents:

$ composer require "commerceguys/intl: ~0.7" "commerceguys/addressing: ~1.0" "commerceguys/zone: ~1.0" "embed/embed: ~2.2

Install Drupal as you would normally, by navigating to your Drupal site in your browser.

Follow the installation instructions in the site to install the distribution.

How it works...

Installation profiles work by including additional modules that are part of the contributed project realm or custom modules. The profile will then define them as dependencies to be installed with Drupal. When you select an installation profile, you are instructing Drupal to install a set of modules on installation.

Demo Framework declares itself as an exclusive installation profile. Distributions that declare this are automatically selected and assumed to be the default installation option. The exclusive flag was added with Drupal 7.22 to improve the experience of using a Drupal distribution (http://drupal.org/node/1961012).

There's more...

Distributions provide a specialized version of Drupal with specific feature sets, but there are a few items worth discussing.

Makefiles

The current standard for generating a built distribution is the utilization of Drush and makefiles. Makefiles allow a user to define a specific version of Drupal core and other projects (such as themes, modules, and third-party libraries) that will make up a Drupal code base. It is not a dependency management workflow, like Composer, but is a build tool.

If you take a look at the Demo Framework's profile folder, you will see drupal-org.make and drupal-org-core.make. These are parsed by the Drupal.org packager to compile the code base and package it as a .zip or .tar.gz, like the one you downloaded.

Installing with Drush

As discussed in the first recipe'sThere's more...section, you can install a Drupal site through the Drush command-line tool. You can instruct Drush to use a specific installation profile by providing it as the first argument.

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.

The following command would install the Drupal 8 site using the Demo Framework:

$ cd /path/to/drupal8

$ drush pm-download df

$ drush site-install df -db-url=mysql://user:pass@localhost/database

Using Composer

Currently, Drupal.org does not package distributions using Composer, which is why there was an extra step to add dependencies when installing the distribution. Many distributions provide project templates to make scaffolding projects simpler.

For example, the following command will set up a Demo Framework site with docroot as the directory for the web server document root, which contains Drupal 8:

$ composer create-project acquia/df-project df

The project template is available on Acqua's GitHub at https://github.com/acquia/df-project/.

Another distribution, Open Social, provides a template of its own:

$ composer create-project goalgorilla/social_template

The project template is available at https://github.com/goalgorilla/social_template.

See also...