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 entities of this type have multilingual support.
162 protected $translatable = FALSE;
165 * Indicates whether the revision form fields should be added to the form.
169 protected $show_revision_ui = FALSE;
172 * The human-readable name of the type.
176 * @see \Drupal\Core\Entity\EntityTypeInterface::getLabel()
178 protected $label = '';
181 * The human-readable label for a collection of entities of the type.
185 * @see \Drupal\Core\Entity\EntityTypeInterface::getCollectionLabel()
187 protected $label_collection = '';
190 * The indefinite singular name of the type.
194 * @see \Drupal\Core\Entity\EntityTypeInterface::getSingularLabel()
196 protected $label_singular = '';
199 * The indefinite plural name of the type.
203 * @see \Drupal\Core\Entity\EntityTypeInterface::getPluralLabel()
205 protected $label_plural = '';
208 * A definite singular/plural name of the type.
210 * Needed keys: "singular" and "plural".
214 * @see \Drupal\Core\Entity\EntityTypeInterface::getCountLabel()
216 protected $label_count = [];
219 * A callable that can be used to provide the entity URI.
223 protected $uri_callback = NULL;
226 * The machine name of the entity type group.
231 * The human-readable name of the entity type group.
233 protected $group_label;
236 * The route name used by field UI to attach its management pages.
240 protected $field_ui_base_route;
243 * Indicates whether this entity type is commonly used as a reference target.
245 * This is used by the Entity reference field to promote an entity type in the
246 * add new field select list in Field UI.
250 protected $common_reference_target = FALSE;
253 * The list cache contexts for this entity type.
257 protected $list_cache_contexts = [];
260 * The list cache tags for this entity type.
264 protected $list_cache_tags = [];
267 * Entity constraint definitions.
271 protected $constraints = [];
274 * Any additional properties and values.
278 protected $additional = [];
281 * Constructs a new EntityType.
283 * @param array $definition
284 * An array of values from the annotation.
286 * @throws \Drupal\Core\Entity\Exception\EntityTypeIdLengthException
287 * Thrown when attempting to instantiate an entity type with too long ID.
289 public function __construct($definition) {
290 // Throw an exception if the entity type ID is longer than 32 characters.
291 if (Unicode::strlen($definition['id']) > static::ID_MAX_LENGTH) {
292 throw new EntityTypeIdLengthException('Attempt to create an entity type with an ID longer than ' . static::ID_MAX_LENGTH . " characters: {$definition['id']}.");
295 foreach ($definition as $property => $value) {
296 $this->set($property, $value);
300 $this->entity_keys += [
304 'default_langcode' => 'default_langcode',
305 'revision_translation_affected' => 'revision_translation_affected',
308 'access' => 'Drupal\Core\Entity\EntityAccessControlHandler',
310 if (isset($this->handlers['storage'])) {
311 $this->checkStorageClass($this->handlers['storage']);
314 // Automatically add the EntityChanged constraint if the entity type tracks
316 if ($this->entityClassImplements(EntityChangedInterface::class)) {
317 $this->addConstraint('EntityChanged');
320 // Ensure a default list cache tag is set.
321 if (empty($this->list_cache_tags)) {
322 $this->list_cache_tags = [$definition['id'] . '_list'];
329 public function get($property) {
330 if (property_exists($this, $property)) {
331 $value = isset($this->{$property}) ? $this->{$property} : NULL;
334 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
342 public function set($property, $value) {
343 if (property_exists($this, $property)) {
344 $this->{$property} = $value;
347 $this->additional[$property] = $value;
355 public function isStaticallyCacheable() {
356 return $this->static_cache;
362 public function isRenderCacheable() {
363 return $this->render_cache;
369 public function isPersistentlyCacheable() {
370 return $this->persistent_cache;
376 public function getKeys() {
377 return $this->entity_keys;
383 public function getKey($key) {
384 $keys = $this->getKeys();
385 return isset($keys[$key]) ? $keys[$key] : FALSE;
391 public function hasKey($key) {
392 $keys = $this->getKeys();
393 return !empty($keys[$key]);
399 public function getOriginalClass() {
400 return $this->originalClass ?: $this->class;
406 public function setClass($class) {
407 if (!$this->originalClass && $this->class) {
408 // If the original class is currently not set, set it to the current
409 // class, assume that is the original class name.
410 $this->originalClass = $this->class;
413 return parent::setClass($class);
419 public function entityClassImplements($interface) {
420 return is_subclass_of($this->getClass(), $interface);
426 public function isSubclassOf($class) {
427 return $this->entityClassImplements($class);
433 public function getHandlerClasses() {
434 return $this->handlers;
440 public function getHandlerClass($handler_type, $nested = FALSE) {
441 if ($this->hasHandlerClass($handler_type, $nested)) {
442 $handlers = $this->getHandlerClasses();
443 return $nested ? $handlers[$handler_type][$nested] : $handlers[$handler_type];
450 public function setHandlerClass($handler_type, $value) {
451 $this->handlers[$handler_type] = $value;
458 public function hasHandlerClass($handler_type, $nested = FALSE) {
459 $handlers = $this->getHandlerClasses();
460 if (!isset($handlers[$handler_type]) || ($nested && !isset($handlers[$handler_type][$nested]))) {
463 $handler = $handlers[$handler_type];
465 $handler = $handler[$nested];
467 return class_exists($handler);
473 public function getStorageClass() {
474 return $this->getHandlerClass('storage');
480 public function setStorageClass($class) {
481 $this->checkStorageClass($class);
482 $this->handlers['storage'] = $class;
487 * Checks that the provided class is compatible with the current entity type.
489 * @param string $class
490 * The class to check.
492 protected function checkStorageClass($class) {
493 // Nothing to check by default.
499 public function getFormClass($operation) {
500 return $this->getHandlerClass('form', $operation);
506 public function setFormClass($operation, $class) {
507 $this->handlers['form'][$operation] = $class;
514 public function hasFormClasses() {
515 return !empty($this->handlers['form']);
521 public function hasRouteProviders() {
522 return !empty($this->handlers['route_provider']);
528 public function getListBuilderClass() {
529 return $this->getHandlerClass('list_builder');
535 public function setListBuilderClass($class) {
536 $this->handlers['list_builder'] = $class;
543 public function hasListBuilderClass() {
544 return $this->hasHandlerClass('list_builder');
550 public function getViewBuilderClass() {
551 return $this->getHandlerClass('view_builder');
557 public function setViewBuilderClass($class) {
558 $this->handlers['view_builder'] = $class;
565 public function hasViewBuilderClass() {
566 return $this->hasHandlerClass('view_builder');
572 public function getRouteProviderClasses() {
573 return !empty($this->handlers['route_provider']) ? $this->handlers['route_provider'] : [];
579 public function getAccessControlClass() {
580 return $this->getHandlerClass('access');
586 public function setAccessClass($class) {
587 $this->handlers['access'] = $class;
594 public function getAdminPermission() {
595 return $this->admin_permission ?: FALSE;
601 public function getPermissionGranularity() {
602 return $this->permission_granularity;
608 public function getLinkTemplates() {
615 public function getLinkTemplate($key) {
616 $links = $this->getLinkTemplates();
617 return isset($links[$key]) ? $links[$key] : FALSE;
623 public function hasLinkTemplate($key) {
624 $links = $this->getLinkTemplates();
625 return isset($links[$key]);
631 public function setLinkTemplate($key, $path) {
632 if ($path[0] !== '/') {
633 throw new \InvalidArgumentException('Link templates accepts paths, which have to start with a leading slash.');
636 $this->links[$key] = $path;
643 public function getLabelCallback() {
644 return $this->label_callback;
650 public function setLabelCallback($callback) {
651 $this->label_callback = $callback;
658 public function hasLabelCallback() {
659 return isset($this->label_callback);
665 public function getBundleEntityType() {
666 return $this->bundle_entity_type;
672 public function getBundleOf() {
673 return $this->bundle_of;
679 public function getBundleLabel() {
680 return (string) $this->bundle_label;
686 public function getBaseTable() {
687 return $this->base_table;
693 public function showRevisionUi() {
694 return $this->isRevisionable() && $this->show_revision_ui;
700 public function isTranslatable() {
701 return !empty($this->translatable);
707 public function isRevisionable() {
708 // Entity types are revisionable if a revision key has been specified.
709 return $this->hasKey('revision');
715 public function getRevisionDataTable() {
716 return $this->revision_data_table;
722 public function getRevisionTable() {
723 return $this->revision_table;
729 public function getDataTable() {
730 return $this->data_table;
736 public function getLabel() {
743 public function getLowercaseLabel() {
744 return Unicode::strtolower($this->getLabel());
750 public function getCollectionLabel() {
751 if (empty($this->label_collection)) {
752 $label = $this->getLabel();
753 $this->label_collection = new TranslatableMarkup('@label entities', ['@label' => $label], [], $this->getStringTranslation());
755 return $this->label_collection;
761 public function getSingularLabel() {
762 if (empty($this->label_singular)) {
763 $lowercase_label = $this->getLowercaseLabel();
764 $this->label_singular = $lowercase_label;
766 return $this->label_singular;
772 public function getPluralLabel() {
773 if (empty($this->label_plural)) {
774 $lowercase_label = $this->getLowercaseLabel();
775 $this->label_plural = new TranslatableMarkup('@label entities', ['@label' => $lowercase_label], [], $this->getStringTranslation());
777 return $this->label_plural;
783 public function getCountLabel($count) {
784 if (empty($this->label_count)) {
785 return $this->formatPlural($count, '@count @label', '@count @label entities', ['@label' => $this->getLowercaseLabel()], ['context' => 'Entity type label']);
787 $context = isset($this->label_count['context']) ? $this->label_count['context'] : 'Entity type label';
788 return $this->formatPlural($count, $this->label_count['singular'], $this->label_count['plural'], ['context' => $context]);
794 public function getUriCallback() {
795 return $this->uri_callback;
801 public function setUriCallback($callback) {
802 $this->uri_callback = $callback;
809 public function getGroup() {
817 public function getGroupLabel() {
818 return !empty($this->group_label) ? $this->group_label : $this->t('Other', [], ['context' => 'Entity type group']);
824 public function getListCacheContexts() {
825 return $this->list_cache_contexts;
831 public function getListCacheTags() {
832 return $this->list_cache_tags;
838 public function getConfigDependencyKey() {
839 // Return 'content' for the default implementation as important distinction
840 // is that dependencies on other configuration entities are hard
841 // dependencies and have to exist before creating the dependent entity.
848 public function isCommonReferenceTarget() {
849 return $this->common_reference_target;
855 public function getConstraints() {
856 return $this->constraints;
862 public function setConstraints(array $constraints) {
863 $this->constraints = $constraints;
870 public function addConstraint($constraint_name, $options = NULL) {
871 $this->constraints[$constraint_name] = $options;
878 public function getBundleConfigDependency($bundle) {
879 // If this entity type uses entities to manage its bundles then depend on
880 // the bundle entity.
881 if ($bundle_entity_type_id = $this->getBundleEntityType()) {
882 if (!$bundle_entity = \Drupal::entityManager()->getStorage($bundle_entity_type_id)->load($bundle)) {
883 throw new \LogicException(sprintf('Missing bundle entity, entity type %s, entity id %s.', $bundle_entity_type_id, $bundle));
885 $config_dependency = [
887 'name' => $bundle_entity->getConfigDependencyName(),
891 // Depend on the provider of the entity type.
892 $config_dependency = [
894 'name' => $this->getProvider(),
898 return $config_dependency;