Tools like PHP_Codesniffer (phpcs) can be used to help ensure your code adheres to Drupal's coding standards. As a module developer, you should use phpcs and its Drupal-specific rule sets on all custom module code.
In this tutorial, we'll:
- Learn about PHP_Codesniffer (phpcs).
- Install PHP_Codesniffer and the Drupal-specific rules.
- Use phpcs to lint our custom code.
By the end of this tutorial, you should be able to use PHP_Codesniffer to help automate the process of adhering to Drupal's coding standards.
Coding standards are vital in collaborative environments like Drupal. These guidelines ensure consistency, readability, and maintainability of the codebase. Drupal has its own coding standards that outline best practices and formatting guidelines, promote code quality, simplify code reviews, and enhance the project's overall health.
In this tutorial, we'll:
- Explore the use of coding standards in Drupal.
- Highlight basic practices and sources for detailed information.
By the end of this tutorial you should be able to explain why coding standards are important and where to find more info about Drupal's coding standards.
To use a hook in a Drupal module, we need to add a function with a specific naming convention to your module's MODULE_NAME.module file. Each hook has unique arguments and an expected return value. In this tutorial, we'll walk through the process of implementing a hook by adding end-user help text for the anytown module, which Drupal's administrative UI will display. The process we'll use here applies to any hook implementation.
In this tutorial, we'll:
- Locate the documentation for
hook_help()
. - Implement the hook in the anytown module.
- Verify our hook implementation.
By the end, you'll have implemented hook_help()
to display help text in the Drupal UI.
Update hooks in Drupal are used for executing database updates or applying configuration changes during module updates. They ensure these changes occur once and in the correct order.
In this tutorial, we'll:
- Explore the purpose of update hooks.
- Learn how to implement update hooks for database updates or configuration changes.
By the end of this tutorial, you should be able to understand the use case for update hooks and know how to get started implementing one.
It's a Drupal best practice to always use Drupal's internationalization utilities for any user interface strings in your code. This includes the PHP t()
function and StringTranslationTrait
trait, the Twig t
filter, and the JavaScript Drupal.t()
function. This makes it possible for our module's interface to be localized.
In this tutorial, we'll:
- Edit the
WeatherPage
controller and use thet()
method from theStringTranslationTrait
trait for all UI strings. - Update the weather-page.html.twig template file to use the Twig
t
filter. - Modify the JavaScript in our forecast.js code to use the
Drupal.t()
function for UI strings.
By the end of this tutorial you should be able to update the PHP, Twig, and JavaScript code in your module to ensure that any user interface strings they output are translatable.
Entities are the building blocks of Drupal's data structures. As module developers, the Entity API provides a way to manage custom data with minimal code. You'll use it when altering or enhancing existing content or when managing custom data sets. Instead of writing SQL, you'll be using the Entity API to manage data within a Drupal application.
In this tutorial, we'll:
- Define entities and their significance in Drupal.
- Distinguish between content entities and configuration entities.
- Explore entity-related terminology such as bundles, fields, annotations, plugins, and handlers.
By the end of this tutorial, you'll have a foundational understanding of the Entity API and how it's used for data management in Drupal.
Validation happens whenever an entity is created or updated, ensuring data integrity across form submissions, JSON:API requests, and direct entity object manipulation. Drupal's Entity Validation API, consists of constraints, validators, and their integration. As module developer, we'll use this API to enforce custom rules about what constitutes valid data.
In this tutorial, we'll:
- Learn about the roles of constraints and validators within Drupal's validation system.
- See how to create and integrate custom validation rules.
- Apply custom validation to an entity type that enforces specific data integrity rules.
By the end of this tutorial, you should be able to define new constraints and validators, and associate them with entity types.
We must validate user input entered in forms to ensure data integrity and security. In our Anytown module, we need to verify that the location input for the weather forecast API is a valid 5-digit ZIP code.
In this tutorial, we'll:
- Add a
validateForm()
method to the form controller. - Ensure the
location
field contains a 5-digit ZIP code. - Display an error if validation fails.
By the end of this tutorial, you'll know how to add custom validation logic to a form.
In the world of Drupal development, ensuring the security of custom code is paramount. Drupal's security standards and features offer a robust foundation, but developers must also adhere to security best practices to safeguard against vulnerabilities. From sanitizing output to prevent cross-site scripting (XSS) attacks to using Drupal's database abstraction layer to avert SQL injection, Drupal empowers developers to write secure code. But it's still up to you, the developer, to do so.
In this tutorial, we'll:
- Provide an overview of these practices.
- Introduces additional resources to increase your security expertise.
By the end of this tutorial, you should be able to articulate some of the common security risks and mitigation strategies for Drupal modules.
Concept: Caching
FreeCaching is an essential piece of website performance and user experience. Drupal's Cache API enables you as a module developer to specify cacheability information for data rendered through the Render API. By storing previously calculated data or page renderings, Drupal can skip complex backend processes for subsequent requests and deliver content faster. Drupal's Cache API provides services to store, retrieve, and invalidate cached data.
In this tutorial, we'll:
- Learn why caching is important.
- Cover key terms and concepts associated with the Cache API.
- Explore how and when custom modules can implement caching.
By the end of this tutorial, you should understand the core features of Drupal's Cache API and when to use them.
The logic for retrieving and processing a weather forecast is hard-coded into the WeatherPage
controller. To make our code more reusable, maintainable, and organized, we can refactor this code into a custom weather forecast API service.
In this tutorial, we'll:
- Define a PHP interface for a weather forecast API service and write an implementation of that service.
- Create a service definition file to inform Drupal about our new service and its dependencies.
- Refactor the
WeatherPage
controller to use the new service, which will clean up duplicate code.
By the end of this tutorial, you'll be able to define and use a custom service in a Drupal module.
We can enhance our site's performance by using Drupal's cache backend service to cache and reuse results from an external API request. We can cache the results of weather forecast API queries within a custom service. This will give us an opportunity to practice implementing Drupal's caching capabilities and optimize the performance of our module.
In this tutorial, we'll:
- Inject the cache backend service into our forecast API service class.
- Implement caching to store and reuse weather forecast API results locally.
By the end of this tutorial, you'll understand how to use Drupal's cache backend service for data caching.
Concept: Render API
FreeDrupal's Render API plays a crucial role in how content is presented on a site. The Render API manages how content is rendered through render arrays and render elements.
In this tutorial, we'll:
- Define render arrays, highlighting properties and elements.
- Explain how render elements are used as shorthand for complex structures.
- Describe the primary types of data we can use in a render array.
- Touch on the role of renderers and special methods for rendering entities.
By the end of this tutorial, you'll better understand how Drupal constructs a page's output through render arrays and streamlines rendering with render elements.
Every introduction to coding starts with a "Hello, World!" example, right? With Drupal, it's a bit more complex than just echo "Hello, World!"
. To follow Drupal best practices, we should provide content from our custom code in a way that allows a site administrator to choose where and when it's shown, instead of hard-coding those decisions. This keeps our Drupal application flexible and customizable.
In this tutorial, we'll:
- Author a custom block plugin that outputs the string "Hello, World!".
- Place the block on the home page of our site.
By the end of this tutorial, you should have written the code for a custom block that can display the string "Hello, World!".
Whenever your custom code outputs a render array, you need to use the #cache
property to define the cacheability of the content. This includes providing information about any related context that informs Drupal about how the content varies, and tags that help Drupal know what circumstances might require the cached data to be invalidated. We can add #cache
properties to the render arrays output by both the custom block, and the weather page controller, to ensure they are properly cached.
In this tutorial, we'll:
- Learn how to use the
#cache
property of a render array to provide cacheability data to Drupal. - Provide context about the data that's being displayed.
- Tell Drupal about any dependencies of the content.
By the end of this tutorial you should be able to use the #cache
property to define the cacheability of the content contained in a render array.
Implementing plugins often involves accessing Drupal services to use their functions. Since plugins are PHP classes, we can use dependency injection to access services in our class. This tutorial demonstrates injecting dependencies into plugin classes. We'll update the HelloWorldBlock
class to use the current_user
service through dependency injection.
In this tutorial, we'll:
- Learn how to inject dependencies into plugin classes.
- Update the
HelloWorldBlock
class to use dependency injection for accessing thecurrent_user
service.
By the end of this tutorial, you should understand how to use dependency injection within plugin classes.
Settings forms are commonly used in Drupal modules to allow administrators to manage a module's configuration. This tutorial will guide you through creating a settings form for the Anytown weather forecast module, enabling site administrators to customize the location for weather forecasts.
In this tutorial, we'll:
- Define a new form controller for a settings form.
- Build a
$form
array with options for the settings form. - Associate the form with a route and menu item.
By the end of this tutorial, you'll have a custom settings form that administrators can access.
Installing community-contributed Drupal modules can enhance your project's functionality without the need to develop custom code. It's common to find a module that almost meets your needs but requires updates or additional features. Engaging with the module maintainer and other community members to improve these modules enriches the Drupal ecosystem and aligns with the open source values of collaboration and shared progress. This tutorial outlines how to actively participate in the development of contributed modules by filing issues, submitting patches (through merge requests), and collaborating in issue queues.
In this tutorial, we'll learn:
- The benefits of using contributed modules over custom code.
- The importance of collaboration in the Drupal community.
- Where to learn more about participating in the development of contributed modules.
By the end of this tutorial, you should understand how to enhance and contribute to Drupal's contributed modules ecosystem.
Deciding to share your custom module with the Drupal community can significantly impact both your project and the wider Drupal ecosystem. Contributing modules not only helps enrich the community but also provides a platform for feedback, improvement, and collaboration.
In this tutorial, we'll:
- Explore the benefits of contributing modules to the Drupal community.
- Discuss the key considerations for preparing your module for contribution.
- Highlight the trade-offs between contributing a module and maintaining custom code.
By the end of this tutorial, you should understand the value of contributing to the Drupal project and know what steps to take to prepare your module for contribution.
The Configuration API provides a standardized method for storing and managing a module's settings in Drupal. This tutorial covers the concepts of active configuration, simple configuration, configuration entities, and configuration schemas, and how to interact with, create, and retrieve configuration data.
By the end of this tutorial, you'll have a better understanding of how to work with configuration data in Drupal, focusing on simple configuration for module settings.