3 namespace Drupal\Core\Entity;
5 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
8 * Provides an interface for an entity type and its metadata.
10 * Entity type classes can provide docblock annotations. The entity type manager
11 * will use these annotations to populate the entity type object with
14 * Additional properties can be defined by module implementations of
15 * hook_entity_type_build(). Existing data can be altered in implementations of
16 * hook_entity_type_alter(), which can also be used to fill in defaults.
17 * Module-specific properties should be documented in the hook implementations
20 * @see \Drupal\Core\Entity\EntityTypeManagerInterface
21 * @see hook_entity_type_build()
22 * @see hook_entity_type_alter()
24 interface EntityTypeInterface extends PluginDefinitionInterface {
27 * The maximum length of ID, in characters.
29 const ID_MAX_LENGTH = 32;
32 * The maximum length of bundle name, in characters.
34 const BUNDLE_MAX_LENGTH = 32;
37 * Gets any arbitrary property.
39 * @param string $property
40 * The property to retrieve.
43 * The value for that property, or NULL if the property does not exist.
45 public function get($property);
48 * Sets a value to an arbitrary property.
50 * @param string $property
51 * The property to use for the value.
57 public function set($property, $value);
60 * Gets the name of the original entity type class.
62 * In case the class name was changed with setClass(), this will return
63 * the initial value. Useful when trying to identify the entity type ID based
67 * The name of the original entity type class.
69 public function getOriginalClass();
72 * Gets an array of entity keys.
75 * An array describing how the Field API can extract certain information
76 * from objects of this entity type:
77 * - id: The name of the property that contains the primary ID of the
78 * entity. Every entity object passed to the Field API must have this
79 * property and its value must be numeric.
80 * - revision: (optional) The name of the property that contains the
81 * revision ID of the entity. The Field API assumes that all revision IDs
82 * are unique across all entities of a type. If this entry is omitted
83 * the entities of this type are not revisionable.
84 * - bundle: (optional) The name of the property that contains the bundle
85 * name for the entity. The bundle name defines which set of fields are
86 * attached to the entity (e.g. what nodes call "content type"). This
87 * entry can be omitted if this entity type exposes a single bundle (such
88 * that all entities have the same collection of fields). The name of this
89 * single bundle will be the same as the entity type.
90 * - label: (optional) The name of the property that contains the entity
91 * label. For example, if the entity's label is located in
92 * $entity->subject, then 'subject' should be specified here. If complex
93 * logic is required to build the label,
94 * \Drupal\Core\Entity\EntityInterface::label() should be used.
95 * - langcode: (optional) The name of the property that contains the
96 * language code. For instance, if the entity's language is located in
97 * $entity->langcode, then 'langcode' should be specified here.
98 * - uuid: (optional) The name of the property that contains the universally
99 * unique identifier of the entity, which is used to distinctly identify
100 * an entity across different systems.
102 public function getKeys();
105 * Gets a specific entity key.
108 * The name of the entity key to return.
110 * @return string|bool
111 * The entity key, or FALSE if it does not exist.
113 * @see self::getKeys()
115 public function getKey($key);
118 * Indicates if a given entity key exists.
121 * The name of the entity key to check.
124 * TRUE if a given entity key exists, FALSE otherwise.
126 public function hasKey($key);
129 * Indicates whether entities should be statically cached.
132 * TRUE if static caching should be used; FALSE otherwise.
134 public function isStaticallyCacheable();
137 * Indicates whether the rendered output of entities should be cached.
141 public function isRenderCacheable();
144 * Indicates if the persistent cache of field data should be used.
146 * @todo Used by ContentEntityStorageBase only.
148 * The persistent cache should usually only be disabled if a higher level
149 * persistent cache is available for the entity type.
153 public function isPersistentlyCacheable();
156 * Determines if there is a handler for a given type.
158 * @param string $handler_type
159 * The type of handler to check.
160 * @param bool $nested
161 * (optional) If this handler has a nested definition. Defaults to FALSE.
164 * TRUE if a handler of this type exists, FALSE otherwise.
166 public function hasHandlerClass($handler_type, $nested = FALSE);
169 * @param string $handler_type
170 * The handler type to get.
172 * @return array|string|null
173 * The handlers for a given type, or NULL if none exist.
175 public function getHandlerClass($handler_type);
178 * Gets an array of handlers.
181 * An associative array where the keys are the names of different handler
182 * types (listed below) and the values are the names of the classes that
183 * implement that handler:
184 * - storage: The name of the class used to load the objects. The class must
185 * implement \Drupal\Core\Entity\EntityStorageInterface.
186 * - form: An associative array where the keys are the names of the
187 * different form operations (such as 'create', 'edit', or 'delete') and
188 * the values are the names of the handler classes for those
189 * operations. The name of the operation is passed also to the form
190 * handler's constructor, so that one class can be used for multiple
191 * entity forms when the forms are similar. The classes must implement
192 * \Drupal\Core\Entity\EntityFormInterface.
193 * - list: The name of the class that provides listings of the entities. The
194 * class must implement \Drupal\Core\Entity\EntityListBuilderInterface.
195 * - render: The name of the class that is used to render the entities. The
196 * class must implement \Drupal\Core\Entity\EntityViewBuilderInterface.
197 * - access: The name of the class that is used for access checks. The class
198 * must implement \Drupal\Core\Entity\EntityAccessControlHandlerInterface.
199 * Defaults to \Drupal\Core\Entity\EntityAccessControlHandler.
200 * - route_provider: (optional) A list of class names, keyed by a group
201 * string, which will be used to define routes related to this entity
202 * type. These classes must implement
203 * \Drupal\Core\Entity\Routing\EntityRouteProviderInterface.
205 public function getHandlerClasses();
208 * Gets the storage class.
211 * The class for this entity type's storage.
213 public function getStorageClass();
216 * Sets the storage class.
218 * @param string $class
219 * The class for this entity type's storage.
223 public function setStorageClass($class);
226 * Gets the form class for a specific operation.
228 * @param string $operation
229 * The name of the operation to use, e.g., 'default'.
232 * The class for this operation's form for this entity type.
234 * @see \Drupal\Core\Entity\EntityFormBuilderInterface
236 public function getFormClass($operation);
239 * Sets a form class for a specific operation.
241 * @param string $operation
242 * The operation to use this form class for.
243 * @param string $class
244 * The form class implementing
245 * \Drupal\Core\Entity\EntityFormInterface.
249 * @see \Drupal\Core\Entity\EntityFormBuilderInterface
251 public function setFormClass($operation, $class);
254 * Indicates if this entity type has any forms.
257 * TRUE if there are any forms for this entity type, FALSE otherwise.
259 public function hasFormClasses();
262 * Indicates if this entity type has any route provider.
266 public function hasRouteProviders();
269 * Gets all the route provide handlers.
271 * Much like forms you can define multiple route provider handlers.
275 public function getRouteProviderClasses();
278 * Gets the list class.
281 * The class for this entity type's list.
283 public function getListBuilderClass();
286 * Sets the list class.
288 * @param string $class
289 * The list class to use for the operation.
293 public function setListBuilderClass($class);
296 * Indicates if this entity type has a list class.
299 * TRUE if there is a list for this entity type, FALSE otherwise.
301 public function hasListBuilderClass();
304 * Gets the view builder class.
307 * The class for this entity type's view builder.
309 public function getViewBuilderClass();
312 * Gets the view builder class.
314 * @param string $class
315 * The class for this entity type's view builder.
319 public function setViewBuilderClass($class);
322 * Indicates if this entity type has a view builder.
325 * TRUE if there is a view builder for this entity type, FALSE otherwise.
327 public function hasViewBuilderClass();
330 * Gets the access control class.
333 * The class for this entity type's access control.
335 public function getAccessControlClass();
338 * Sets the access control handler class.
340 * @param string $class
341 * The class for this entity type's access control handler.
345 public function setAccessClass($class);
348 * Indicates if the entity type class implements the given interface.
350 * @param string $interface
351 * The class or interface to check.
354 * TRUE if the entity type class implements the given interface.
356 public function entityClassImplements($interface);
359 * Indicates if the entity type is a subclass of the given class or interface.
361 * @param string $class
362 * The class or interface to check.
365 * TRUE if the entity type is a subclass of the class or interface.
367 * @deprecated in Drupal 8.3.0 and will be removed before Drupal 9.0.0.
368 * Use Drupal\Core\Entity\EntityTypeInterface::entityClassImplements()
371 public function isSubclassOf($class);
374 * Sets the handlers for a given type.
376 * @param string $handler_type
377 * The type of handler to set.
378 * @param array|string $value
379 * The value for a handler type.
383 public function setHandlerClass($handler_type, $value);
386 * Gets the name of the default administrative permission.
388 * The default \Drupal\Core\Entity\EntityAccessControlHandler class checks this
389 * permission for all operations in its checkAccess() method. Entities with
390 * more complex permissions can extend this class to do their own access
393 * @return string|bool
395 public function getAdminPermission();
398 * Gets the permission granularity level.
400 * The allowed values are respectively "entity_type" or "bundle".
403 * Whether a module exposing permissions for the current entity type
404 * should use entity-type level granularity or bundle level granularity.
406 public function getPermissionGranularity();
409 * Gets the link templates using the URI template syntax.
411 * Links are an array of standard link relations to the URI template that
412 * should be used for them. Where possible, link relationships should use
413 * established IANA relationships rather than custom relationships.
415 * Every entity type should, at minimum, define "canonical", which is the
416 * pattern for URIs to that entity. Even if the entity will have no HTML page
417 * exposed to users it should still have a canonical URI in order to be
418 * compatible with web services. Entities that will be user-editable via an
419 * HTML page must also define an "edit-form" relationship.
421 * By default, the following placeholders are supported:
422 * - [entityType]: The entity type itself will also be a valid token for the
423 * ID of the entity. For instance, a placeholder of {node} used on the Node
425 * - [bundleEntityType]: The bundle machine name itself. For instance, a
426 * placeholder of {node_type} used on the Node class.
428 * Specific entity types may also expand upon this list by overriding the
429 * Entity::urlRouteParameters() method.
431 * @link http://www.iana.org/assignments/link-relations/link-relations.xml @endlink
432 * @link http://tools.ietf.org/html/rfc6570 @endlink
436 public function getLinkTemplates();
439 * Gets the link template for a given key.
444 * @return string|bool
445 * The path for this link, or FALSE if it doesn't exist.
447 public function getLinkTemplate($key);
450 * Indicates if a link template exists for a given key.
456 * TRUE if the link template exists, FALSE otherwise.
458 public function hasLinkTemplate($key);
461 * Sets a single link template.
464 * The name of a link.
465 * @param string $path
466 * The route path to use for the link.
470 * @throws \InvalidArgumentException
471 * Thrown when the path does not start with a leading slash.
473 public function setLinkTemplate($key, $path);
476 * Gets the callback for the label of the entity.
478 * The function takes an entity and returns the label of the entity. Use
479 * language() on the entity to get information on the requested language. The
480 * entity label is the main string associated with an entity; for example, the
481 * title of a node or the subject of a comment. If there is an entity object
482 * property that defines the label, use the 'label' element of the
483 * 'entity_keys' return value component to provide this information. If more
484 * complex logic is needed to determine the label of an entity, you can
485 * instead specify a callback function here, which will be called to determine
488 * @return callable|null
489 * The callback, or NULL if none exists.
491 * @deprecated in Drupal 8.0.x-dev and will be removed before Drupal 9.0.0.
492 * Use Drupal\Core\Entity\EntityInterface::label() for complex label
493 * generation as needed.
495 * @see \Drupal\Core\Entity\EntityInterface::label()
496 * @see \Drupal\Core\Entity\EntityTypeInterface::setLabelCallback()
497 * @see \Drupal\Core\Entity\EntityTypeInterface::hasLabelCallback()
499 * @todo Remove usages of label_callback https://www.drupal.org/node/2450793.
501 public function getLabelCallback();
504 * Sets the label callback.
506 * @param callable $callback
507 * A callable that returns the label of the entity.
511 * @deprecated in Drupal 8.0.x-dev and will be removed before Drupal 9.0.0.
512 * Use EntityInterface::label() for complex label generation as needed.
514 * @see \Drupal\Core\Entity\EntityInterface::label()
515 * @see \Drupal\Core\Entity\EntityTypeInterface::getLabelCallback()
516 * @see \Drupal\Core\Entity\EntityTypeInterface::hasLabelCallback()
518 public function setLabelCallback($callback);
521 * Indicates if a label callback exists.
525 * @deprecated in Drupal 8.0.x-dev and will be removed before Drupal 9.0.0.
526 * Use EntityInterface::label() for complex label generation as needed.
528 * @see \Drupal\Core\Entity\EntityInterface::label()
529 * @see \Drupal\Core\Entity\EntityTypeInterface::getLabelCallback()
530 * @see \Drupal\Core\Entity\EntityTypeInterface::setLabelCallback()
532 public function hasLabelCallback();
535 * Gets the name of the entity type which provides bundles.
537 * @return string|null
538 * The name of the entity type which provides bundles, or NULL if the entity
539 * type does not have a bundle entity type.
541 public function getBundleEntityType();
544 * Gets the entity type for which this entity provides bundles.
546 * It can be used by other modules to act accordingly; for example,
547 * the Field UI module uses it to add operation links to manage fields and
550 * @return string|null
551 * The entity type for which this entity provides bundles, or NULL if does
552 * not provide bundles for another entity type.
554 public function getBundleOf();
557 * Gets the label for the bundle.
562 public function getBundleLabel();
565 * Gets the name of the entity's base table.
567 * @todo Used by SqlContentEntityStorage only.
569 * @return string|null
570 * The name of the entity's base table, or NULL if none exists.
572 public function getBaseTable();
575 * Indicates whether the entity data is internal.
577 * This can be used in a scenario when it is not desirable to expose data of
578 * this entity type to an external system.
580 * The implications of this method are left to the discretion of the caller.
581 * For example, a module providing an HTTP API may not expose entities of
582 * this type or a custom entity reference field settings form may deprioritize
583 * entities of this type in a select list.
586 * TRUE if the entity data is internal, FALSE otherwise.
588 * @see \Drupal\Core\TypedData\DataDefinitionInterface::isInternal()
590 public function isInternal();
593 * Indicates whether entities of this type have multilingual support.
595 * At an entity level, this indicates language support and at a bundle level
596 * this indicates translation support.
600 public function isTranslatable();
603 * Indicates whether the revision form fields should be added to the form.
606 * TRUE if the form field should be added, FALSE otherwise.
608 public function showRevisionUi();
611 * Indicates whether entities of this type have revision support.
615 public function isRevisionable();
618 * Gets the name of the entity's revision data table.
620 * @todo Used by SqlContentEntityStorage only.
622 * @return string|null
623 * The name of the entity type's revision data table, or NULL if none
626 public function getRevisionDataTable();
629 * Gets the name of the entity's revision table.
631 * @todo Used by SqlContentEntityStorage only.
633 * @return string|null
634 * The name of the entity type's revision table, or NULL if none exists.
636 public function getRevisionTable();
639 * Gets the name of the entity's data table.
641 * @todo Used by SqlContentEntityStorage only.
643 * @return string|null
644 * The name of the entity type's data table, or NULL if none exists.
646 public function getDataTable();
649 * Gets the human-readable name of the entity type.
651 * This label should be used to present a human-readable name of the
655 * The human-readable name of the entity type.
657 public function getLabel();
660 * Gets the lowercase form of the human-readable entity type name.
663 * The lowercase form of the human-readable entity type name.
665 * @see \Drupal\Core\Entity\EntityTypeInterface::getLabel()
667 public function getLowercaseLabel();
670 * Gets the uppercase plural form of the name of the entity type.
672 * This should return a human-readable version of the name that can refer
673 * to all the entities of the given type, collectively. An example usage of
674 * this is the page title of a page devoted to a collection of entities such
675 * as "Workflows" (instead of "Workflow entities").
678 * The collection label.
680 public function getCollectionLabel();
683 * Gets the indefinite singular form of the name of the entity type.
685 * This should return the human-readable name for a single instance of
686 * the entity type. For example: "opportunity" (with the plural as
687 * "opportunities"), "child" (with the plural as "children"), or "content
688 * item" (with the plural as "content items").
691 * The singular label.
693 public function getSingularLabel();
696 * Gets the indefinite plural form of the name of the entity type.
698 * This should return the human-readable name for more than one instance of
699 * the entity type. For example: "opportunities" (with the singular as
700 * "opportunity"), "children" (with the singular as "child"), or "content
701 * items" (with the singular as "content item").
706 public function getPluralLabel();
709 * Gets the label's definite article form for use with a count of entities.
711 * This label should be used when the quantity of entities is provided. The
712 * name should be returned in a form usable with a count of the
713 * entities. For example: "1 opportunity", "5 opportunities", "1 child",
714 * "6 children", "1 content item", "25 content items".
717 * The item count to display if the plural form was requested.
722 public function getCountLabel($count);
725 * Gets a callable that can be used to provide the entity URI.
727 * This is only called if there is no matching link template for the link
728 * relationship type, and there is no bundle-specific callback provided.
730 * @return callable|null
731 * A valid callback that is passed the entity or NULL if none is specified.
733 public function getUriCallback();
736 * Sets a callable to use to provide the entity URI.
738 * @param callable $callback
739 * A callback to use to provide a URI for the entity.
743 public function setUriCallback($callback);
746 * Gets the machine name of the entity type group.
750 public function getGroup();
753 * Gets the human-readable name of the entity type group.
757 public function getGroupLabel();
760 * The list cache contexts associated with this entity type.
762 * Enables code listing entities of this type to ensure that rendered listings
763 * are varied as necessary, typically to ensure users of role A see other
764 * entities listed than users of role B.
768 public function getListCacheContexts();
771 * The list cache tags associated with this entity type.
773 * Enables code listing entities of this type to ensure that newly created
774 * entities show up immediately.
778 public function getListCacheTags();
781 * Gets the key that is used to store configuration dependencies.
784 * The key to be used in configuration dependencies when storing
785 * dependencies on entities of this type.
787 public function getConfigDependencyKey();
790 * Indicates whether this entity type is commonly used as a reference target.
793 * TRUE if the entity type is a common reference; FALSE otherwise.
795 public function isCommonReferenceTarget();
798 * Gets an array of validation constraints.
800 * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
801 * details on how constraints are defined.
804 * An array of validation constraint definitions, keyed by constraint name.
805 * Each constraint definition can be used for instantiating
806 * \Symfony\Component\Validator\Constraint objects.
808 * @see \Symfony\Component\Validator\Constraint
810 public function getConstraints();
813 * Sets the array of validation constraints for the FieldItemList.
815 * NOTE: This will overwrite any previously set constraints. In most cases
816 * ContentEntityTypeInterface::addConstraint() should be used instead.
817 * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
818 * details on how constraints are defined.
820 * @param array $constraints
821 * An array of validation constraint definitions, keyed by constraint name.
822 * Each constraint definition can be used for instantiating
823 * \Symfony\Component\Validator\Constraint objects.
827 * @see \Symfony\Component\Validator\Constraint
829 public function setConstraints(array $constraints);
832 * Adds a validation constraint.
834 * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
835 * details on how constraints are defined.
837 * @param string $constraint_name
838 * The name of the constraint to add, i.e. its plugin id.
839 * @param array|null $options
840 * The constraint options as required by the constraint plugin, or NULL.
844 public function addConstraint($constraint_name, $options = NULL);
847 * Gets the config dependency info for this entity, if any exists.
849 * @param string $bundle
853 * An associative array containing the following keys:
854 * - 'type': The config dependency type (e.g. 'module', 'config').
855 * - 'name': The name of the config dependency.
857 public function getBundleConfigDependency($bundle);