Drupal 9 Module Development Danielsipos Drupal 9 Module Development – Third Edition – Third Edition
Total Page:16
File Type:pdf, Size:1020Kb
Drupal 9 Module Development Sipos Daniel Edition Third – Development 9 Module Drupal – Third Edition With its latest release, Drupal 9, the popular kinds of data storage, this Drupal guide will Drupal 9 Module open source CMS platform has been updated demonstrate how to create custom entities with new functionalities for building complex and fi eld types and leverage the Database API Drupal apps with ease. This third edition of for lower-level database queries. You'll also the Drupal Module Development guide covers learn how to introduce JavaScript into your these new Drupal features, helping you to stay module, work with various fi le systems, and on top of code deprecations and the changing ensure that your code works on multilingual Development architecture with every release. sites. Finally, you'll work with Views, create automated tests for your functionality, and The book starts by introducing you to the write secure code. Drupal 9 architecture and its subsystems before showing you how to create your fi rst By the end of the book, you'll have learned module with basic functionality. You'll explore how to develop custom modules that can Third Edition the Drupal logging and mailing systems, learn provide solutions to complex business how to output data using the theme layer, and problems, and who knows, maybe you'll work with menus and links programmatically. even contribute to the Drupal community! Once you've understood the different Get up and running with building powerful Drupal modules and applications Things you will learn: • Develop custom Drupal 9 modules for • Display data and content in a clean and your applications secure way using the theme system • Master different Drupal 9 subsystems • Test your business logic to prevent and APIs regression • Model, store, manipulate, and process data • Stay ahead of the curve and write PHP for effective data management code by implementing best practices www.packt.com Daniel Sipos www.packt.com Foreword by Antonio De Marco, Co-founder, Nuvole Drupal 9 Module Development Third Edition Get up and running with building powerful Drupal modules and applications Daniel Sipos BIRMINGHAM—MUMBAI Drupal 9 Module Development Third Edition Copyright © 2020 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 efort 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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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. Commissioning Editor: Pavan Ramchandani Acquisition Editor: Rohit Rajkumar Senior Editor: Sof Rogers Content Development Editor: Keagan Carneiro Technical Editor: Shubham Sharma Copy Editor: Safs Editing Project Coordinator: Kinjal Bari Proofreader: Safs Editing Indexer: Rekha Nair Production Designer: Aparna Bhagat First published: October 2017 Second edition: March 2019 Tird edition: August 2020 Production reference: 1130820 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80020-462-1 www.packt.com Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? • Spend less time learning and more time coding with practical eBooks and videos from over 4,000 industry professionals • Improve your learning with Skill Plans built especially for you • Get a free eBook or video every month • Fully searchable for easy access to vital information • Copy and paste, print, and bookmark content Did you know that Packt ofers eBook versions of every book published, with PDF and ePub fles available? You can upgrade to the eBook version at packt.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.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and ofers on Packt books and eBooks. Foreword My frst experience with Drupal was in late 2006, when I started using it to build the site of an NGO I was volunteering for. What was making Drupal stand out then is what, in many ways, still sets it apart today: a large set of ready-to-use building blocks, backed up by a community of dedicated maintainers. Over the years, that community has taken on some of the most complex problems for a content management system. Tings like extensible data modeling, multilingualism, a performant caching system or safe deployment of confguration changes are not just Drupal specifcities but, in many ways, they are what defnes the content management industry in the frst place. Tanks to their relentless eforts, people have fought for the heart of that community in order to have their own proposals, their own take on those defning problems, being adopted by the larger audience. We have all witnessed a few of those struggles and, in one way or another, rooted for one or the other school of thought. I remember championing across several DrupalCons for an efcient, features-driven deployment process myself, until the Confguration Management Initiative came along. I was struck by how far that looked from what we were advocating, only to discover that the new confguration management system was its natural, most powerful, evolution. And this book is about just that: evolution. As it's undeniable that Drupal 8 was a rough jump ahead from Drupal 7, Drupal 9 is Drupal 8's natural, most powerful, evolution. And I can't think of anyone better suited to walk you through it than Daniel. Already the author of Drupal 8 Module Development, in this excellent book, Daniel covers in great detail all of Drupal 9's sub-systems, from the unique perspective of a long-time, prominent, Drupal community member. Whether you are a seasoned Drupal developer, or you are taking your frst steps in this powerful system, the content of this book truly is an essential travel companion for any modern Drupal developer. Antonio De Marco Co-founder, Nuvole Contributors About the author Daniel Sipos is a senior web developer specializing in Drupal. He's been working with Drupal sites since version 6, and started out, like many others, as a site builder. He's a self-taught programmer with many years' experience working professionally on complex Drupal projects. In his spare time, he runs Webomelette, a Drupal website where he writes technical articles, tips, and techniques related to Drupal development. I would like to acknowledge my colleagues and friends who have kept and continue to keep me on my toes with everything related to Drupal. Tey are, in no particular order, Antonio De Marco, Francesco Sardara, and Hernani Borges de Freitas. About the reviewers Modesto Caballero has been developing sites with Drupal since 2007. In this time, he's worked for clients including Sega, Nestle, and Nokia. At the time of this writing, he is working as a Drupal consultant in the UK. In his free time, he enjoys spending time with his family, knitting, and talking about himself in the third person. Neerav Bipin Mehta is an experienced Drupal architect and developer. He runs Red Crackle, a web development frm that specializes in Drupal and ReactJS. He and his team have years of experience architecting Drupal solutions for clients such as Salesforce, VMWare, eBay, PayPal, and Databricks. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors. packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specifc hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Developing for Drupal 9 Introducing Drupal (for Databases and MySQL 5 developers) 2 The web server 5 How did we get to Drupal 9? 2 Drupal architecture 6 Developing for Drupal 4 Drupal's major subsystems 10 Tools for developing in Drupal 15 Technologies that drive Drupal 5 PHP 5 Summary 18 2 Creating Your First Module Creating a module 20 Blocks 47 Your frst hook implementation 22 Our frst block plugin 47 Route and controller 25 Block confguration 50 Services 29 Working with links 52 Using services in Drupal 33 The URL 52 Injecting the service into our Controller 33 The link 53 Invoked Controllers 36 Which way to link? 54 The Form API 36 Event Dispatcher and redirects 54 Altering forms 41 Redirecting from a Controller 54 Custom submit handlers 43 Redirecting from a subscriber 55 Rendering forms 44 Dispatching events 60 Service dependencies 45 Summary 64 ii Table of Contents 3 Logging and Mailing Logging 66 Altering someone else's emails 79 The Drupal logging