Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

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

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

Gábor Hojtsy’s picture

use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;

also required for the plugin annotations to work.

iamEAP’s picture

^ no longer true. See this change record

AaronBauman’s picture

The d8 example is incomplete: how do we implement the 'static cache' boolean in D8?