Previously in Drupal 8 (as well as in Drupal 7), you declared a new entity type by implementing hook_entity_info():
function file_entity_info() {
return array(
'file' => array(
'label' => t('File'),
'base table' => 'file_managed',
'controller class' => 'Drupal\file\FileStorageController',
'entity class' => 'Drupal\file\File',
'entity keys' => array(
'id' => 'fid',
'label' => 'filename',
'uuid' => 'uuid',
),
'static cache' => FALSE,
),
);
}
Now, there is no need for a hook, and an annotation is added above the File class, which is now located at core/modules/file/src/Entity/File.php:
namespace Drupal\file\Entity
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\file\FileInterface;
/**
* Defines the file entity class.
*
* @EntityType(
* id = "file",
* label = @Translation("File"),
* controllers = {
* "storage" => "Drupal\file\FileStorageController"
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder"
* },
* base_table = "file_managed",
* entity_keys = {
* "id" = "fid",
* "label" = "filename",
* "uuid" = "uuid"
* }
* )
*/
class File extends ContentEntityBase implements FileInterface {
You no longer need to specify the entity class, as that information is already there.
The "_controller_class" suffix has been removed, and "controller_class" is now called "storage". They are contained by a "controllers" array.
Before where you would have use Drupal\file\File;
, you now have use Drupal\file\Entity\File;
. In most cases, like entity type checks and type hints on hooks and methods, the corresponding interface should be used instead, e.g. \Drupal\file\FileInterface
Any dynamic info added in hook_entity_info() should now be moved to hook_entity_info_alter().
See also: Entities are now classed objects using a defined interface
Comments
use annotation classes
also required for the plugin annotations to work.
^ no longer true. See this
^ no longer true. See this change record
static cache?
The d8 example is incomplete: how do we implement the
'static cache'
boolean in D8?