HeartbeatStreamHtmlRouteProvider.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Drupal\heartbeat8;
  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 stream entities.
  8. *
  9. * @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
  10. * @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
  11. */
  12. class HeartbeatStreamHtmlRouteProvider 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 ($settings_form_route = $this->getSettingsFormRoute($entity_type)) {
  35. $collection->add("$entity_type_id.settings", $settings_form_route);
  36. }
  37. return $collection;
  38. }
  39. /**
  40. * Gets the collection route.
  41. *
  42. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  43. * The entity type.
  44. *
  45. * @return \Symfony\Component\Routing\Route|null
  46. * The generated route, if available.
  47. */
  48. protected function getCollectionRoute(EntityTypeInterface $entity_type) {
  49. if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) {
  50. $entity_type_id = $entity_type->id();
  51. $route = new Route($entity_type->getLinkTemplate('collection'));
  52. $route
  53. ->setDefaults([
  54. '_entity_list' => $entity_type_id,
  55. '_title' => "{$entity_type->getLabel()} list",
  56. ])
  57. ->setRequirement('_permission', 'access heartbeat stream overview')
  58. ->setOption('_admin_route', TRUE);
  59. return $route;
  60. }
  61. }
  62. /**
  63. * Gets the version history route.
  64. *
  65. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  66. * The entity type.
  67. *
  68. * @return \Symfony\Component\Routing\Route|null
  69. * The generated route, if available.
  70. */
  71. protected function getHistoryRoute(EntityTypeInterface $entity_type) {
  72. if ($entity_type->hasLinkTemplate('version-history')) {
  73. $route = new Route($entity_type->getLinkTemplate('version-history'));
  74. $route
  75. ->setDefaults([
  76. '_title' => "{$entity_type->getLabel()} revisions",
  77. '_controller' => '\Drupal\heartbeat8\Controller\HeartbeatStreamController::revisionOverview',
  78. ])
  79. ->setRequirement('_permission', 'access heartbeat stream revisions')
  80. ->setOption('_admin_route', TRUE);
  81. return $route;
  82. }
  83. }
  84. /**
  85. * Gets the revision route.
  86. *
  87. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  88. * The entity type.
  89. *
  90. * @return \Symfony\Component\Routing\Route|null
  91. * The generated route, if available.
  92. */
  93. protected function getRevisionRoute(EntityTypeInterface $entity_type) {
  94. if ($entity_type->hasLinkTemplate('revision')) {
  95. $route = new Route($entity_type->getLinkTemplate('revision'));
  96. $route
  97. ->setDefaults([
  98. '_controller' => '\Drupal\heartbeat8\Controller\HeartbeatStreamController::revisionShow',
  99. '_title_callback' => '\Drupal\heartbeat8\Controller\HeartbeatStreamController::revisionPageTitle',
  100. ])
  101. ->setRequirement('_permission', 'access heartbeat stream revisions')
  102. ->setOption('_admin_route', TRUE);
  103. return $route;
  104. }
  105. }
  106. /**
  107. * Gets the revision revert 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 getRevisionRevertRoute(EntityTypeInterface $entity_type) {
  116. if ($entity_type->hasLinkTemplate('revision_revert')) {
  117. $route = new Route($entity_type->getLinkTemplate('revision_revert'));
  118. $route
  119. ->setDefaults([
  120. '_form' => '\Drupal\heartbeat8\Form\HeartbeatStreamRevisionRevertForm',
  121. '_title' => 'Revert to earlier revision',
  122. ])
  123. ->setRequirement('_permission', 'revert all heartbeat stream revisions')
  124. ->setOption('_admin_route', TRUE);
  125. return $route;
  126. }
  127. }
  128. /**
  129. * Gets the revision delete route.
  130. *
  131. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  132. * The entity type.
  133. *
  134. * @return \Symfony\Component\Routing\Route|null
  135. * The generated route, if available.
  136. */
  137. protected function getRevisionDeleteRoute(EntityTypeInterface $entity_type) {
  138. if ($entity_type->hasLinkTemplate('revision_delete')) {
  139. $route = new Route($entity_type->getLinkTemplate('revision_delete'));
  140. $route
  141. ->setDefaults([
  142. '_form' => '\Drupal\heartbeat8\Form\HeartbeatStreamRevisionDeleteForm',
  143. '_title' => 'Delete earlier revision',
  144. ])
  145. ->setRequirement('_permission', 'delete all heartbeat stream revisions')
  146. ->setOption('_admin_route', TRUE);
  147. return $route;
  148. }
  149. }
  150. /**
  151. * Gets the settings form route.
  152. *
  153. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  154. * The entity type.
  155. *
  156. * @return \Symfony\Component\Routing\Route|null
  157. * The generated route, if available.
  158. */
  159. protected function getSettingsFormRoute(EntityTypeInterface $entity_type) {
  160. if (!$entity_type->getBundleEntityType()) {
  161. $route = new Route("/admin/structure/{$entity_type->id()}/settings");
  162. $route
  163. ->setDefaults([
  164. '_form' => 'Drupal\heartbeat8\Form\HeartbeatStreamSettingsForm',
  165. '_title' => "{$entity_type->getLabel()} settings",
  166. ])
  167. ->setRequirement('_permission', $entity_type->getAdminPermission())
  168. ->setOption('_admin_route', TRUE);
  169. return $route;
  170. }
  171. }
  172. }