Module Development
Topic

Utility Functions and Classes for Drupal 8, 9, and 10

You may have heard the saying, “There’s a module for that.” Well, there may be a function for that as well. In Drupal’s core library (located at core/lib/Drupal/) there are a number of utility functions and classes that, as a module developer, you will find could make your task easier or less tedious. They are intended to serve as reusable solutions to common problems like cryptography, Unicode strings, and user input sanitization that you’ll face when writing a custom module.

Often these functions or classes are used in best practices and so it’s a good idea to become familiar with the types of functionality that they cover. Information about these utility classes and functions may be found by navigating to api.drupal.org. Under the “Other Essential APIs” heading, find the link to “Utility classes and functions."

In addition to the list of classes and descriptions you find there, you will also find reference to the following utilities:

  • common.inc: Common functions that many Drupal modules will need to reference. The functions that are critical and need to be available even when serving a cached page are instead located in bootstrap.inc.
  • File Interface: Common file handling functions.
  • Formatting: Functions to format numbers, strings, dates, etc.
  • PHP wrapper functions: Functions that are wrappers or custom implementations of PHP functions. Certain PHP functions should not be used in Drupal. Instead, Drupal's replacement functions should be used.
  • Sanitization functions: Functions to sanitize values.
  • Transliteration: Transliterate from Unicode to US-ASCII.
  • Input validation: Functions to validate user input.

Example tasks

  • Find data in nested arrays without building a one-off recursive search function
  • Sanitize values from user input
  • Work with Unicode strings

Confidence

These utility functions and classes are often subject to refactoring or deprecation. Be sure to consult the API topic page and click through to the function or method to see if it is deprecated.

Drupalize.Me resources

Posted on Wednesday, January 31, 2024 - 18:03 by Blake Hall

We've made it to the final blog post (for now?) in our spotlight on Symfony in Drupal. In this blog post, we'll take a look at Symfony's utility components. We're using this term to group together Symfony components that provide useful functionality. It's a great idea to familiarize yourself with these. You can make use of them in your own modules, or bring them into other PHP application code.

Drupal 8, 9, and 10
More information

Any text that will be displayed to user as part of your application's user interface should be passed through the t() function, or an equivalent, so that it can be translated into other languages as needed. This tutorial will look at how to use the t() function.

This tutorial contains information that applies to anyone writing modules or themes. And many of the tutorials you read on this site and on the web in general will expect that you understand how basic string translation works.

In this tutorial we'll look at:

  • Passing strings through the t() function or equivalent so they are available for translation
  • Using placeholders for dynamic content in translatable strings
  • Tips for making your code's interface strings easier to translate

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate guides

External resources

  • Utility classes and functions (api.drupal.org)
    • Overview of utility classes and functions for developers.
  • Writing secure code for Drupal (Drupal.org)
    • A guide to writing secure code in Drupal, including sanitizing user input.
  • Hashing function for session (drupal.stackexchange.com)
    • This Drupal Stack Exchange question asks: “I need to verify the users session of Drupal on a Node.js server. The session ID has been hashed and it is stored in the Drupal 8 database. How can I hash the browsers session ID to compare it with the one stored in the database?”