localgov_alert_banner-1.8.5/src/AlertBannerEntityHtmlRouteProvider.php
src/AlertBannerEntityHtmlRouteProvider.php
<?php
declare(strict_types=1);
namespace Drupal\localgov_alert_banner;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides routes for Alert banner entities.
*
* @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class AlertBannerEntityHtmlRouteProvider extends AdminHtmlRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
$entity_type_id = $entity_type->id();
if ($status_form_route = $this->getStatusFormRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.status_form", $status_form_route);
}
if ($collection_route = $this->getCollectionRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.collection", $collection_route);
}
return $collection;
}
/**
* Gets the status-form route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getStatusFormRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('status-form')) {
$entity_type_id = $entity_type->id();
$route = new Route($entity_type->getLinkTemplate('status-form'));
$route
->addDefaults([
'_entity_form' => "{$entity_type_id}.status",
'_title_callback' => '\Drupal\localgov_alert_banner\Controller\AlertBannerEntityController::getStatusFormTitle',
])
->setRequirement('_entity_access', "{$entity_type_id}.update")
->setOption('parameters', [
$entity_type_id => ['type' => 'entity:' . $entity_type_id],
]);
// Entity types with serial IDs can specify this in their route
// requirements, improving the matching process.
if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
$route->setRequirement($entity_type_id, '\d+');
}
return $route;
}
}
/**
* Gets the collection route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getCollectionRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('collection')) {
$entity_type_id = $entity_type->id();
$route = new Route($entity_type->getLinkTemplate('collection'));
$route
->setDefaults([
'_entity_list' => $entity_type_id,
'_title' => "{$entity_type->getLabel()} list",
])
->setRequirement('_permission', 'access localgov alert banner listing page')
->setOption('_admin_route', TRUE);
return $route;
}
}
}
