RouteSubscriber.php in Workflow 8
File
src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\workflow\Routing;
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) {
$field_list = workflow_get_workflow_fields_by_entity_type();
foreach ($field_list as $entityTypeId => $fields) {
$path = "/{$entityTypeId}/{{$entityTypeId}}/workflow";
$route = $this
->getEntityLoadRoute($entityTypeId, $path);
$collection
->add("entity.{$entityTypeId}.workflow_history", $route);
foreach ($fields as $field_name => $field) {
$path = "/{$entityTypeId}/{{$entityTypeId}}/workflow/{$field_name}";
$route = $this
->getEntityLoadRoute($entityTypeId, $path);
$collection
->add("entity.{$entityTypeId}.workflow_history.{$field_name}", $route);
}
}
}
protected function getEntityLoadRoute($entityTypeId, $path) {
$route = new Route($path, [
'_controller' => '\\Drupal\\workflow\\Controller\\WorkflowTransitionListController::historyOverview',
'_title' => 'Workflow history',
], [
'_custom_access' => '\\Drupal\\workflow\\Access\\WorkflowHistoryAccess::access',
], [
'_admin_route' => TRUE,
'_workflow_entity_type_id' => $entityTypeId,
'parameters' => [
$entityTypeId => [
'type' => 'entity:' . $entityTypeId,
],
],
]);
return $route;
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
100,
];
return $events;
}
public function listWorkflowFields() {
$fieldType = 'workflow';
$fieldStorageConfigs = $this->entityTypeManager
->getStorage('field_storage_config')
->loadByProperties([
'type' => $fieldType,
]);
if (!$fieldStorageConfigs) {
return [];
}
$availableItems = [];
foreach ($fieldStorageConfigs as $fieldStorage) {
$availableItems[] = $fieldStorage;
}
return $availableItems;
}
}