View source
<?php
namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Routing;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\drupalmoduleupgrader\Routing\Drupal7\RouteWrapper;
use Drupal\drupalmoduleupgrader\Routing\ParameterMap;
use Drupal\drupalmoduleupgrader\TargetInterface;
use Drupal\drupalmoduleupgrader\Utility\FormConverterFactory;
use Psr\Log\LoggerInterface;
class FormRoute extends ContentRoute {
protected $formConverter;
public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translator, LoggerInterface $log, RouteProviderInterface $route_provider, PluginManagerInterface $rewriters, FormConverterFactory $form_converter) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $translator, $log, $route_provider, $rewriters);
$this->formConverter = $form_converter;
}
public function getName(TargetInterface $target, RouteWrapper $route) {
$name = $target
->id() . '.' . $this
->unPrefix($route['page arguments'][0], $target
->id());
$arguments = array_filter(array_slice($route['page arguments'], 1), 'is_string');
if ($arguments) {
$name .= '_' . implode('_', $arguments);
}
return $name;
}
protected function buildParameterMap(TargetInterface $target, RouteWrapper $route) {
$map = parent::buildParameterMap($target, $route);
$indexer = $target
->getIndexer('function');
if ($indexer
->has($route['page arguments'][0])) {
$builder = $indexer
->get($route['page arguments'][0]);
$parameters = $this
->bumpKeys(array_slice($builder
->getParameters()
->toArray(), 2), 2);
$arguments = $this
->bumpKeys(array_slice($route['page arguments'], 1), 2);
$map
->merge(new ParameterMap($route
->getPath(), $parameters, $arguments));
}
return $map;
}
private function bumpKeys(array $input, $offset = 0) {
$output = [];
foreach ($input as $key => $value) {
$output[$key + $offset] = $value;
}
return $output;
}
public function buildRoute(TargetInterface $target, RouteWrapper $route) {
$controller = $this->formConverter
->get($target, $route['page arguments'][0])
->build();
$target
->getIndexer('class')
->addFile($this
->writeClass($target, $controller));
}
protected function getController(TargetInterface $target, RouteWrapper $route) {
return $this->formConverter
->get($target, $route['page arguments'][0])
->render();
}
public function buildRouteDefinition(TargetInterface $target, RouteWrapper $route) {
$definition = parent::buildRouteDefinition($target, $route);
$definition
->setDefault('_form', $this
->getController($target, $route)
->getName()
->getAbsolutePath());
return $definition;
}
}