StatusHtmlRouteProvider.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Drupal\statusmessage;
  3. use Drupal\Core\Entity\EntityTypeInterface;
  4. use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
  5. use Symfony\Component\Routing\Route;
  6. /**
  7. * Provides routes for Status entities.
  8. *
  9. * @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
  10. * @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
  11. */
  12. class StatusHtmlRouteProvider extends AdminHtmlRouteProvider {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function getRoutes(EntityTypeInterface $entity_type) {
  17. $collection = parent::getRoutes($entity_type);
  18. $entity_type_id = $entity_type->id();
  19. if ($collection_route = $this->getCollectionRoute($entity_type)) {
  20. $collection->add("entity.{$entity_type_id}.collection", $collection_route);
  21. }
  22. if ($add_form_route = $this->getAddFormRoute($entity_type)) {
  23. $collection->add("entity.{$entity_type_id}.add_form", $add_form_route);
  24. }
  25. $add_page_route = $this->getAddPageRoute($entity_type);
  26. $collection->add("$entity_type_id.add_page", $add_page_route);
  27. if ($settings_form_route = $this->getSettingsFormRoute($entity_type)) {
  28. $collection->add("$entity_type_id.settings", $settings_form_route);
  29. }
  30. return $collection;
  31. }
  32. /**
  33. * Gets the collection route.
  34. *
  35. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  36. * The entity type.
  37. *
  38. * @return \Symfony\Component\Routing\Route|null
  39. * The generated route, if available.
  40. */
  41. protected function getCollectionRoute(EntityTypeInterface $entity_type) {
  42. if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) {
  43. $entity_type_id = $entity_type->id();
  44. $route = new Route($entity_type->getLinkTemplate('collection'));
  45. $route
  46. ->setDefaults([
  47. '_entity_list' => $entity_type_id,
  48. '_title' => "{$entity_type->getLabel()} list",
  49. ])
  50. ->setRequirement('_permission', 'view status entities')
  51. ->setOption('_admin_route', TRUE);
  52. return $route;
  53. }
  54. }
  55. /**
  56. * Gets the add-form route.
  57. *
  58. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  59. * The entity type.
  60. *
  61. * @return \Symfony\Component\Routing\Route|null
  62. * The generated route, if available.
  63. */
  64. protected function getAddFormRoute(EntityTypeInterface $entity_type) {
  65. if ($entity_type->hasLinkTemplate('add-form')) {
  66. $entity_type_id = $entity_type->id();
  67. $parameters = [
  68. $entity_type_id => ['type' => 'entity:' . $entity_type_id],
  69. ];
  70. $route = new Route($entity_type->getLinkTemplate('add-form'));
  71. $bundle_entity_type_id = $entity_type->getBundleEntityType();
  72. // Content entities with bundles are added via a dedicated controller.
  73. $route
  74. ->setDefaults([
  75. '_controller' => 'Drupal\statusmessage\Controller\StatusAddController::addForm',
  76. '_title_callback' => 'Drupal\statusmessage\Controller\StatusAddController::getAddFormTitle',
  77. ])
  78. ->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_entity_type_id . '}');
  79. $parameters[$bundle_entity_type_id] = ['type' => 'entity:' . $bundle_entity_type_id];
  80. $route
  81. ->setOption('parameters', $parameters)
  82. ->setOption('_admin_route', TRUE);
  83. return $route;
  84. }
  85. }
  86. /**
  87. * Gets the add page route.
  88. *
  89. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  90. * The entity type.
  91. *
  92. * @return \Symfony\Component\Routing\Route|null
  93. * The generated route, if available.
  94. */
  95. protected function getAddPageRoute(EntityTypeInterface $entity_type) {
  96. $route = new Route("/admin/structure/{$entity_type->id()}/add");
  97. $route
  98. ->setDefaults([
  99. '_controller' => 'Drupal\statusmessage\Controller\StatusAddController::add',
  100. '_title' => "Add {$entity_type->getLabel()}",
  101. ])
  102. ->setRequirement('_entity_create_access', $entity_type->id())
  103. ->setOption('_admin_route', TRUE);
  104. return $route;
  105. }
  106. /**
  107. * Gets the settings form route.
  108. *
  109. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  110. * The entity type.
  111. *
  112. * @return \Symfony\Component\Routing\Route|null
  113. * The generated route, if available.
  114. */
  115. protected function getSettingsFormRoute(EntityTypeInterface $entity_type) {
  116. if (!$entity_type->getBundleEntityType()) {
  117. $route = new Route("/admin/structure/{$entity_type->id()}/settings");
  118. $route
  119. ->setDefaults([
  120. '_form' => 'Drupal\statusmessage\Form\StatusSettingsForm',
  121. '_title' => "{$entity_type->getLabel()} settings",
  122. ])
  123. ->setRequirement('_permission', $entity_type->getAdminPermission())
  124. ->setOption('_admin_route', TRUE);
  125. return $route;
  126. }
  127. }
  128. }