Previously, each entity type class would extend EntityInterface, ContentEntityInterface, or ConfigEntityInterface, and EntityInterface or the class name (e.g. Node) was used to type-hint parameters.
Now each entity type class should implement its own interface, and when a method or function argument must be that entity type, type-hint with the specific interface.
For example, hook_entity_delete()
still works on any entity type, so a custom module would use:
use Drupal\Core\Entity\EntityInterface;
function mymodule_entity_delete(EntityInterface $entity) {
}
But a node-specific hook like hook_node_prepare()
would instead specify that it expects a node:
use Drupal\node\NodeInterface;
function mymodule_node_prepare(NodeInterface $node) {
}
In addition, any public methods added to a custom entity type must be on the interface as well.
For example, Drupal\menu_link\Plugin\Core\Entity\MenuLink has a getRoute()
method, so it is documented and specified on Drupal\menu_link\MenuLinkInterface.