Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavMorr/38c8fd4c70c4ab70fc8fe200fee2186c to your computer and use it in GitHub Desktop.
Save DavMorr/38c8fd4c70c4ab70fc8fe200fee2186c to your computer and use it in GitHub Desktop.
Entity API implements Typed Data API : Using the API - Examples

Entity API implements Typed Data API

from Entity API implements Typed Data API (drupal.org)

Using the API

Entity API defines some magic methods, like __get(), to allow for fast and easy access to field values. So using the API is very straightforward and the syntax reminds a lot of the pre Drupal 8 era.

Fetching the actual value of the image’s alternative text will be done as below:

Figure 1

// The most verbose way.
$string = $entity->get('image')->offsetGet(0)->get('alt')->getValue();

// With magic added by the Entity API.
$string = $entity->image[0]->alt;

// With more magic added by Entity API, to fetch the first item
// in the list by default.
$string = $entity->image->alt;

The above example only adds some nice syntax to the old API. The below examples demonstrates where the real value of this API comes in - inspecting the data:

Figure 2

// Returns an array with named keys for all fields and their
// definitions. For example the ‘image’ field.
$property_definitions = $entity->getFieldDefinitions();

// Returns an array with name keys for all properties and their
// definitions. For example the ‘file_id’ and ‘alt’ properties.
$property_definitions = $entity->image
  ->getFieldDefinition()
  ->getFieldStorageDefinition()
  ->getPropertyDefinitions();

// Returns only definition for the ‘alt’ property.
$string_definition = $entity->image
  ->getFieldDefinition()
  ->getFieldStorageDefinition()
  ->getPropertyDefinition('alt');

Based on the definitions we fetched above, we can now do clever things like serialization or other data massaging. We can also expose this data over semantically rich APIs, such as an JSON-LD endpoint so that other systems can understand the essentials of our data.

See https://drupal.org/node/2078241 on more information about how to define and use field definitions of an entity type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment