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
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. €18.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781782168775
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. €18.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 106.97
Drupal 8 Module Development
€36.99
Drupal 8 Development Cookbook
€36.99
Drupal 8 Blueprints
€32.99
Total 106.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.