Providing a custom storage handler
Storage handlers control the loading, saving, and deleting of an entity. Content entity types have the default storage handler of \Drupal\Core\Entity\Sql\SqlContentEntityStorage
. Configuration entity types have the default storage handler of \Drupal\Core\Config\Entity\ConfigEntityStorage
. These classes can be extended to implement alternative methods and set as the entity type’s storage
handler.
In this recipe, we will create a method for the Message
entity type created previously in this chapter to load all messages of a specific type.
How to do it…
- Create a
MessageStorage
class in the module’ssrc
directory. This class will extend theSqlContentEntityStorage
class:<?php
namespace Drupal\mymodule;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
class MessageStorage extends SqlContentEntityStorage {
}
The default storage for content entity types is the SqlContentEntityStorage
class, which is why we extend...