View source
<?php
namespace Drupal\flexiform\Routing;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\field_ui\Routing\RouteSubscriber as FieldUIRouteSubscriber;
use Drupal\flexiform\FormComponentTypePluginManager;
use Drupal\flexiform\FormComponent\FormComponentTypeCreateableInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends FieldUIRouteSubscriber {
protected $entityTypeManager;
protected $formComponentTypeManager;
public function __construct(EntityManagerInterface $manager, EntityTypeManagerInterface $entity_type_manager, FormComponentTypePluginManager $form_component_type_manager) {
parent::__construct($manager);
$this->entityTypeManager = $entity_type_manager;
$this->formComponentTypeManager = $form_component_type_manager;
}
protected function alterRoutes(RouteCollection $collection) {
foreach ($this->entityTypeManager
->getStorage('entity_form_mode')
->loadMultiple() as $form_mode_id => $form_mode) {
if ($exposure_settings = $form_mode
->getThirdPartySetting('flexiform', 'exposure')) {
$options = [];
foreach ($exposure_settings['parameters'] as $name => $parameter_info) {
$options['parameters'][$name] = [
'type' => 'entity:' . $parameter_info['entity_type'],
];
}
$options['parameters']['form_mode'] = [
'type' => 'entity:entity_form_mode',
];
$route = new Route($exposure_settings['path'], [
'_controller' => '\\Drupal\\flexiform\\Controller\\FlexiformController::formModePage',
'_title_callback' => '\\Drupal\\flexiform\\Controller\\FlexiformController::formModePageTitle',
'form_mode' => $form_mode_id,
], [
'_flexiform_form_mode_page_access_check' => 'TRUE',
], $options);
$collection
->add("flexiform.form_mode_page.{$form_mode_id}", $route);
}
}
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($route_name = $entity_type
->get('field_ui_base_route')) {
if (!($entity_route = $collection
->get($route_name))) {
continue;
}
$path = $entity_route
->getPath();
$options = $entity_route
->getOptions();
if ($bundle_entity_type = $entity_type
->getBundleEntityType()) {
$options['parameters'][$bundle_entity_type] = [
'type' => 'entity:' . $bundle_entity_type,
];
}
$options['_field_ui'] = TRUE;
$options['_flexiform_form_entity'] = TRUE;
$defaults = [
'entity_type_id' => $entity_type_id,
];
if (strpos($path, '{bundle}') === FALSE) {
$defaults['bundle'] = !$entity_type
->hasKey('bundle') ? $entity_type_id : '';
}
$route = new Route("{$path}/form-display/{form_mode_name}/add-form-entity", [
'_form' => '\\Drupal\\flexiform\\Form\\FormEntityAddForm',
'_title' => 'Add form entity',
] + $defaults, [
'_field_ui_form_mode_access' => 'administer ' . $entity_type_id . ' form display',
], $options);
$collection
->add("entity.entity_form_display.{$entity_type_id}.form_mode.form_entity_add", $route);
$route = new Route("{$path}/form-display/{form_mode_name}/edit-form-entity/{entity_namespace}", [
'_form' => '\\Drupal\\flexiform\\Form\\FormEntityEditForm',
'_title' => 'Configure form entity',
] + $defaults, [
'_field_ui_form_mode_access' => 'administer ' . $entity_type_id . ' form display',
], $options);
$collection
->add("entity.entity_form_display.{$entity_type_id}.form_mode.form_entity_edit", $route);
foreach ($this->formComponentTypeManager
->getDefinitions() as $plugin_id => $definition) {
$component_type = $this->formComponentTypeManager
->createInstance($plugin_id);
if ($component_type instanceof FormComponentTypeCreateableInterface) {
$route = new Route("{$path}/form-display/add-component-" . str_replace('_', '-', $plugin_id), [
'component_type' => $plugin_id,
'form_mode_name' => 'default',
'_form' => '\\Drupal\\flexiform\\Form\\FormComponentAddForm',
'_title' => 'Add ' . $definition['label'],
] + $defaults, [
'_permission' => 'administer ' . $entity_type_id . ' display',
], $options);
$collection
->add("entity.entity_form_display.{$entity_type_id}.default.component_type.{$plugin_id}.add", $route);
$route = new Route("{$path}/form-display/{form_mode_name}/add-component-" . str_replace('_', '-', $plugin_id), [
'component_type' => $plugin_id,
'_form' => '\\Drupal\\flexiform\\Form\\FormComponentAddForm',
'_title' => 'Add ' . $definition['label'],
] + $defaults, [
'_permission' => 'administer ' . $entity_type_id . ' display',
], $options);
$collection
->add("entity.entity_form_display.{$entity_type_id}.form_mode.component_type.{$plugin_id}.add", $route);
}
}
}
}
}
}