AgreementRouteProvider.php in Agreement 3.0.x
File
src/Entity/Routing/AgreementRouteProvider.php
View source
<?php
namespace Drupal\agreement\Entity\Routing;
use Drupal\agreement\Entity\Agreement;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Routing\EntityRouteProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class AgreementRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = new RouteCollection();
$agreements = $this->entityTypeManager
->getStorage('agreement')
->loadMultiple();
if (!empty($agreements)) {
foreach ($agreements as $id => $agreement) {
$collection
->add("agreement.{$id}", $this
->getCanonicalRouteForEntity($agreement));
}
}
return $collection;
}
protected function getCanonicalRouteForEntity(Agreement $agreement) {
$route = new Route($agreement
->get('path'));
$route
->addDefaults([
'_form' => '\\Drupal\\agreement\\Form\\AgreementForm',
'_title_callback' => '\\Drupal\\agreement\\Form\\AgreementForm::title',
'agreement' => $agreement
->id(),
])
->setRequirements([
'_permission' => 'access content',
])
->setOption('parameters', [
'agreement' => [
'type' => 'entity:agreement',
],
]);
return $route;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity_type.manager'));
}
}