View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Component\Plugin\Definition\PluginDefinition;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\Exception\EntityTypeIdLengthException;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
class EntityType extends PluginDefinition implements EntityTypeInterface {
use DependencySerializationTrait;
use StringTranslationTrait;
protected $static_cache = TRUE;
protected $render_cache = TRUE;
protected $persistent_cache = TRUE;
protected $entity_keys = [];
protected $id;
protected $originalClass;
protected $handlers = [];
protected $admin_permission;
protected $permission_granularity = 'entity_type';
protected $links = [];
protected $bundle_entity_type = NULL;
protected $bundle_of = NULL;
protected $bundle_label = NULL;
protected $base_table = NULL;
protected $revision_data_table = NULL;
protected $revision_table = NULL;
protected $data_table = NULL;
protected $internal = FALSE;
protected $translatable = FALSE;
protected $show_revision_ui = FALSE;
protected $label = '';
protected $label_collection = '';
protected $label_singular = '';
protected $label_plural = '';
protected $label_count = [];
protected $uri_callback = NULL;
protected $group;
protected $group_label;
protected $field_ui_base_route;
protected $common_reference_target = FALSE;
protected $list_cache_contexts = [];
protected $list_cache_tags = [];
protected $constraints = [];
protected $additional = [];
public function __construct($definition) {
if (mb_strlen($definition['id']) > static::ID_MAX_LENGTH) {
throw new EntityTypeIdLengthException('Attempt to create an entity type with an ID longer than ' . static::ID_MAX_LENGTH . " characters: {$definition['id']}.");
}
foreach ($definition as $property => $value) {
$this
->set($property, $value);
}
$this->entity_keys += [
'revision' => '',
'bundle' => '',
'langcode' => '',
'default_langcode' => 'default_langcode',
'revision_translation_affected' => 'revision_translation_affected',
];
$this->handlers += [
'access' => 'Drupal\\Core\\Entity\\EntityAccessControlHandler',
];
if (isset($this->handlers['storage'])) {
$this
->checkStorageClass($this->handlers['storage']);
}
if ($this
->entityClassImplements(EntityChangedInterface::class)) {
$this
->addConstraint('EntityChanged');
}
if ($this
->entityClassImplements(ContentEntityInterface::class)) {
$this
->addConstraint('EntityUntranslatableFields');
}
if (empty($this->list_cache_tags)) {
$this->list_cache_tags = [
$definition['id'] . '_list',
];
}
}
public function get($property) {
if (property_exists($this, $property)) {
$value = $this->{$property} ?? NULL;
}
else {
$value = $this->additional[$property] ?? NULL;
}
return $value;
}
public function set($property, $value) {
if (property_exists($this, $property)) {
$this->{$property} = $value;
}
else {
$this->additional[$property] = $value;
}
return $this;
}
public function isInternal() {
return $this->internal;
}
public function isStaticallyCacheable() {
return $this->static_cache;
}
public function isRenderCacheable() {
return $this->render_cache;
}
public function isPersistentlyCacheable() {
return $this->persistent_cache;
}
public function getKeys() {
return $this->entity_keys;
}
public function getKey($key) {
$keys = $this
->getKeys();
return $keys[$key] ?? FALSE;
}
public function hasKey($key) {
$keys = $this
->getKeys();
return !empty($keys[$key]);
}
public function getOriginalClass() {
return $this->originalClass ?: $this->class;
}
public function setClass($class) {
if (!$this->originalClass && $this->class) {
$this->originalClass = $this->class;
}
return parent::setClass($class);
}
public function entityClassImplements($interface) {
return is_subclass_of($this
->getClass(), $interface);
}
public function getHandlerClasses() {
return $this->handlers;
}
public function getHandlerClass($handler_type, $nested = FALSE) {
if ($this
->hasHandlerClass($handler_type, $nested)) {
$handlers = $this
->getHandlerClasses();
return $nested ? $handlers[$handler_type][$nested] : $handlers[$handler_type];
}
return NULL;
}
public function setHandlerClass($handler_type, $value) {
$this->handlers[$handler_type] = $value;
return $this;
}
public function hasHandlerClass($handler_type, $nested = FALSE) {
$handlers = $this
->getHandlerClasses();
if (!isset($handlers[$handler_type]) || $nested && !isset($handlers[$handler_type][$nested])) {
return FALSE;
}
$handler = $handlers[$handler_type];
if ($nested) {
$handler = $handler[$nested];
}
return class_exists($handler);
}
public function getStorageClass() {
return $this
->getHandlerClass('storage');
}
public function setStorageClass($class) {
$this
->checkStorageClass($class);
$this->handlers['storage'] = $class;
return $this;
}
protected function checkStorageClass($class) {
}
public function getFormClass($operation) {
return $this
->getHandlerClass('form', $operation);
}
public function setFormClass($operation, $class) {
$this->handlers['form'][$operation] = $class;
return $this;
}
public function hasFormClasses() {
return !empty($this->handlers['form']);
}
public function hasRouteProviders() {
return !empty($this->handlers['route_provider']);
}
public function getListBuilderClass() {
return $this
->getHandlerClass('list_builder');
}
public function setListBuilderClass($class) {
$this->handlers['list_builder'] = $class;
return $this;
}
public function hasListBuilderClass() {
return $this
->hasHandlerClass('list_builder');
}
public function getViewBuilderClass() {
return $this
->getHandlerClass('view_builder');
}
public function setViewBuilderClass($class) {
$this->handlers['view_builder'] = $class;
return $this;
}
public function hasViewBuilderClass() {
return $this
->hasHandlerClass('view_builder');
}
public function getRouteProviderClasses() {
return !empty($this->handlers['route_provider']) ? $this->handlers['route_provider'] : [];
}
public function getAccessControlClass() {
return $this
->getHandlerClass('access');
}
public function setAccessClass($class) {
$this->handlers['access'] = $class;
return $this;
}
public function getAdminPermission() {
return $this->admin_permission ?: FALSE;
}
public function getPermissionGranularity() {
return $this->permission_granularity;
}
public function getLinkTemplates() {
return $this->links;
}
public function getLinkTemplate($key) {
$links = $this
->getLinkTemplates();
return $links[$key] ?? FALSE;
}
public function hasLinkTemplate($key) {
$links = $this
->getLinkTemplates();
return isset($links[$key]);
}
public function setLinkTemplate($key, $path) {
if ($path[0] !== '/') {
throw new \InvalidArgumentException('Link templates accepts paths, which have to start with a leading slash.');
}
$this->links[$key] = $path;
return $this;
}
public function getBundleEntityType() {
return $this->bundle_entity_type;
}
public function getBundleOf() {
return $this->bundle_of;
}
public function getBundleLabel() {
if (!empty($this->bundle_label)) {
return (string) $this->bundle_label;
}
elseif ($bundle_entity_type_id = $this
->getBundleEntityType()) {
return (string) \Drupal::entityTypeManager()
->getDefinition($bundle_entity_type_id)
->getLabel();
}
return (string) new TranslatableMarkup('@type_label bundle', [
'@type_label' => $this
->getLabel(),
], [], $this
->getStringTranslation());
}
public function getBaseTable() {
return $this->base_table;
}
public function showRevisionUi() {
return $this
->isRevisionable() && $this->show_revision_ui;
}
public function isTranslatable() {
return !empty($this->translatable);
}
public function isRevisionable() {
return $this
->hasKey('revision');
}
public function getRevisionDataTable() {
return $this->revision_data_table;
}
public function getRevisionTable() {
return $this->revision_table;
}
public function getDataTable() {
return $this->data_table;
}
public function getLabel() {
return $this->label;
}
public function getCollectionLabel() {
if (empty($this->label_collection)) {
$label = $this
->getLabel();
$this->label_collection = new TranslatableMarkup('@label entities', [
'@label' => $label,
], [], $this
->getStringTranslation());
}
return $this->label_collection;
}
public function getSingularLabel() {
if (empty($this->label_singular)) {
$lowercase_label = mb_strtolower($this
->getLabel());
$this->label_singular = $lowercase_label;
}
return $this->label_singular;
}
public function getPluralLabel() {
if (empty($this->label_plural)) {
$lowercase_label = $this
->getSingularLabel();
$this->label_plural = new TranslatableMarkup('@label entities', [
'@label' => $lowercase_label,
], [], $this
->getStringTranslation());
}
return $this->label_plural;
}
public function getCountLabel($count) {
if (empty($this->label_count)) {
return $this
->formatPlural($count, '@count @label', '@count @label entities', [
'@label' => $this
->getSingularLabel(),
], [
'context' => 'Entity type label',
]);
}
$options = [];
if (isset($this->label_count['context'])) {
$options['context'] = $this->label_count['context'];
}
return $this
->formatPlural($count, $this->label_count['singular'], $this->label_count['plural'], [], $options);
}
public function getUriCallback() {
return $this->uri_callback;
}
public function setUriCallback($callback) {
$this->uri_callback = $callback;
return $this;
}
public function getGroup() {
return $this->group;
}
public function getGroupLabel() {
return !empty($this->group_label) ? $this->group_label : $this
->t('Other', [], [
'context' => 'Entity type group',
]);
}
public function getListCacheContexts() {
return $this->list_cache_contexts;
}
public function getListCacheTags() {
return $this->list_cache_tags;
}
public function getConfigDependencyKey() {
return 'content';
}
public function isCommonReferenceTarget() {
return $this->common_reference_target;
}
public function getConstraints() {
return $this->constraints;
}
public function setConstraints(array $constraints) {
$this->constraints = $constraints;
return $this;
}
public function addConstraint($constraint_name, $options = NULL) {
$this->constraints[$constraint_name] = $options;
return $this;
}
public function getBundleConfigDependency($bundle) {
if ($bundle_entity_type_id = $this
->getBundleEntityType()) {
if (!($bundle_entity = \Drupal::entityTypeManager()
->getStorage($bundle_entity_type_id)
->load($bundle))) {
throw new \LogicException(sprintf('Missing bundle entity, entity type %s, entity id %s.', $bundle_entity_type_id, $bundle));
}
$config_dependency = [
'type' => 'config',
'name' => $bundle_entity
->getConfigDependencyName(),
];
}
else {
$config_dependency = [
'type' => 'module',
'name' => $this
->getProvider(),
];
}
return $config_dependency;
}
}