Module Development

Typed Data API for Drupal 8, 9, and 10

The Typed Data API in Drupal helps add additional functionality to PHP's built-in data types that make working with data in Drupal much more predictable. It allows code to make intelligent guesses about the type of data that a field on an entity contains. For example differentiating between a string of text, and a string of text that represents a URL.

In this tutorial we'll:

  • Look at the 3 main types of typed data in Drupal: primitives, complex data, and lists.
  • See how different data types and definitions are defined and show how to define your own data type.
  • Look at the interfaces provided by each data type to see some of the benefits to adding this abstraction layer.

By the end of this tutorial you should have a better understanding of what the Typed Data API is, where you'll most likely encounter it, and how to use it in your code.