Matt Glaman-Drupal 8 Development Cookbook-Packt Publishing (2016) 1.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Drupal 8 Development Cookbook Over 60 hands-on recipes that get you acquainted with Drupal 8's features and help you harness its power Matt Glaman BIRMINGHAM - MUMBAI Drupal 8 Development Cookbook Copyright © 2016 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 Production reference: 1040316 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78588-147-3 www.packtpub.com Credits Author Project Coordinator Matt Glaman Shweta H Birwatkar Reviewer Proofreader Todd Zebert Commissioning Editor Indexer Amarabha Banerjee Tejal Daruwale Soni Acquisition Editor Graphics Manish Nainani Jason Monteiro Content Development Editor Production Coordinator Deepti Thore Manu Joseph Technical Editor Cover Work Naveenkumar Jain Manu Joseph Copy Editors Ting Baker Rashmi Sawant About the Author Matt Glaman is a developer at Commerce Guys. He is an open source developer who has been working with Drupal since 2013. He has also been developing web apps for many years prior to this. Since then, he has contributed to over 60 community projects, including being a co-maintainer of Drupal Commerce. While mostly focusing on Drupal and PHP development, he created https://contribkanban.com, an AngularJS application, to provide Kanban boards for the Drupal community to collaborate with. 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. and kicking off my Drupal career. I would also like to thank my mentors Drupal community! About the Reviewer Todd Zebert has been involved with Drupal since version 6. He is a full-stack web developer Miles. He has also been a technical reviewer for Developing with Drush, Packt Publishing. He has a diverse background in technology, including infrastructure, network engineering, project management, and IT leadership. His experience with web development started with the original Mosaic graphical web browser, SHTML/CGI, and Perl. He's an entrepreneur involved with the Los Angeles start-up community. He's a believer in volunteering, open source, the Maker movement, and contributing back. He's also an advocate for Science, Technology, Engineering, Art, and Math (STEAM) education. I'd like to thank the Drupal community, which is like no other. Finally, I'd like to thank my pre-teen son with whom I get to share my interest in technology and program video games and microcontrollers. www.PacktPub.com eBooks, discount offers, and more Did you know that Packt offers eBook versions of every book published, with PDF and ePub 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. TM https://www2.packtpub.com/books/subscription/packtlib library. Here, you can search, access, and read Packt's entire library of books. 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 Table of Contents Preface v Chapter 1: Up and Running with Drupal 8 1 Introduction 1 Installing Drupal 2 Using a distribution 8 Installing modules and themes 10 Using multisites in Drupal 8 14 Tools for setting up an environment 16 Running Simpletest and PHPUnit 18 Chapter 2: The Content Authoring Experience 23 Introduction 23 Adding and editing content 27 Creating a menu and linking content 30 Providing inline editing 32 Creating a custom content type 34 Customizing the form display of a node 40 Customizing the display output of a node 43 Chapter 3: Displaying Content through Views 47 Introduction 47 Listing content 48 Editing the default admin interfaces 52 Creating a block from a View 55 Utilizing dynamic arguments 58 Adding a relationship in a View 61 Providing an Entity Reference result View 66 i Table of Contents Chapter 4: Extending Drupal 69 Introduction 69 Creating a module 70 Using Features 2.x 90 Chapter 5: Frontend for the Win 97 Introduction 97 Creating a custom theme based on Classy 98 Using the new asset management system 102 Twig templating 109 Using the Breakpoint module 113 Using the Responsive Image module 115 Chapter 6: Creating Forms with the Form API 121 Introduction 121 Creating a form 122 Using new HTML5 elements 128 Validating form data 132 Processing submitted form data 136 Altering other forms 140 Chapter 7: Plug and Play with Plugins 145 Introduction 145 Creating blocks using plugins 146 Creating a custom plugin type 169 Chapter 8: Multilingual and Internationalization 181 Introduction 181 Translating administrative interfaces 182 Translating content 191 Creating multilingual views 195 Chapter 9: Introduction 201 ii Table of Contents Chapter 10: The Entity API 225 Introduction 225 Creating a content entity type 237 Creating a bundle for a content entity type 247 Implementing custom access control for an entity 257 Providing a custom storage handler 264 Creating a route provider 267 Chapter 11: Off the Drupalicon Island 273 Introduction 273 Implementing and using a third-party JavaScript library 274 Implementing and using a third-party CSS library 279 Implementing and using a third-party PHP library 284 Using Composer manager 287 Chapter 12: Web Services 293 Introduction 293 Enabling RESTful interfaces 294 Using GET to retrieve data 299 Using POST to create data 302 Using PATCH to update data 307 Using Views to provide custom data sources 310 Authentication 313 Chapter 13: The Drupal CLI 319 Introduction 319 Rebuilding cache in Console or Drush 320 Using Drush to interact with the database 321 Using Drush to manage users 325 Scaffolding code through Console 327 Making a Drush command 330 Making a Console command 335 Index 341 iii Preface Drupal is a content management system used to build websites for small businesses, e-commerce, enterprise systems, and many more. Created by over 4,500 contributors, Drupal 8 provides many new features for Drupal. Whether you are new to Drupal, or an experienced Drupalist, Drupal 8 Development Cookbook contains recipes that help you immerse yourself in what Drupal 8 has to offer. What this book covers Chapter 1, Up and Running with Drupal 8, covers the requirements to run Drupal 8, walks you through the installation process, and extends 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, explains how to work with Drupal's Form API to create custom forms to collect data. Chapter 5, Frontend for the Win, teaches you 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, teaches you how to write a module for Drupal, the building blocks of functionalities in Drupal. Chapter 7, Plug and Play with Plugins, introduces plugins, one of the newest components in Chapter 8, Multilingual and Internationalization, introduces features provided by Drupal 8 administration. v Preface Chapter 9, Chapter 10, The Entity API, dives into the Entity API in Drupal, allowing you to create custom Chapter 11, Off the Drupalicon Island, explains how Drupal allows you to embrace the mantra of proudly built elsewhere and includes third-party libraries in your Drupal site. Chapter 12, Web Services, shows you 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 Chapter 1, Up and Running with Drupal 8, details all of these requirements and includes a recipe that highlights an out of the box development server setup. Atom.io editor: https://atom.io/ https://www.jetbrains.com/ phpstorm/ https://www.drupal.org/project/vimrc Who this book is for This book is for those who have been working with Drupal, such as site builders and backend and frontend developers, and who are eager to see what awaits them when they start using Drupal 8.