Topic
Last updated November 2, 2017

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

  • Make Strings Translatable
    • 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.

More Guides

We have guides on many Drupal skills and topics.

Explore guides

External resources

  • Utility classes and functions (api.drupal.org)
    • Overview of utility classes and functions for developers.
  • Sanitization functions (api.drupal.org)
    • Contains a list of functions that can be used to sanitize values in Drupal.
  • 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?"