RouteSubscriber.php in Pardot Integration 2.x
File
src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\pardot\Routing;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_manager) {
$this->entityTypeManager = $entity_manager;
}
protected function alterRoutes(RouteCollection $collection) {
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type_id === 'contact_form' || $entity_type_id === 'webform') {
if ($route = $this
->getFormRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.pardot_form_mapping", $route);
}
}
}
return $collection;
}
protected function getFormRoute(EntityTypeInterface $entity_type) {
if ($route = new Route($entity_type
->getLinkTemplate('pardot-form-mapping'))) {
$entity_type_id = $entity_type
->id();
if ($entity_type_id === 'contact_form') {
$method = 'renderContactFormMapping';
}
if ($entity_type_id === 'webform') {
$method = 'renderWebformFormMapping';
}
if ($method) {
$route
->setDefaults([
'_title' => "Pardot Form Map",
'_controller' => '\\Drupal\\pardot\\Controller\\PardotFormMappingController::' . $method,
])
->setRequirement('_permission', 'administer pardot form map')
->setOption('parameters', [
$entity_type_id => 'entity:' . $entity_type_id,
])
->setOption('_admin_route', TRUE);
return $route;
}
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
100,
];
return $events;
}
}