跳转到主要内容
rat 提交于 24 September 2014

原文地址: https://www.drupal.org/node/2207559

原文版本: August 25, 2014.

 

Entity types, both configuration and content entity are defined using annotation in the entity class.

The Example entity is defined in Drupal\example\Entity\Example.php (assumes the module name is "example"):


<?php
/**
 * @file
 * Definition of Drupal\example\Entity\Example.
 */
namespace Drupal\example\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
/**
 * Defines an example configuration entity.
 *
 * @ConfigEntityType(
 *   id = "example",
 *   label = @Translation("Example"),
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid"
 *   }
 * )
 */
class Example extends ConfigEntityBase {
  // ...
}
?>

Configuration entities use "@ConfigEntityType" and extend the Drupal\Core\Config\Entity\ConfigEntityBase class. Content entities use "@ContentEntityType" and extend the Drupal\Core\Config\Entity\ContentEntityBase class.

The following properties can be used in entity type annotations:

id (required)
Unique identifier. Use the module name, optionally with an additional _suffix.
config_prefix (configuration only)
Additional configuration identifier. Typically the suffix of the ‘id’ property. Example: id = “node_type”, config_prefix = “type”. The resulting configuration file will have module name as prefix. So {module}.{config_prefix}.{id key value} would be node.type.article
label
Human readable entity name. Example: @Translation("Breakpoint")
bundle_label
Human readable bundle name. Example: @Translation("Content type")
controllers
Array of entity controller classes, keyed by controller type.

storage: The class that loads and saves the object.

form: Array of entity forms classes keyed by their operation (such as 'create', 'edit', or 'delete'). One class can be used for multiple entity operations. This routes can be accessed in *routing.yml via eg. _entity_form: 'foobar.create'

list: The class that provides listings of the entity.

render: The class that renders the entity.

access: The class that is used for access checks. Defaults to \Drupal\Core\Entity\EntityAccessController.

view_builder: The class that provides the entity view builder.

translation: The class that is used for entity translation.

base_table (content only)
The name of the entity's base table. Example: “node”
data_table (content only)
The name of the entity's data table. Example: “node_field_data”
revision_table (content only)
the name of the entity's revision table. Example: “node_revision”
revision_data_table (content only)
The name of the entity's revision data table. Example: “node_field_revision”
admin_permission
The permission required to administer this entity. Example: admin_permission = "administer blocks" Entities with more complex permissions can set their own access controller.
admin_permission
Whether fields can be attached to the entity. Defaults to FALSE.
field_cache
Whether the persistent cache of field data should be used. Defaults to TRUE.
uri_callback
A function (defined in *.module) or class and method that provides the entity URI.
translatable
Whether the entity has multilingual support. Defaults to FALSE.
fieldable
Boolean value, if true additional fields can be added to the entity via gui (ie. via manage fields tab).
render_cache
Whether the rendered output of the entity should be cached. Defaults to TRUE.
entity_keys
Names of object properties that contain certain entity information. Example: $entity->title contains the entity label. Keys: id, label, uuid. Optional keys: bundle, revision.
bundle_entity_type
The name of the entity type which provides bundles. Example: bundle_entity_type = "node_type"
permission_granularity
TODO: Needs description. Allowed values: "entity_type" (default), "bundle" or FALSE.
links
Entity URL definitions, referencing to routes. 'canonical' is the default route for. TODO: Needs description.