3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Routing;
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Routing\RouteProviderInterface;
7 use Drupal\Core\StringTranslation\TranslationInterface;
8 use Drupal\drupalmoduleupgrader\Routing\Drupal7\RouteWrapper;
9 use Drupal\drupalmoduleupgrader\Routing\ParameterMap;
10 use Drupal\drupalmoduleupgrader\TargetInterface;
11 use Drupal\drupalmoduleupgrader\Utility\FormConverterFactory;
12 use Psr\Log\LoggerInterface;
16 * id = "drupal_get_form",
17 * description = @Translation("Converts a drupal_get_form() menu item to a _form route."),
18 * dependencies = { "router.route_provider", "plugin.manager.drupalmoduleupgrader.rewriter", "drupalmoduleupgrader.form_converter" }
21 class FormRoute extends ContentRoute {
24 * @var \Drupal\drupalmoduleupgrader\Utility\FormConverter
26 protected $formConverter;
28 public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translator, LoggerInterface $log, RouteProviderInterface $route_provider, PluginManagerInterface $rewriters, FormConverterFactory $form_converter) {
29 parent::__construct($configuration, $plugin_id, $plugin_definition, $translator, $log, $route_provider, $rewriters);
30 $this->formConverter = $form_converter;
36 public function getName(TargetInterface $target, RouteWrapper $route) {
37 $name = $target->id() . '.' . $this->unPrefix($route['page arguments'][0], $target->id());
39 $arguments = array_filter(array_slice($route['page arguments'], 1), 'is_string');
41 $name .= '_' . implode('_', $arguments);
50 protected function buildParameterMap(TargetInterface $target, RouteWrapper $route) {
51 $map = parent::buildParameterMap($target, $route);
53 $indexer = $target->getIndexer('function');
54 if ($indexer->has($route['page arguments'][0])) {
55 $builder = $indexer->get($route['page arguments'][0]);
56 $parameters = $this->bumpKeys(array_slice($builder->getParameters()->toArray(), 2), 2);
57 $arguments = $this->bumpKeys(array_slice($route['page arguments'], 1), 2);
58 $map->merge(new ParameterMap($route->getPath(), $parameters, $arguments));
65 * Returns a copy of the input array with the keys increased by $offset. This
66 * only works on numerically indexed arrays; I don't know what it does to
67 * associative arrays, but probably nothing good.
73 * The offset to add to the keys.
77 private function bumpKeys(array $input, $offset = 0) {
80 foreach ($input as $key => $value) {
81 $output[ $key + $offset ] = $value;
90 public function buildRoute(TargetInterface $target, RouteWrapper $route) {
91 $controller = $this->formConverter->get($target, $route['page arguments'][0])->build();
92 $target->getIndexer('class')->addFile($this->writeClass($target, $controller));
95 protected function getController(TargetInterface $target, RouteWrapper $route) {
96 return $this->formConverter->get($target, $route['page arguments'][0])->render();
102 public function buildRouteDefinition(TargetInterface $target, RouteWrapper $route) {
103 $definition = parent::buildRouteDefinition($target, $route);
104 $definition->setDefault('_form', $this->getController($target, $route)->getName()->getAbsolutePath());