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',
307 'access' => 'Drupal\Core\Entity\EntityAccessControlHandler',
309 if (isset($this->handlers['storage'])) {
310 $this->checkStorageClass($this->handlers['storage']);
313 // Automatically add the EntityChanged constraint if the entity type tracks
315 if ($this->entityClassImplements(EntityChangedInterface::class) ) {
316 $this->addConstraint('EntityChanged');
319 // Ensure a default list cache tag is set.
320 if (empty($this->list_cache_tags)) {
321 $this->list_cache_tags = [$definition['id'] . '_list'];
328 public function get($property) {
329 if (property_exists($this, $property)) {
330 $value = isset($this->{$property}) ? $this->{$property} : NULL;
333 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
341 public function set($property, $value) {
342 if (property_exists($this, $property)) {
343 $this->{$property} = $value;
346 $this->additional[$property] = $value;
354 public function isStaticallyCacheable() {
355 return $this->static_cache;
361 public function isRenderCacheable() {
362 return $this->render_cache;
368 public function isPersistentlyCacheable() {
369 return $this->persistent_cache;
375 public function getKeys() {
376 return $this->entity_keys;
382 public function getKey($key) {
383 $keys = $this->getKeys();
384 return isset($keys[$key]) ? $keys[$key] : FALSE;
390 public function hasKey($key) {
391 $keys = $this->getKeys();
392 return !empty($keys[$key]);
398 public function getOriginalClass() {
399 return $this->originalClass ?: $this->class;
405 public function setClass($class) {
406 if (!$this->originalClass && $this->class) {
407 // If the original class is currently not set, set it to the current
408 // class, assume that is the original class name.
409 $this->originalClass = $this->class;
412 return parent::setClass($class);
418 public function entityClassImplements($interface) {
419 return is_subclass_of($this->getClass(), $interface);
425 public function isSubclassOf($class) {
426 return $this->entityClassImplements($class);
432 public function getHandlerClasses() {
433 return $this->handlers;
439 public function getHandlerClass($handler_type, $nested = FALSE) {
440 if ($this->hasHandlerClass($handler_type, $nested)) {
441 $handlers = $this->getHandlerClasses();
442 return $nested ? $handlers[$handler_type][$nested] : $handlers[$handler_type];
449 public function setHandlerClass($handler_type, $value) {
450 $this->handlers[$handler_type] = $value;
457 public function hasHandlerClass($handler_type, $nested = FALSE) {
458 $handlers = $this->getHandlerClasses();
459 if (!isset($handlers[$handler_type]) || ($nested && !isset($handlers[$handler_type][$nested]))) {
462 $handler = $handlers[$handler_type];
464 $handler = $handler[$nested];
466 return class_exists($handler);
472 public function getStorageClass() {
473 return $this->getHandlerClass('storage');
479 public function setStorageClass($class) {
480 $this->checkStorageClass($class);
481 $this->handlers['storage'] = $class;
486 * Checks that the provided class is compatible with the current entity type.
488 * @param string $class
489 * The class to check.
491 protected function checkStorageClass($class) {
492 // Nothing to check by default.
498 public function getFormClass($operation) {
499 return $this->getHandlerClass('form', $operation);
505 public function setFormClass($operation, $class) {
506 $this->handlers['form'][$operation] = $class;
513 public function hasFormClasses() {
514 return !empty($this->handlers['form']);
520 public function hasRouteProviders() {
521 return !empty($this->handlers['route_provider']);
527 public function getListBuilderClass() {
528 return $this->getHandlerClass('list_builder');
534 public function setListBuilderClass($class) {
535 $this->handlers['list_builder'] = $class;
542 public function hasListBuilderClass() {
543 return $this->hasHandlerClass('list_builder');
549 public function getViewBuilderClass() {
550 return $this->getHandlerClass('view_builder');
556 public function setViewBuilderClass($class) {
557 $this->handlers['view_builder'] = $class;
564 public function hasViewBuilderClass() {
565 return $this->hasHandlerClass('view_builder');
571 public function getRouteProviderClasses() {
572 return !empty($this->handlers['route_provider']) ? $this->handlers['route_provider'] : [];
578 public function getAccessControlClass() {
579 return $this->getHandlerClass('access');
585 public function setAccessClass($class) {
586 $this->handlers['access'] = $class;
593 public function getAdminPermission() {
594 return $this->admin_permission ?: FALSE;
600 public function getPermissionGranularity() {
601 return $this->permission_granularity;
607 public function getLinkTemplates() {
614 public function getLinkTemplate($key) {
615 $links = $this->getLinkTemplates();
616 return isset($links[$key]) ? $links[$key] : FALSE;
622 public function hasLinkTemplate($key) {
623 $links = $this->getLinkTemplates();
624 return isset($links[$key]);
630 public function setLinkTemplate($key, $path) {
631 if ($path[0] !== '/') {
632 throw new \InvalidArgumentException('Link templates accepts paths, which have to start with a leading slash.');
635 $this->links[$key] = $path;
642 public function getLabelCallback() {
643 return $this->label_callback;
649 public function setLabelCallback($callback) {
650 $this->label_callback = $callback;
657 public function hasLabelCallback() {
658 return isset($this->label_callback);
664 public function getBundleEntityType() {
665 return $this->bundle_entity_type;
671 public function getBundleOf() {
672 return $this->bundle_of;
678 public function getBundleLabel() {
679 return (string) $this->bundle_label;
685 public function getBaseTable() {
686 return $this->base_table;
692 public function showRevisionUi() {
693 return $this->isRevisionable() && $this->show_revision_ui;
699 public function isTranslatable() {
700 return !empty($this->translatable);
706 public function isRevisionable() {
707 // Entity types are revisionable if a revision key has been specified.
708 return $this->hasKey('revision');
714 public function getRevisionDataTable() {
715 return $this->revision_data_table;
721 public function getRevisionTable() {
722 return $this->revision_table;
728 public function getDataTable() {
729 return $this->data_table;
735 public function getLabel() {
742 public function getLowercaseLabel() {
743 return Unicode::strtolower($this->getLabel());
749 public function getCollectionLabel() {
750 if (empty($this->label_collection)) {
751 $label = $this->getLabel();
752 $this->label_collection = new TranslatableMarkup('@label entities', ['@label' => $label], [], $this->getStringTranslation());
754 return $this->label_collection;
760 public function getSingularLabel() {
761 if (empty($this->label_singular)) {
762 $lowercase_label = $this->getLowercaseLabel();
763 $this->label_singular = $lowercase_label;
765 return $this->label_singular;
771 public function getPluralLabel() {
772 if (empty($this->label_plural)) {
773 $lowercase_label = $this->getLowercaseLabel();
774 $this->label_plural = new TranslatableMarkup('@label entities', ['@label' => $lowercase_label], [], $this->getStringTranslation());
776 return $this->label_plural;
782 public function getCountLabel($count) {
783 if (empty($this->label_count)) {
784 return $this->formatPlural($count, '@count @label', '@count @label entities', ['@label' => $this->getLowercaseLabel()], ['context' => 'Entity type label']);
786 $context = isset($this->label_count['context']) ? $this->label_count['context'] : 'Entity type label';
787 return $this->formatPlural($count, $this->label_count['singular'], $this->label_count['plural'], ['context' => $context]);
793 public function getUriCallback() {
794 return $this->uri_callback;
800 public function setUriCallback($callback) {
801 $this->uri_callback = $callback;
808 public function getGroup() {
816 public function getGroupLabel() {
817 return !empty($this->group_label) ? $this->group_label : $this->t('Other', [], ['context' => 'Entity type group']);
823 public function getListCacheContexts() {
824 return $this->list_cache_contexts;
830 public function getListCacheTags() {
831 return $this->list_cache_tags;
837 public function getConfigDependencyKey() {
838 // Return 'content' for the default implementation as important distinction
839 // is that dependencies on other configuration entities are hard
840 // dependencies and have to exist before creating the dependent entity.
847 public function isCommonReferenceTarget() {
848 return $this->common_reference_target;
854 public function getConstraints() {
855 return $this->constraints;
861 public function setConstraints(array $constraints) {
862 $this->constraints = $constraints;
869 public function addConstraint($constraint_name, $options = NULL) {
870 $this->constraints[$constraint_name] = $options;
877 public function getBundleConfigDependency($bundle) {
878 // If this entity type uses entities to manage its bundles then depend on
879 // the bundle entity.
880 if ($bundle_entity_type_id = $this->getBundleEntityType()) {
881 if (!$bundle_entity = \Drupal::entityManager()->getStorage($bundle_entity_type_id)->load($bundle)) {
882 throw new \LogicException(sprintf('Missing bundle entity, entity type %s, entity id %s.', $bundle_entity_type_id, $bundle));
884 $config_dependency = [
886 'name' => $bundle_entity->getConfigDependencyName(),
890 // Depend on the provider of the entity type.
891 $config_dependency = [
893 'name' => $this->getProvider(),
897 return $config_dependency;