Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Drupal 8 Module Development
Drupal 8 Module Development

Drupal 8 Module Development: Build and customize Drupal 8 modules and extensions efficiently

eBook
$27.98 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Drupal 8 Module Development

Creating Your First Module

Now that we have covered some of the introductory aspects of Drupal 8 module development, it’s time to dive right into the meat of what we are doing here--module creation.

Here are some of the important topics that we will cover in this chapter:

  • Creating a new Drupal 8 module--the files that are necessary to get started
  • Creating a route and controller
  • Creating and using a service
  • Creating a form
  • Creating a custom block
  • Working with links
  • Using the Event Dispatcher

Concretely, in this chapter, we will create a new custom module called Hello World. In this module, we will define a route that maps to a Controller and that outputs the age-old programming message. So, this will be our first win.

Next, we will define a service that our Controller will use to pimp up our message. After all, we don't want the same message presented to the user...

Creating a module

Creating a simple Drupal 8 module is not difficult. You only need one file to get it recognized by the core installation and to be able to enable it. In this state, it won't do much, but it will be installable. Let's first take a look at how to do this, and then we will progressively add meat to it in order to achieve the goals set out at the beginning of the chapter.

Custom Drupal 8 modules typically belong inside the /custom directory of the /modules folder found inside the root Drupal installation. You would put contributed modules inside a /contrib directory instead, in order to have a clear distinction. This is a standard practice, so that is where we will place our custom module, called Hello World.

We will start by creating a folder called hello_world. This will also be the module's machine name used in many other places. Inside, we will...

Using services in Drupal 8

Before we go and use our service in the Controller we created, let's take a breather and run through the ways you can make use of services once they are registered.

There are essentially two ways--statically and injected. The first is done by a static call to the Service Container, whereas the second uses dependency injection to pass the object through the constructor (or in some rare cases, a setter method). However, let's check out how, why, and what is the real difference.

Statically, you would use the global Drupal class to instantiate a service:

$service = \Drupal::service('hello_world.salutation');

This is how we use services in the .module files and classes, which are not exposed to the Service Container and into which we cannot inject--although the latter instances are rare. A few popular services also have shorthand methods...

Blocks

Custom blocks in Drupal 8 are plugins. Finally, we encounter our first plugin type. For the sake of full disclosure, the content blocks that you create through the UI to place in a region and the custom blocks that are placed in a region are content entities. So, the block system is a good example of how entities and plugins work hand in hand in Drupal 8, and not to make matters too complex for you, configuration entities (about which we will learn in a later chapter) also play a big role here.

The block system in Drupal 8 is a great shift from its predecessor. Before, you had to implement two obligatory hooks plus two optional hooks if you wanted the block to have a configuration, and the latter was always saved somewhere that had nothing to do with the block itself. In Drupal 8, we work with a simple plugin class that can be made container-aware (that is, we can inject...

Working with links

One of the principle characteristics of a web application is the myriad of links between its resources. They are in fact the glue that brings it together. So, in this section, I want to show you a few common techniques used while working with links programmatically in Drupal 8.

There are two main aspects when talking about link building in Drupal--the URL and the actual link tag itself. So, creating a link involves a two-step process that deals with these two, but can also be shortened into a single call via some helper methods.

The URL

URLs in Drupal 8 are represented with the Drupal\Core\Url class, which has a number of static methods that allow you to create an instance. The most important of these is...

Event Dispatcher and redirects

A common thing you'll have to do as a module developer is to intercept a given request and redirect it to another page, and more often than not, this will have to be dynamic, depending on the current user or other contextual info. Drupal 7 developers know very well that this has always been an easy task. Simply implement hook_init(), which gets called on each request and then use the famous drupal_goto() function. This, however, is no longer the case in Drupal 8. What we have to do now is subscribe to the kernel.request event (remember this from the preceding chapter?) and then change the response directly. However, before seeing an example of this, let's take a look at how we can perform a simpler redirect from within a Controller.

Redirecting...

Summary

In this chapter, we covered a great deal of info about the things you need to know when developing Drupal 8 modules. The first thing we did was create our very own module skeleton that can be installed on a Drupal 8 site. Then, we saw how to create a new page at a specific path (route) and show some basic data on that page. Nothing too complex, but enough to illustrate one of the most common tasks you will do as a module developer. We then took that to new level and abstracted the logic for that data calculation into a service. Not only that, but we also saw how we can use that service, and, more importantly, how we should use it. Next, we saw how we can work with the Form API in Drupal 8 to allow administrators to add some configuration to the site. A very important takeaway here was also that the Form API page in Drupal 8 will prove invaluable going forward because you...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Unleash the power of Drupal 8 programming by creating efficient modules and extensions
  • Explore a plethora of Drupal 8 APIs and get the best out of them using the power of PHP coding
  • Learn to implement efficient data management and data security by creating dedicated modules for it.

Description

Drupal is an open source web-based content management system (CMS) that can be used for building anything from simple websites to complex applications. It enables individuals and organizations to build platforms that engage users and deliver the right content at the right time. Drupal 8 is an exciting new development in the Drupal community. However, the differences from the previous version are substantial and this can put quite some pressure on Drupal 7 developers that need to catch up. This book aims to help such developers in getting up to speed with Drupal 8 module development. The book first introduces you to the Drupal 8 architecture and its subsystems before diving into creating your first module with basic functionality. Building upon that, you will cover many core APIs and functionalities available to module developers. You will work with the Drupal logging and mailing systems, learn how to output data using the theme layer and work with menus and links programmatically. Then, you will learn how to work with different kinds of data storages, create custom entities, field types and leverage the Database API for lower level database queries. Moreover, you will learn about the Drupal 8 access system and caching layer as well as the APIs used for data processing (queues and batches). You will further see how to introduce javascript into your module, work with the various file systems and ensure the code you write works on multilingual sites. Finally, you will learn how to programmatically work with Views, write automated tests for your functionality and also write secure code in general. By the end of the book, you will have learned how to develop your own custom module from scratch that can help solve a small problem or even provide complex functionality. And who knows, maybe you’ll even contribute it back to the Drupal community.

Who is this book for?

The primary target of this book is Drupal developers who want to learn how to write modules and carry out development in Drupal 8. It is also intended for Drupal site builders and PHP developers who have basic Object Oriented Programming skills. A little bit of Symfony experience is helpful but not mandatory.

What you will learn

  • Write a Drupal 8 module with custom functionality and hook into various extension points
  • Master numerous Drupal 8 sub-systems and APIs
  • Model, store, and manipulate data in various ways and for various purposes
  • Display data and content in a presentable and secure way
  • Learn about the theme system and render arrays

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 27, 2017
Length: 566 pages
Edition : 1st
Language : English
ISBN-13 : 9781782168782
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Oct 27, 2017
Length: 566 pages
Edition : 1st
Language : English
ISBN-13 : 9781782168782
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 141.97
Drupal 8 Module Development
$48.99
Drupal 8 Development Cookbook
$48.99
Drupal 8 Blueprints
$43.99
Total $ 141.97 Stars icon
Banner background image

Table of Contents

18 Chapters
Developing for Drupal 8 Chevron down icon Chevron up icon
Creating Your First Module Chevron down icon Chevron up icon
Logging and Mailing Chevron down icon Chevron up icon
Theming Chevron down icon Chevron up icon
Menus and Menu Links Chevron down icon Chevron up icon
Data Modeling and Storage Chevron down icon Chevron up icon
Your Own Custom Entity and Plugin Types Chevron down icon Chevron up icon
The Database API Chevron down icon Chevron up icon
Custom Fields Chevron down icon Chevron up icon
Access Control Chevron down icon Chevron up icon
Caching Chevron down icon Chevron up icon
JavaScript and the Ajax API Chevron down icon Chevron up icon
Internationalization and Languages Chevron down icon Chevron up icon
Batches, Queues, and Cron Chevron down icon Chevron up icon
Views Chevron down icon Chevron up icon
Working with Files and Images Chevron down icon Chevron up icon
Automated Testing Chevron down icon Chevron up icon
Drupal 8 Security Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(8 Ratings)
5 star 62.5%
4 star 12.5%
3 star 12.5%
2 star 12.5%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Asiq Apr 24, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book if you want to learn Drupal8.
Amazon Verified review Amazon
James Danforth Feb 15, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent is an understatement. This book is allowing me to get up to speed on Drupal 8 development at least 10X faster than if I had to sort through all the Drupal 8 documentation on my own. The book is my train into a big Drupal territory. If you need to develop in version 8 and you need to get fast track information on how to do it well, fast, this is the book for you. The book vastly flattens out the early part of Drupal 8's learning curve for those without a lot of experience with it. I would buy it again for sure.
Amazon Verified review Amazon
Amazon Customer Jul 19, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Very good book for Drupal developers.
Amazon Verified review Amazon
M.G. Aug 14, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Throughly conceived, and brilliantly executed. A really good resource for an in-depth look at Drupal 8's many aspects. Use it for reference all the time.
Amazon Verified review Amazon
William A. Kudrle Jun 09, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am surprised that this has not gotten more reviews. I have not found another book that is as thorough and in-depth about how Drupal 8 works. It goes over custom module development, as the title implies, but it also goes into depth about how the internals of Drupal 8 work as well. Many improvements have been made to the internals of Drupal 8, but it is difficult to know many of them unless you have a text like this to guide you. I liken this book to what Pro Drupal Development by John Van Dyke was for Drupal 6. Reading through Van Dyke's book was what gave me the ability to actually create custom Drupal 6 modules. This book is doing that for me for Drupal 8. The only criticism that I would have is that it would be nice to have more details about how to use the Drupal console to create custom modules and plugins. That may be too much of a moving target, but in practical terms just getting the basic modules and plugins to be generated with Drupal console can be a confidence boost and provide some understanding from the immediate feedback of the basic custom code.I highly recommend this book if you really want to understand what is going in with Drupal 8 and/or intend to do any custom programming.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.