3 namespace Drupal\Core\Entity;
5 use Drupal\Component\Plugin\Definition\PluginDefinition;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Entity\Exception\EntityTypeIdLengthException;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\StringTranslation\TranslatableMarkup;
12 * Provides an implementation of an entity type and its metadata.
16 class EntityType extends PluginDefinition implements EntityTypeInterface {
18 use StringTranslationTrait;
21 * Indicates whether entities should be statically cached.
25 protected $static_cache = TRUE;
28 * Indicates whether the rendered output of entities should be cached.
32 protected $render_cache = TRUE;
35 * Indicates if the persistent cache of field data should be used.
39 protected $persistent_cache = TRUE;
42 * An array of entity keys.
46 protected $entity_keys = [];
49 * The unique identifier of this entity type.
56 * The name of the original entity type class.
58 * This is only set if the class name is changed.
62 protected $originalClass;
65 * An array of handlers.
69 protected $handlers = [];
72 * The name of the default administrative permission.
76 protected $admin_permission;
79 * The permission granularity level.
81 * The allowed values are respectively "entity_type" or "bundle".
85 protected $permission_granularity = 'entity_type';
87 * Link templates using the URI template syntax.
91 protected $links = [];
94 * The name of a callback that returns the label of the entity.
98 * @deprecated in Drupal 8.0.x-dev and will be removed before Drupal 9.0.0.
99 * Use Drupal\Core\Entity\EntityInterface::label() for complex label
100 * generation as needed.
102 * @see \Drupal\Core\Entity\EntityInterface::label()
104 * @todo Remove usages of label_callback https://www.drupal.org/node/2450793.
106 protected $label_callback = NULL;
109 * The name of the entity type which provides bundles.
113 protected $bundle_entity_type = NULL;
116 * The name of the entity type for which bundles are provided.
120 protected $bundle_of = NULL;
123 * The human-readable name of the entity bundles, e.g. Vocabulary.
127 protected $bundle_label = NULL;
130 * The name of the entity type's base table.
134 protected $base_table = NULL;
137 * The name of the entity type's revision data table.
141 protected $revision_data_table = NULL;
144 * The name of the entity type's revision table.
148 protected $revision_table = NULL;
151 * The name of the entity type's data table.
155 protected $data_table = NULL;
158 * Indicates whether the entity data is internal.
162 protected $internal = FALSE;
165 * Indicates whether entities of this type have multilingual support.
169 protected $translatable = FALSE;
172 * Indicates whether the revision form fields should be added to the form.
176 protected $show_revision_ui = FALSE;
179 * The human-readable name of the type.
183 * @see \Drupal\Core\Entity\EntityTypeInterface::getLabel()
185 protected $label = '';
188 * The human-readable label for a collection of entities of the type.
192 * @see \Drupal\Core\Entity\EntityTypeInterface::getCollectionLabel()
194 protected $label_collection = '';
197 * The indefinite singular name of the type.
201 * @see \Drupal\Core\Entity\EntityTypeInterface::getSingularLabel()
203 protected $label_singular = '';
206 * The indefinite plural name of the type.
210 * @see \Drupal\Core\Entity\EntityTypeInterface::getPluralLabel()
212 protected $label_plural = '';
215 * A definite singular/plural name of the type.
217 * Needed keys: "singular" and "plural".
221 * @see \Drupal\Core\Entity\EntityTypeInterface::getCountLabel()
223 protected $label_count = [];
226 * A callable that can be used to provide the entity URI.
230 protected $uri_callback = NULL;
233 * The machine name of the entity type group.
238 * The human-readable name of the entity type group.
240 protected $group_label;
243 * The route name used by field UI to attach its management pages.
247 protected $field_ui_base_route;
250 * Indicates whether this entity type is commonly used as a reference target.
252 * This is used by the Entity reference field to promote an entity type in the
253 * add new field select list in Field UI.
257 protected $common_reference_target = FALSE;
260 * The list cache contexts for this entity type.
264 protected $list_cache_contexts = [];
267 * The list cache tags for this entity type.
271 protected $list_cache_tags = [];
274 * Entity constraint definitions.
278 protected $constraints = [];
281 * Any additional properties and values.
285 protected $additional = [];
288 * Constructs a new EntityType.
290 * @param array $definition
291 * An array of values from the annotation.
293 * @throws \Drupal\Core\Entity\Exception\EntityTypeIdLengthException
294 * Thrown when attempting to instantiate an entity type with too long ID.
296 public function __construct($definition) {
297 // Throw an exception if the entity type ID is longer than 32 characters.
298 if (Unicode::strlen($definition['id']) > static::ID_MAX_LENGTH) {
299 throw new EntityTypeIdLengthException('Attempt to create an entity type with an ID longer than ' . static::ID_MAX_LENGTH . " characters: {$definition['id']}.");
302 foreach ($definition as $property => $value) {
303 $this->set($property, $value);
307 $this->entity_keys += [
311 'default_langcode' => 'default_langcode',
312 'revision_translation_affected' => 'revision_translation_affected',
315 'access' => 'Drupal\Core\Entity\EntityAccessControlHandler',
317 if (isset($this->handlers['storage'])) {
318 $this->checkStorageClass($this->handlers['storage']);
321 // Automatically add the "EntityChanged" constraint if the entity type
322 // tracks the changed time.
323 if ($this->entityClassImplements(EntityChangedInterface::class)) {
324 $this->addConstraint('EntityChanged');
326 // Automatically add the "EntityUntranslatableFields" constraint if we have
327 // an entity type supporting translatable fields and pending revisions.
328 if ($this->entityClassImplements(ContentEntityInterface::class)) {
329 $this->addConstraint('EntityUntranslatableFields');
332 // Ensure a default list cache tag is set.
333 if (empty($this->list_cache_tags)) {
334 $this->list_cache_tags = [$definition['id'] . '_list'];
341 public function get($property) {
342 if (property_exists($this, $property)) {
343 $value = isset($this->{$property}) ? $this->{$property} : NULL;
346 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
354 public function set($property, $value) {
355 if (property_exists($this, $property)) {
356 $this->{$property} = $value;
359 $this->additional[$property] = $value;
367 public function isInternal() {
368 return $this->internal;
374 public function isStaticallyCacheable() {
375 return $this->static_cache;
381 public function isRenderCacheable() {
382 return $this->render_cache;
388 public function isPersistentlyCacheable() {
389 return $this->persistent_cache;
395 public function getKeys() {
396 return $this->entity_keys;
402 public function getKey($key) {
403 $keys = $this->getKeys();
404 return isset($keys[$key]) ? $keys[$key] : FALSE;
410 public function hasKey($key) {
411 $keys = $this->getKeys();
412 return !empty($keys[$key]);
418 public function getOriginalClass() {
419 return $this->originalClass ?: $this->class;
425 public function setClass($class) {
426 if (!$this->originalClass && $this->class) {
427 // If the original class is currently not set, set it to the current
428 // class, assume that is the original class name.
429 $this->originalClass = $this->class;
432 return parent::setClass($class);
438 public function entityClassImplements($interface) {
439 return is_subclass_of($this->getClass(), $interface);
445 public function isSubclassOf($class) {
446 return $this->entityClassImplements($class);
452 public function getHandlerClasses() {
453 return $this->handlers;
459 public function getHandlerClass($handler_type, $nested = FALSE) {
460 if ($this->hasHandlerClass($handler_type, $nested)) {
461 $handlers = $this->getHandlerClasses();
462 return $nested ? $handlers[$handler_type][$nested] : $handlers[$handler_type];
469 public function setHandlerClass($handler_type, $value) {
470 $this->handlers[$handler_type] = $value;
477 public function hasHandlerClass($handler_type, $nested = FALSE) {
478 $handlers = $this->getHandlerClasses();
479 if (!isset($handlers[$handler_type]) || ($nested && !isset($handlers[$handler_type][$nested]))) {
482 $handler = $handlers[$handler_type];
484 $handler = $handler[$nested];
486 return class_exists($handler);
492 public function getStorageClass() {
493 return $this->getHandlerClass('storage');
499 public function setStorageClass($class) {
500 $this->checkStorageClass($class);
501 $this->handlers['storage'] = $class;
506 * Checks that the provided class is compatible with the current entity type.
508 * @param string $class
509 * The class to check.
511 protected function checkStorageClass($class) {
512 // Nothing to check by default.
518 public function getFormClass($operation) {
519 return $this->getHandlerClass('form', $operation);
525 public function setFormClass($operation, $class) {
526 $this->handlers['form'][$operation] = $class;
533 public function hasFormClasses() {
534 return !empty($this->handlers['form']);
540 public function hasRouteProviders() {
541 return !empty($this->handlers['route_provider']);
547 public function getListBuilderClass() {
548 return $this->getHandlerClass('list_builder');
554 public function setListBuilderClass($class) {
555 $this->handlers['list_builder'] = $class;
562 public function hasListBuilderClass() {
563 return $this->hasHandlerClass('list_builder');
569 public function getViewBuilderClass() {
570 return $this->getHandlerClass('view_builder');
576 public function setViewBuilderClass($class) {
577 $this->handlers['view_builder'] = $class;
584 public function hasViewBuilderClass() {
585 return $this->hasHandlerClass('view_builder');
591 public function getRouteProviderClasses() {
592 return !empty($this->handlers['route_provider']) ? $this->handlers['route_provider'] : [];
598 public function getAccessControlClass() {
599 return $this->getHandlerClass('access');
605 public function setAccessClass($class) {
606 $this->handlers['access'] = $class;
613 public function getAdminPermission() {
614 return $this->admin_permission ?: FALSE;
620 public function getPermissionGranularity() {
621 return $this->permission_granularity;
627 public function getLinkTemplates() {
634 public function getLinkTemplate($key) {
635 $links = $this->getLinkTemplates();
636 return isset($links[$key]) ? $links[$key] : FALSE;
642 public function hasLinkTemplate($key) {
643 $links = $this->getLinkTemplates();
644 return isset($links[$key]);
650 public function setLinkTemplate($key, $path) {
651 if ($path[0] !== '/') {
652 throw new \InvalidArgumentException('Link templates accepts paths, which have to start with a leading slash.');
655 $this->links[$key] = $path;
662 public function getLabelCallback() {
663 return $this->label_callback;
669 public function setLabelCallback($callback) {
670 $this->label_callback = $callback;
677 public function hasLabelCallback() {
678 return isset($this->label_callback);
684 public function getBundleEntityType() {
685 return $this->bundle_entity_type;
691 public function getBundleOf() {
692 return $this->bundle_of;
698 public function getBundleLabel() {
699 // If there is no bundle label defined, try to provide some sensible
701 if (!empty($this->bundle_label)) {
702 return (string) $this->bundle_label;
704 elseif ($bundle_entity_type_id = $this->getBundleEntityType()) {
705 return (string) \Drupal::entityTypeManager()->getDefinition($bundle_entity_type_id)->getLabel();
707 return (string) new TranslatableMarkup('@type_label bundle', ['@type_label' => $this->getLabel()], [], $this->getStringTranslation());
713 public function getBaseTable() {
714 return $this->base_table;
720 public function showRevisionUi() {
721 return $this->isRevisionable() && $this->show_revision_ui;
727 public function isTranslatable() {
728 return !empty($this->translatable);
734 public function isRevisionable() {
735 // Entity types are revisionable if a revision key has been specified.
736 return $this->hasKey('revision');
742 public function getRevisionDataTable() {
743 return $this->revision_data_table;
749 public function getRevisionTable() {
750 return $this->revision_table;
756 public function getDataTable() {
757 return $this->data_table;
763 public function getLabel() {
770 public function getLowercaseLabel() {
771 return Unicode::strtolower($this->getLabel());
777 public function getCollectionLabel() {
778 if (empty($this->label_collection)) {
779 $label = $this->getLabel();
780 $this->label_collection = new TranslatableMarkup('@label entities', ['@label' => $label], [], $this->getStringTranslation());
782 return $this->label_collection;
788 public function getSingularLabel() {
789 if (empty($this->label_singular)) {
790 $lowercase_label = $this->getLowercaseLabel();
791 $this->label_singular = $lowercase_label;
793 return $this->label_singular;
799 public function getPluralLabel() {
800 if (empty($this->label_plural)) {
801 $lowercase_label = $this->getLowercaseLabel();
802 $this->label_plural = new TranslatableMarkup('@label entities', ['@label' => $lowercase_label], [], $this->getStringTranslation());
804 return $this->label_plural;
810 public function getCountLabel($count) {
811 if (empty($this->label_count)) {
812 return $this->formatPlural($count, '@count @label', '@count @label entities', ['@label' => $this->getLowercaseLabel()], ['context' => 'Entity type label']);
814 $context = isset($this->label_count['context']) ? $this->label_count['context'] : 'Entity type label';
815 return $this->formatPlural($count, $this->label_count['singular'], $this->label_count['plural'], ['context' => $context]);
821 public function getUriCallback() {
822 return $this->uri_callback;
828 public function setUriCallback($callback) {
829 $this->uri_callback = $callback;
836 public function getGroup() {
844 public function getGroupLabel() {
845 return !empty($this->group_label) ? $this->group_label : $this->t('Other', [], ['context' => 'Entity type group']);
851 public function getListCacheContexts() {
852 return $this->list_cache_contexts;
858 public function getListCacheTags() {
859 return $this->list_cache_tags;
865 public function getConfigDependencyKey() {
866 // Return 'content' for the default implementation as important distinction
867 // is that dependencies on other configuration entities are hard
868 // dependencies and have to exist before creating the dependent entity.
875 public function isCommonReferenceTarget() {
876 return $this->common_reference_target;
882 public function getConstraints() {
883 return $this->constraints;
889 public function setConstraints(array $constraints) {
890 $this->constraints = $constraints;
897 public function addConstraint($constraint_name, $options = NULL) {
898 $this->constraints[$constraint_name] = $options;
905 public function getBundleConfigDependency($bundle) {
906 // If this entity type uses entities to manage its bundles then depend on
907 // the bundle entity.
908 if ($bundle_entity_type_id = $this->getBundleEntityType()) {
909 if (!$bundle_entity = \Drupal::entityManager()->getStorage($bundle_entity_type_id)->load($bundle)) {
910 throw new \LogicException(sprintf('Missing bundle entity, entity type %s, entity id %s.', $bundle_entity_type_id, $bundle));
912 $config_dependency = [
914 'name' => $bundle_entity->getConfigDependencyName(),
918 // Depend on the provider of the entity type.
919 $config_dependency = [
921 'name' => $this->getProvider(),
925 return $config_dependency;