HeartbeatHtmlRouteProvider.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Drupal\heartbeat;
  3. use Drupal\Core\Entity\EntityTypeInterface;
  4. use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
  5. use Symfony\Component\Routing\Route;
  6. /**
  7. * Provides routes for Heartbeat entities.
  8. *
  9. * @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
  10. * @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
  11. */
  12. class HeartbeatHtmlRouteProvider 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 ($history_route = $this->getHistoryRoute($entity_type)) {
  23. $collection->add("entity.{$entity_type_id}.version_history", $history_route);
  24. }
  25. if ($revision_route = $this->getRevisionRoute($entity_type)) {
  26. $collection->add("entity.{$entity_type_id}.revision", $revision_route);
  27. }
  28. if ($revert_route = $this->getRevisionRevertRoute($entity_type)) {
  29. $collection->add("entity.{$entity_type_id}.revision_revert", $revert_route);
  30. }
  31. if ($delete_route = $this->getRevisionDeleteRoute($entity_type)) {
  32. $collection->add("entity.{$entity_type_id}.revision_delete", $delete_route);
  33. }
  34. if ($translation_route = $this->getRevisionTranslationRevertRoute($entity_type)) {
  35. $collection->add("{$entity_type_id}.revision_revert_translation_confirm", $translation_route);
  36. }
  37. if ($settings_form_route = $this->getSettingsFormRoute($entity_type)) {
  38. $collection->add("$entity_type_id.settings", $settings_form_route);
  39. }
  40. return $collection;
  41. }
  42. /**
  43. * Gets the collection route.
  44. *
  45. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  46. * The entity type.
  47. *
  48. * @return \Symfony\Component\Routing\Route|null
  49. * The generated route, if available.
  50. */
  51. protected function getCollectionRoute(EntityTypeInterface $entity_type) {
  52. if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) {
  53. $entity_type_id = $entity_type->id();
  54. $route = new Route($entity_type->getLinkTemplate('collection'));
  55. $route
  56. ->setDefaults([
  57. '_entity_list' => $entity_type_id,
  58. '_title' => "{$entity_type->getLabel()} list",
  59. ])
  60. ->setRequirement('_permission', 'access heartbeat overview')
  61. ->setOption('_admin_route', TRUE);
  62. return $route;
  63. }
  64. }
  65. /**
  66. * Gets the version history route.
  67. *
  68. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  69. * The entity type.
  70. *
  71. * @return \Symfony\Component\Routing\Route|null
  72. * The generated route, if available.
  73. */
  74. protected function getHistoryRoute(EntityTypeInterface $entity_type) {
  75. if ($entity_type->hasLinkTemplate('version-history')) {
  76. $route = new Route($entity_type->getLinkTemplate('version-history'));
  77. $route
  78. ->setDefaults([
  79. '_title' => "{$entity_type->getLabel()} revisions",
  80. '_controller' => '\Drupal\heartbeat\Controller\HeartbeatController::revisionOverview',
  81. ])
  82. ->setRequirement('_permission', 'access heartbeat revisions')
  83. ->setOption('_admin_route', TRUE);
  84. return $route;
  85. }
  86. }
  87. /**
  88. * Gets the revision route.
  89. *
  90. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  91. * The entity type.
  92. *
  93. * @return \Symfony\Component\Routing\Route|null
  94. * The generated route, if available.
  95. */
  96. protected function getRevisionRoute(EntityTypeInterface $entity_type) {
  97. if ($entity_type->hasLinkTemplate('revision')) {
  98. $route = new Route($entity_type->getLinkTemplate('revision'));
  99. $route
  100. ->setDefaults([
  101. '_controller' => '\Drupal\heartbeat\Controller\HeartbeatController::revisionShow',
  102. '_title_callback' => '\Drupal\heartbeat\Controller\HeartbeatController::revisionPageTitle',
  103. ])
  104. ->setRequirement('_permission', 'access heartbeat revisions')
  105. ->setOption('_admin_route', TRUE);
  106. return $route;
  107. }
  108. }
  109. /**
  110. * Gets the revision revert route.
  111. *
  112. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  113. * The entity type.
  114. *
  115. * @return \Symfony\Component\Routing\Route|null
  116. * The generated route, if available.
  117. */
  118. protected function getRevisionRevertRoute(EntityTypeInterface $entity_type) {
  119. if ($entity_type->hasLinkTemplate('revision_revert')) {
  120. $route = new Route($entity_type->getLinkTemplate('revision_revert'));
  121. $route
  122. ->setDefaults([
  123. '_form' => '\Drupal\heartbeat\Form\HeartbeatRevisionRevertForm',
  124. '_title' => 'Revert to earlier revision',
  125. ])
  126. ->setRequirement('_permission', 'revert all heartbeat revisions')
  127. ->setOption('_admin_route', TRUE);
  128. return $route;
  129. }
  130. }
  131. /**
  132. * Gets the revision delete route.
  133. *
  134. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  135. * The entity type.
  136. *
  137. * @return \Symfony\Component\Routing\Route|null
  138. * The generated route, if available.
  139. */
  140. protected function getRevisionDeleteRoute(EntityTypeInterface $entity_type) {
  141. if ($entity_type->hasLinkTemplate('revision_delete')) {
  142. $route = new Route($entity_type->getLinkTemplate('revision_delete'));
  143. $route
  144. ->setDefaults([
  145. '_form' => '\Drupal\heartbeat\Form\HeartbeatRevisionDeleteForm',
  146. '_title' => 'Delete earlier revision',
  147. ])
  148. ->setRequirement('_permission', 'delete all heartbeat revisions')
  149. ->setOption('_admin_route', TRUE);
  150. return $route;
  151. }
  152. }
  153. /**
  154. * Gets the revision translation revert route.
  155. *
  156. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  157. * The entity type.
  158. *
  159. * @return \Symfony\Component\Routing\Route|null
  160. * The generated route, if available.
  161. */
  162. protected function getRevisionTranslationRevertRoute(EntityTypeInterface $entity_type) {
  163. if ($entity_type->hasLinkTemplate('translation_revert')) {
  164. $route = new Route($entity_type->getLinkTemplate('translation_revert'));
  165. $route
  166. ->setDefaults([
  167. '_form' => '\Drupal\heartbeat\Form\HeartbeatRevisionRevertTranslationForm',
  168. '_title' => 'Revert to earlier revision of a translation',
  169. ])
  170. ->setRequirement('_permission', 'revert all heartbeat revisions')
  171. ->setOption('_admin_route', TRUE);
  172. return $route;
  173. }
  174. }
  175. /**
  176. * Gets the settings form route.
  177. *
  178. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  179. * The entity type.
  180. *
  181. * @return \Symfony\Component\Routing\Route|null
  182. * The generated route, if available.
  183. */
  184. protected function getSettingsFormRoute(EntityTypeInterface $entity_type) {
  185. if (!$entity_type->getBundleEntityType()) {
  186. $route = new Route("/admin/structure/{$entity_type->id()}/settings");
  187. $route
  188. ->setDefaults([
  189. '_form' => 'Drupal\heartbeat\Form\HeartbeatSettingsForm',
  190. '_title' => "{$entity_type->getLabel()} settings",
  191. ])
  192. ->setRequirement('_permission', $entity_type->getAdminPermission())
  193. ->setOption('_admin_route', TRUE);
  194. return $route;
  195. }
  196. }
  197. }