EntityController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace Drupal\Core\Entity\Controller;
  3. use Drupal\Core\Entity\EntityDescriptionInterface;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityRepositoryInterface;
  6. use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
  7. use Drupal\Core\Entity\EntityTypeManagerInterface;
  8. use Drupal\Core\Entity\EntityTypeInterface;
  9. use Drupal\Core\Link;
  10. use Drupal\Core\Render\RendererInterface;
  11. use Drupal\Core\Routing\RouteMatchInterface;
  12. use Drupal\Core\Routing\UrlGeneratorInterface;
  13. use Drupal\Core\Routing\UrlGeneratorTrait;
  14. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  15. use Drupal\Core\StringTranslation\StringTranslationTrait;
  16. use Drupal\Core\StringTranslation\TranslationInterface;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. /**
  19. * Provides the add-page and title callbacks for entities.
  20. *
  21. * It provides:
  22. * - The add-page callback.
  23. * - An add title callback for entity types.
  24. * - An add title callback for entity types with bundles.
  25. * - A view title callback.
  26. * - An edit title callback.
  27. * - A delete title callback.
  28. */
  29. class EntityController implements ContainerInjectionInterface {
  30. use StringTranslationTrait;
  31. use UrlGeneratorTrait;
  32. /**
  33. * The entity manager.
  34. *
  35. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  36. */
  37. protected $entityTypeManager;
  38. /**
  39. * The entity type bundle info.
  40. *
  41. * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
  42. */
  43. protected $entityTypeBundleInfo;
  44. /**
  45. * The entity repository.
  46. *
  47. * @var \Drupal\Core\Entity\EntityRepositoryInterface
  48. */
  49. protected $entityRepository;
  50. /**
  51. * The renderer.
  52. *
  53. * @var \Drupal\Core\Render\RendererInterface
  54. */
  55. protected $renderer;
  56. /**
  57. * Constructs a new EntityController.
  58. *
  59. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  60. * The entity type manager.
  61. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
  62. * The entity type bundle info.
  63. * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
  64. * The entity repository.
  65. * @param \Drupal\Core\Render\RendererInterface $renderer
  66. * The renderer.
  67. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
  68. * The string translation.
  69. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
  70. * The url generator.
  71. */
  72. public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityRepositoryInterface $entity_repository, RendererInterface $renderer, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator) {
  73. $this->entityTypeManager = $entity_type_manager;
  74. $this->entityTypeBundleInfo = $entity_type_bundle_info;
  75. $this->entityRepository = $entity_repository;
  76. $this->renderer = $renderer;
  77. $this->stringTranslation = $string_translation;
  78. $this->urlGenerator = $url_generator;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public static function create(ContainerInterface $container) {
  84. return new static(
  85. $container->get('entity_type.manager'),
  86. $container->get('entity_type.bundle.info'),
  87. $container->get('entity.repository'),
  88. $container->get('renderer'),
  89. $container->get('string_translation'),
  90. $container->get('url_generator')
  91. );
  92. }
  93. /**
  94. * Displays add links for the available bundles.
  95. *
  96. * Redirects to the add form if there's only one bundle available.
  97. *
  98. * @param string $entity_type_id
  99. * The entity type ID.
  100. *
  101. * @return \Symfony\Component\HttpFoundation\RedirectResponse|array
  102. * If there's only one available bundle, a redirect response.
  103. * Otherwise, a render array with the add links for each bundle.
  104. */
  105. public function addPage($entity_type_id) {
  106. $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
  107. $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
  108. $bundle_key = $entity_type->getKey('bundle');
  109. $bundle_entity_type_id = $entity_type->getBundleEntityType();
  110. $build = [
  111. '#theme' => 'entity_add_list',
  112. '#bundles' => [],
  113. ];
  114. if ($bundle_entity_type_id) {
  115. $bundle_argument = $bundle_entity_type_id;
  116. $bundle_entity_type = $this->entityTypeManager->getDefinition($bundle_entity_type_id);
  117. $bundle_entity_type_label = $bundle_entity_type->getLowercaseLabel();
  118. $build['#cache']['tags'] = $bundle_entity_type->getListCacheTags();
  119. // Build the message shown when there are no bundles.
  120. $link_text = $this->t('Add a new @entity_type.', ['@entity_type' => $bundle_entity_type_label]);
  121. $link_route_name = 'entity.' . $bundle_entity_type->id() . '.add_form';
  122. $build['#add_bundle_message'] = $this->t('There is no @entity_type yet. @add_link', [
  123. '@entity_type' => $bundle_entity_type_label,
  124. '@add_link' => Link::createFromRoute($link_text, $link_route_name)->toString(),
  125. ]);
  126. // Filter out the bundles the user doesn't have access to.
  127. $access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
  128. foreach ($bundles as $bundle_name => $bundle_info) {
  129. $access = $access_control_handler->createAccess($bundle_name, NULL, [], TRUE);
  130. if (!$access->isAllowed()) {
  131. unset($bundles[$bundle_name]);
  132. }
  133. $this->renderer->addCacheableDependency($build, $access);
  134. }
  135. // Add descriptions from the bundle entities.
  136. $bundles = $this->loadBundleDescriptions($bundles, $bundle_entity_type);
  137. }
  138. else {
  139. $bundle_argument = $bundle_key;
  140. }
  141. $form_route_name = 'entity.' . $entity_type_id . '.add_form';
  142. // Redirect if there's only one bundle available.
  143. if (count($bundles) == 1) {
  144. $bundle_names = array_keys($bundles);
  145. $bundle_name = reset($bundle_names);
  146. return $this->redirect($form_route_name, [$bundle_argument => $bundle_name]);
  147. }
  148. // Prepare the #bundles array for the template.
  149. foreach ($bundles as $bundle_name => $bundle_info) {
  150. $build['#bundles'][$bundle_name] = [
  151. 'label' => $bundle_info['label'],
  152. 'description' => isset($bundle_info['description']) ? $bundle_info['description'] : '',
  153. 'add_link' => Link::createFromRoute($bundle_info['label'], $form_route_name, [$bundle_argument => $bundle_name]),
  154. ];
  155. }
  156. return $build;
  157. }
  158. /**
  159. * Provides a generic add title callback for an entity type.
  160. *
  161. * @param string $entity_type_id
  162. * The entity type ID.
  163. *
  164. * @return string
  165. * The title for the entity add page.
  166. */
  167. public function addTitle($entity_type_id) {
  168. $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
  169. return $this->t('Add @entity-type', ['@entity-type' => $entity_type->getLowercaseLabel()]);
  170. }
  171. /**
  172. * Provides a generic add title callback for entities with bundles.
  173. *
  174. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  175. * The route match.
  176. * @param string $entity_type_id
  177. * The entity type ID.
  178. * @param string $bundle_parameter
  179. * The name of the route parameter that holds the bundle.
  180. *
  181. * @return string
  182. * The title for the entity add page, if the bundle was found.
  183. */
  184. public function addBundleTitle(RouteMatchInterface $route_match, $entity_type_id, $bundle_parameter) {
  185. $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
  186. // If the entity has bundle entities, the parameter might have been upcasted
  187. // so fetch the raw parameter.
  188. $bundle = $route_match->getRawParameter($bundle_parameter);
  189. if ((count($bundles) > 1) && isset($bundles[$bundle])) {
  190. return $this->t('Add @bundle', ['@bundle' => $bundles[$bundle]['label']]);
  191. }
  192. // If the entity supports bundles generally, but only has a single bundle,
  193. // the bundle is probably something like 'Default' so that it preferable to
  194. // use the entity type label.
  195. else {
  196. return $this->addTitle($entity_type_id);
  197. }
  198. }
  199. /**
  200. * Provides a generic title callback for a single entity.
  201. *
  202. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  203. * The route match.
  204. * @param \Drupal\Core\Entity\EntityInterface $_entity
  205. * (optional) An entity, passed in directly from the request attributes.
  206. *
  207. * @return string|null
  208. * The title for the entity view page, if an entity was found.
  209. */
  210. public function title(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
  211. if ($entity = $this->doGetEntity($route_match, $_entity)) {
  212. return $entity->label();
  213. }
  214. }
  215. /**
  216. * Provides a generic edit title callback.
  217. *
  218. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  219. * The route match.
  220. * @param \Drupal\Core\Entity\EntityInterface $_entity
  221. * (optional) An entity, passed in directly from the request attributes.
  222. *
  223. * @return string|null
  224. * The title for the entity edit page, if an entity was found.
  225. */
  226. public function editTitle(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
  227. if ($entity = $this->doGetEntity($route_match, $_entity)) {
  228. return $this->t('Edit %label', ['%label' => $entity->label()]);
  229. }
  230. }
  231. /**
  232. * Provides a generic delete title callback.
  233. *
  234. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  235. * The route match.
  236. * @param \Drupal\Core\Entity\EntityInterface $_entity
  237. * (optional) An entity, passed in directly from the request attributes, and
  238. * set in \Drupal\Core\Entity\Enhancer\EntityRouteEnhancer.
  239. *
  240. * @return string
  241. * The title for the entity delete page.
  242. */
  243. public function deleteTitle(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
  244. if ($entity = $this->doGetEntity($route_match, $_entity)) {
  245. return $this->t('Delete %label', ['%label' => $entity->label()]);
  246. }
  247. }
  248. /**
  249. * Determines the entity.
  250. *
  251. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  252. * The route match.
  253. * @param \Drupal\Core\Entity\EntityInterface $_entity
  254. * (optional) The entity, set in
  255. * \Drupal\Core\Entity\Enhancer\EntityRouteEnhancer.
  256. *
  257. * @return \Drupal\Core\Entity\EntityInterface|null
  258. * The entity, if it is passed in directly or if the first parameter of the
  259. * active route is an entity; otherwise, NULL.
  260. */
  261. protected function doGetEntity(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) {
  262. if ($_entity) {
  263. $entity = $_entity;
  264. }
  265. else {
  266. // Let's look up in the route object for the name of upcasted values.
  267. foreach ($route_match->getParameters() as $parameter) {
  268. if ($parameter instanceof EntityInterface) {
  269. $entity = $parameter;
  270. break;
  271. }
  272. }
  273. }
  274. if (isset($entity)) {
  275. return $this->entityRepository->getTranslationFromContext($entity);
  276. }
  277. }
  278. /**
  279. * Expands the bundle information with descriptions, if known.
  280. *
  281. * @param array $bundles
  282. * An array of bundle information.
  283. * @param \Drupal\Core\Entity\EntityTypeInterface $bundle_entity_type
  284. * The bundle entity type definition.
  285. *
  286. * @return array
  287. * The expanded array of bundle information.
  288. */
  289. protected function loadBundleDescriptions(array $bundles, EntityTypeInterface $bundle_entity_type) {
  290. if (!$bundle_entity_type->entityClassImplements(EntityDescriptionInterface::class)) {
  291. return $bundles;
  292. }
  293. $bundle_names = array_keys($bundles);
  294. $storage = $this->entityTypeManager->getStorage($bundle_entity_type->id());
  295. /** @var \Drupal\Core\Entity\EntityDescriptionInterface[] $bundle_entities */
  296. $bundle_entities = $storage->loadMultiple($bundle_names);
  297. foreach ($bundles as $bundle_name => &$bundle_info) {
  298. if (isset($bundle_entities[$bundle_name])) {
  299. $bundle_info['description'] = $bundle_entities[$bundle_name]->getDescription();
  300. }
  301. }
  302. return $bundles;
  303. }
  304. }