An Entity Property API for Drupal 8

Problem: 

Having the entity API unifying CRUD for Drupal 8 is already a great step forward. It seems obvious that it should handle full CRUD, but for it to be really useful we need to care more about the entity properties, fields and their differences.

While fields feature APIs for validation, access information, widgets, display formatters or translatability, we have nothing like that for regular entity properties. However, for modules to be able to work with entities in general, we need some of those features for regular entity properties too. Consider the use-case "RESTful web services", or any module that wants to generically use or update data from entities, like the Search API or Rules already do.

Proposed solution: 

The entity API module achieves already something similar in Drupal 7. It comes with an Entity property information system, which kind of wraps Drupal's divergent data structures behind unified data wrappers.

However for Drupal 8 we want to avoid the need for unnecessary wrappers. So we need to come up with an API that fulfills developer's needs without anything extra. So should be everything a field then?

I'd like to present and debate a slightly different approach: The entity property API, which cares about an unified and easy way for accessing and updating property values, validation, access information as well as other property metadata.
With such an entity property API in place, fields could very much build upon this generally useful API and complement it wherever necessary. Related, we might want to look into streamlining field storage and entity storage, and provide a storage system that can handle translatable properties out of the box.

For example, this is how the API could work:
<?php
// Get a node author.
$account = $node->author->get('author');
// Still, you can access $node->uid directly.

// Get the english version of the node title.
$node->get('title', array('lang' => 'en'));

// Throws exception if mail address is formatted invalid.
$account->set('mail', $value);

// Chain things as you wish.
// -> Get the node author's fourth hobby stored in his main profile.
print $node->get('author')->get('profile_main')->get('hobbies', array('delta' => 3));
?>

Platinum Sponsors