3 namespace Drupal\Core\Entity;
5 use Drupal\Component\Plugin\Definition\PluginDefinition;
6 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
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 DependencySerializationTrait;
19 use StringTranslationTrait;
22 * Indicates whether entities should be statically cached.
26 protected $static_cache = TRUE;
29 * Indicates whether the rendered output of entities should be cached.
33 protected $render_cache = TRUE;
36 * Indicates if the persistent cache of field data should be used.
40 protected $persistent_cache = TRUE;
43 * An array of entity keys.
47 protected $entity_keys = [];
50 * The unique identifier of this entity type.
57 * The name of the original entity type class.
59 * This is only set if the class name is changed.
63 protected $originalClass;
66 * An array of handlers.
70 protected $handlers = [];
73 * The name of the default administrative permission.
77 protected $admin_permission;
80 * The permission granularity level.
82 * The allowed values are respectively "entity_type" or "bundle".
86 protected $permission_granularity = 'entity_type';
88 * Link templates using the URI template syntax.
92 protected $links = [];
95 * The name of a callback that returns the label of the entity.
99 * @deprecated in Drupal 8.0.x-dev and will be removed before Drupal 9.0.0.
100 * Use Drupal\Core\Entity\EntityInterface::label() for complex label
101 * generation as needed.
103 * @see \Drupal\Core\Entity\EntityInterface::label()
105 * @todo Remove usages of label_callback https://www.drupal.org/node/2450793.
107 protected $label_callback = NULL;
110 * The name of the entity type which provides bundles.
114 protected $bundle_entity_type = NULL;
117 * The name of the entity type for which bundles are provided.
121 protected $bundle_of = NULL;
124 * The human-readable name of the entity bundles, e.g. Vocabulary.
128 protected $bundle_label = NULL;
131 * The name of the entity type's base table.
135 protected $base_table = NULL;
138 * The name of the entity type's revision data table.
142 protected $revision_data_table = NULL;
145 * The name of the entity type's revision table.
149 protected $revision_table = NULL;
152 * The name of the entity type's data table.
156 protected $data_table = NULL;
159 * Indicates whether the entity data is internal.
163 protected $internal = FALSE;
166 * Indicates whether entities of this type have multilingual support.
170 protected $translatable = FALSE;
173 * Indicates whether the revision form fields should be added to the form.
177 protected $show_revision_ui = FALSE;
180 * The human-readable name of the type.
184 * @see \Drupal\Core\Entity\EntityTypeInterface::getLabel()
186 protected $label = '';
189 * The human-readable label for a collection of entities of the type.
193 * @see \Drupal\Core\Entity\EntityTypeInterface::getCollectionLabel()
195 protected $label_collection = '';
198 * The indefinite singular name of the type.
202 * @see \Drupal\Core\Entity\EntityTypeInterface::getSingularLabel()
204 protected $label_singular = '';
207 * The indefinite plural name of the type.
211 * @see \Drupal\Core\Entity\EntityTypeInterface::getPluralLabel()
213 protected $label_plural = '';
216 * A definite singular/plural name of the type.
218 * Needed keys: "singular" and "plural".
222 * @see \Drupal\Core\Entity\EntityTypeInterface::getCountLabel()
224 protected $label_count = [];
227 * A callable that can be used to provide the entity URI.
231 protected $uri_callback = NULL;
234 * The machine name of the entity type group.
239 * The human-readable name of the entity type group.
241 protected $group_label;
244 * The route name used by field UI to attach its management pages.
248 protected $field_ui_base_route;
251 * Indicates whether this entity type is commonly used as a reference target.
253 * This is used by the Entity reference field to promote an entity type in the
254 * add new field select list in Field UI.
258 protected $common_reference_target = FALSE;
261 * The list cache contexts for this entity type.
265 protected $list_cache_contexts = [];
268 * The list cache tags for this entity type.
272 protected $list_cache_tags = [];
275 * Entity constraint definitions.
279 protected $constraints = [];
282 * Any additional properties and values.
286 protected $additional = [];
289 * Constructs a new EntityType.
291 * @param array $definition
292 * An array of values from the annotation.
294 * @throws \Drupal\Core\Entity\Exception\EntityTypeIdLengthException
295 * Thrown when attempting to instantiate an entity type with too long ID.
297 public function __construct($definition) {
298 // Throw an exception if the entity type ID is longer than 32 characters.
299 if (mb_strlen($definition['id']) > static::ID_MAX_LENGTH) {
300 throw new EntityTypeIdLengthException('Attempt to create an entity type with an ID longer than ' . static::ID_MAX_LENGTH . " characters: {$definition['id']}.");
303 foreach ($definition as $property => $value) {
304 $this->set($property, $value);
308 $this->entity_keys += [
312 'default_langcode' => 'default_langcode',
313 'revision_translation_affected' => 'revision_translation_affected',
316 'access' => 'Drupal\Core\Entity\EntityAccessControlHandler',
318 if (isset($this->handlers['storage'])) {
319 $this->checkStorageClass($this->handlers['storage']);
322 // Automatically add the "EntityChanged" constraint if the entity type
323 // tracks the changed time.
324 if ($this->entityClassImplements(EntityChangedInterface::class)) {
325 $this->addConstraint('EntityChanged');
327 // Automatically add the "EntityUntranslatableFields" constraint if we have
328 // an entity type supporting translatable fields and pending revisions.
329 if ($this->entityClassImplements(ContentEntityInterface::class)) {
330 $this->addConstraint('EntityUntranslatableFields');
333 // Ensure a default list cache tag is set.
334 if (empty($this->list_cache_tags)) {
335 $this->list_cache_tags = [$definition['id'] . '_list'];
342 public function get($property) {
343 if (property_exists($this, $property)) {
344 $value = isset($this->{$property}) ? $this->{$property} : NULL;
347 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
355 public function set($property, $value) {
356 if (property_exists($this, $property)) {
357 $this->{$property} = $value;
360 $this->additional[$property] = $value;
368 public function isInternal() {
369 return $this->internal;
375 public function isStaticallyCacheable() {
376 return $this->static_cache;
382 public function isRenderCacheable() {
383 return $this->render_cache;
389 public function isPersistentlyCacheable() {
390 return $this->persistent_cache;
396 public function getKeys() {
397 return $this->entity_keys;
403 public function getKey($key) {
404 $keys = $this->getKeys();
405 return isset($keys[$key]) ? $keys[$key] : FALSE;
411 public function hasKey($key) {
412 $keys = $this->getKeys();
413 return !empty($keys[$key]);
419 public function getOriginalClass() {
420 return $this->originalClass ?: $this->class;
426 public function setClass($class) {
427 if (!$this->originalClass && $this->class) {
428 // If the original class is currently not set, set it to the current
429 // class, assume that is the original class name.
430 $this->originalClass = $this->class;
433 return parent::setClass($class);
439 public function entityClassImplements($interface) {
440 return is_subclass_of($this->getClass(), $interface);
446 public function isSubclassOf($class) {
447 return $this->entityClassImplements($class);
453 public function getHandlerClasses() {
454 return $this->handlers;
460 public function getHandlerClass($handler_type, $nested = FALSE) {
461 if ($this->hasHandlerClass($handler_type, $nested)) {
462 $handlers = $this->getHandlerClasses();
463 return $nested ? $handlers[$handler_type][$nested] : $handlers[$handler_type];
470 public function setHandlerClass($handler_type, $value) {
471 $this->handlers[$handler_type] = $value;
478 public function hasHandlerClass($handler_type, $nested = FALSE) {
479 $handlers = $this->getHandlerClasses();
480 if (!isset($handlers[$handler_type]) || ($nested && !isset($handlers[$handler_type][$nested]))) {
483 $handler = $handlers[$handler_type];
485 $handler = $handler[$nested];
487 return class_exists($handler);
493 public function getStorageClass() {
494 return $this->getHandlerClass('storage');
500 public function setStorageClass($class) {
501 $this->checkStorageClass($class);
502 $this->handlers['storage'] = $class;
507 * Checks that the provided class is compatible with the current entity type.
509 * @param string $class
510 * The class to check.
512 protected function checkStorageClass($class) {
513 // Nothing to check by default.
519 public function getFormClass($operation) {
520 return $this->getHandlerClass('form', $operation);
526 public function setFormClass($operation, $class) {
527 $this->handlers['form'][$operation] = $class;
534 public function hasFormClasses() {
535 return !empty($this->handlers['form']);
541 public function hasRouteProviders() {
542 return !empty($this->handlers['route_provider']);
548 public function getListBuilderClass() {
549 return $this->getHandlerClass('list_builder');
555 public function setListBuilderClass($class) {
556 $this->handlers['list_builder'] = $class;
563 public function hasListBuilderClass() {
564 return $this->hasHandlerClass('list_builder');
570 public function getViewBuilderClass() {
571 return $this->getHandlerClass('view_builder');
577 public function setViewBuilderClass($class) {
578 $this->handlers['view_builder'] = $class;
585 public function hasViewBuilderClass() {
586 return $this->hasHandlerClass('view_builder');
592 public function getRouteProviderClasses() {
593 return !empty($this->handlers['route_provider']) ? $this->handlers['route_provider'] : [];
599 public function getAccessControlClass() {
600 return $this->getHandlerClass('access');
606 public function setAccessClass($class) {
607 $this->handlers['access'] = $class;
614 public function getAdminPermission() {
615 return $this->admin_permission ?: FALSE;
621 public function getPermissionGranularity() {
622 return $this->permission_granularity;
628 public function getLinkTemplates() {
635 public function getLinkTemplate($key) {
636 $links = $this->getLinkTemplates();
637 return isset($links[$key]) ? $links[$key] : FALSE;
643 public function hasLinkTemplate($key) {
644 $links = $this->getLinkTemplates();
645 return isset($links[$key]);
651 public function setLinkTemplate($key, $path) {
652 if ($path[0] !== '/') {
653 throw new \InvalidArgumentException('Link templates accepts paths, which have to start with a leading slash.');
656 $this->links[$key] = $path;
663 public function getLabelCallback() {
664 return $this->label_callback;
670 public function setLabelCallback($callback) {
671 $this->label_callback = $callback;
678 public function hasLabelCallback() {
679 return isset($this->label_callback);
685 public function getBundleEntityType() {
686 return $this->bundle_entity_type;
692 public function getBundleOf() {
693 return $this->bundle_of;
699 public function getBundleLabel() {
700 // If there is no bundle label defined, try to provide some sensible
702 if (!empty($this->bundle_label)) {
703 return (string) $this->bundle_label;
705 elseif ($bundle_entity_type_id = $this->getBundleEntityType()) {
706 return (string) \Drupal::entityTypeManager()->getDefinition($bundle_entity_type_id)->getLabel();
708 return (string) new TranslatableMarkup('@type_label bundle', ['@type_label' => $this->getLabel()], [], $this->getStringTranslation());
714 public function getBaseTable() {
715 return $this->base_table;
721 public function showRevisionUi() {
722 return $this->isRevisionable() && $this->show_revision_ui;
728 public function isTranslatable() {
729 return !empty($this->translatable);
735 public function isRevisionable() {
736 // Entity types are revisionable if a revision key has been specified.
737 return $this->hasKey('revision');
743 public function getRevisionDataTable() {
744 return $this->revision_data_table;
750 public function getRevisionTable() {
751 return $this->revision_table;
757 public function getDataTable() {
758 return $this->data_table;
764 public function getLabel() {
771 public function getLowercaseLabel() {
772 return mb_strtolower($this->getLabel());
778 public function getCollectionLabel() {
779 if (empty($this->label_collection)) {
780 $label = $this->getLabel();
781 $this->label_collection = new TranslatableMarkup('@label entities', ['@label' => $label], [], $this->getStringTranslation());
783 return $this->label_collection;
789 public function getSingularLabel() {
790 if (empty($this->label_singular)) {
791 $lowercase_label = $this->getLowercaseLabel();
792 $this->label_singular = $lowercase_label;
794 return $this->label_singular;
800 public function getPluralLabel() {
801 if (empty($this->label_plural)) {
802 $lowercase_label = $this->getLowercaseLabel();
803 $this->label_plural = new TranslatableMarkup('@label entities', ['@label' => $lowercase_label], [], $this->getStringTranslation());
805 return $this->label_plural;
811 public function getCountLabel($count) {
812 if (empty($this->label_count)) {
813 return $this->formatPlural($count, '@count @label', '@count @label entities', ['@label' => $this->getLowercaseLabel()], ['context' => 'Entity type label']);
815 $context = isset($this->label_count['context']) ? $this->label_count['context'] : 'Entity type label';
816 return $this->formatPlural($count, $this->label_count['singular'], $this->label_count['plural'], ['context' => $context]);
822 public function getUriCallback() {
823 return $this->uri_callback;
829 public function setUriCallback($callback) {
830 $this->uri_callback = $callback;
837 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;