PageRenderTime 30ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Entity/EntityTypeManager.php

https://gitlab.com/geeta7/drupal
PHP | 261 lines | 131 code | 37 blank | 93 comment | 15 complexity | 584e7c5747468e37a554edc7d7a6b208 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Core\Entity\EntityTypeManager.
  5. */
  6. namespace Drupal\Core\Entity;
  7. use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
  8. use Drupal\Component\Plugin\Exception\PluginNotFoundException;
  9. use Drupal\Core\Cache\CacheBackendInterface;
  10. use Drupal\Core\DependencyInjection\ClassResolverInterface;
  11. use Drupal\Core\Entity\Exception\InvalidLinkTemplateException;
  12. use Drupal\Core\Extension\ModuleHandlerInterface;
  13. use Drupal\Core\Plugin\DefaultPluginManager;
  14. use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
  15. use Drupal\Core\StringTranslation\TranslationInterface;
  16. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  17. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  18. /**
  19. * Manages entity type plugin definitions.
  20. *
  21. * Each entity type definition array is set in the entity type's
  22. * annotation and altered by hook_entity_type_alter().
  23. *
  24. * @see \Drupal\Core\Entity\Annotation\EntityType
  25. * @see \Drupal\Core\Entity\EntityInterface
  26. * @see \Drupal\Core\Entity\EntityTypeInterface
  27. * @see hook_entity_type_alter()
  28. */
  29. class EntityTypeManager extends DefaultPluginManager implements EntityTypeManagerInterface, ContainerAwareInterface {
  30. use ContainerAwareTrait;
  31. /**
  32. * Contains instantiated handlers keyed by handler type and entity type.
  33. *
  34. * @var array
  35. */
  36. protected $handlers = [];
  37. /**
  38. * The string translation service.
  39. *
  40. * @var \Drupal\Core\StringTranslation\TranslationInterface
  41. */
  42. protected $stringTranslation;
  43. /**
  44. * The class resolver.
  45. *
  46. * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
  47. */
  48. protected $classResolver;
  49. /**
  50. * Constructs a new Entity plugin manager.
  51. *
  52. * @param \Traversable $namespaces
  53. * An object that implements \Traversable which contains the root paths
  54. * keyed by the corresponding namespace to look for plugin implementations,
  55. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  56. * The module handler.
  57. * @param \Drupal\Core\Cache\CacheBackendInterface $cache
  58. * The cache backend to use.
  59. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
  60. * The string translation.
  61. * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
  62. * The class resolver.
  63. */
  64. public function __construct(\Traversable $namespaces, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, TranslationInterface $string_translation, ClassResolverInterface $class_resolver) {
  65. parent::__construct('Entity', $namespaces, $module_handler, 'Drupal\Core\Entity\EntityInterface');
  66. $this->setCacheBackend($cache, 'entity_type', ['entity_types']);
  67. $this->alterInfo('entity_type');
  68. $this->discovery = new AnnotatedClassDiscovery('Entity', $namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
  69. $this->stringTranslation = $string_translation;
  70. $this->classResolver = $class_resolver;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function processDefinition(&$definition, $plugin_id) {
  76. /** @var \Drupal\Core\Entity\EntityTypeInterface $definition */
  77. parent::processDefinition($definition, $plugin_id);
  78. // All link templates must have a leading slash.
  79. foreach ((array) $definition->getLinkTemplates() as $link_relation_name => $link_template) {
  80. if ($link_template[0] != '/') {
  81. throw new InvalidLinkTemplateException("Link template '$link_relation_name' for entity type '$plugin_id' must start with a leading slash, the current link template is '$link_template'");
  82. }
  83. }
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function findDefinitions() {
  89. $definitions = $this->getDiscovery()->getDefinitions();
  90. // Directly call the hook implementations to pass the definitions to them
  91. // by reference, so new entity types can be added.
  92. foreach ($this->moduleHandler->getImplementations('entity_type_build') as $module) {
  93. $function = $module . '_' . 'entity_type_build';
  94. $function($definitions);
  95. }
  96. foreach ($definitions as $plugin_id => $definition) {
  97. $this->processDefinition($definition, $plugin_id);
  98. }
  99. $this->alterDefinitions($definitions);
  100. return $definitions;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getDefinition($entity_type_id, $exception_on_invalid = TRUE) {
  106. if (($entity_type = parent::getDefinition($entity_type_id, FALSE)) && class_exists($entity_type->getClass())) {
  107. return $entity_type;
  108. }
  109. elseif (!$exception_on_invalid) {
  110. return NULL;
  111. }
  112. throw new PluginNotFoundException($entity_type_id, sprintf('The "%s" entity type does not exist.', $entity_type_id));
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function clearCachedDefinitions() {
  118. parent::clearCachedDefinitions();
  119. $this->handlers = [];
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function useCaches($use_caches = FALSE) {
  125. parent::useCaches($use_caches);
  126. if (!$use_caches) {
  127. $this->handlers = [];
  128. }
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function hasHandler($entity_type, $handler_type) {
  134. if ($definition = $this->getDefinition($entity_type, FALSE)) {
  135. return $definition->hasHandlerClass($handler_type);
  136. }
  137. return FALSE;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function getStorage($entity_type) {
  143. return $this->getHandler($entity_type, 'storage');
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function getListBuilder($entity_type) {
  149. return $this->getHandler($entity_type, 'list_builder');
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getFormObject($entity_type, $operation) {
  155. if (!$class = $this->getDefinition($entity_type, TRUE)->getFormClass($operation)) {
  156. throw new InvalidPluginDefinitionException($entity_type, sprintf('The "%s" entity type did not specify a "%s" form class.', $entity_type, $operation));
  157. }
  158. $form_object = $this->classResolver->getInstanceFromDefinition($class);
  159. return $form_object
  160. ->setStringTranslation($this->stringTranslation)
  161. ->setModuleHandler($this->moduleHandler)
  162. ->setEntityTypeManager($this)
  163. ->setOperation($operation)
  164. // The entity manager cannot be injected due to a circular dependency.
  165. // @todo Remove this set call in https://www.drupal.org/node/2603542.
  166. ->setEntityManager(\Drupal::entityManager());
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function getRouteProviders($entity_type) {
  172. if (!isset($this->handlers['route_provider'][$entity_type])) {
  173. $route_provider_classes = $this->getDefinition($entity_type, TRUE)->getRouteProviderClasses();
  174. foreach ($route_provider_classes as $type => $class) {
  175. $this->handlers['route_provider'][$entity_type][$type] = $this->createHandlerInstance($class, $this->getDefinition($entity_type));
  176. }
  177. }
  178. return isset($this->handlers['route_provider'][$entity_type]) ? $this->handlers['route_provider'][$entity_type] : [];
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function getViewBuilder($entity_type) {
  184. return $this->getHandler($entity_type, 'view_builder');
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function getAccessControlHandler($entity_type) {
  190. return $this->getHandler($entity_type, 'access');
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function getHandler($entity_type, $handler_type) {
  196. if (!isset($this->handlers[$handler_type][$entity_type])) {
  197. $definition = $this->getDefinition($entity_type);
  198. $class = $definition->getHandlerClass($handler_type);
  199. if (!$class) {
  200. throw new InvalidPluginDefinitionException($entity_type, sprintf('The "%s" entity type did not specify a %s handler.', $entity_type, $handler_type));
  201. }
  202. $this->handlers[$handler_type][$entity_type] = $this->createHandlerInstance($class, $definition);
  203. }
  204. return $this->handlers[$handler_type][$entity_type];
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function createHandlerInstance($class, EntityTypeInterface $definition = NULL) {
  210. if (is_subclass_of($class, 'Drupal\Core\Entity\EntityHandlerInterface')) {
  211. $handler = $class::createInstance($this->container, $definition);
  212. }
  213. else {
  214. $handler = new $class($definition);
  215. }
  216. if (method_exists($handler, 'setModuleHandler')) {
  217. $handler->setModuleHandler($this->moduleHandler);
  218. }
  219. if (method_exists($handler, 'setStringTranslation')) {
  220. $handler->setStringTranslation($this->stringTranslation);
  221. }
  222. return $handler;
  223. }
  224. }