Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Routing / FormRoute.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Routing;
4
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;
13
14 /**
15  * @Converter(
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" }
19  * )
20  */
21 class FormRoute extends ContentRoute {
22
23   /**
24    * @var \Drupal\drupalmoduleupgrader\Utility\FormConverter
25    */
26   protected $formConverter;
27
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;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function getName(TargetInterface $target, RouteWrapper $route) {
37     $name = $target->id() . '.' . $this->unPrefix($route['page arguments'][0], $target->id());
38
39     $arguments = array_filter(array_slice($route['page arguments'], 1), 'is_string');
40     if ($arguments) {
41       $name .= '_' . implode('_', $arguments);
42     }
43
44     return $name;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function buildParameterMap(TargetInterface $target, RouteWrapper $route) {
51     $map = parent::buildParameterMap($target, $route);
52
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));
59     }
60
61     return $map;
62   }
63
64   /**
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.
68    *
69    * @param array $input
70    *  The input array.
71    *
72    * @param int $offset
73    *  The offset to add to the keys.
74    *
75    * @return array
76    */
77   private function bumpKeys(array $input, $offset = 0) {
78     $output = [];
79
80     foreach ($input as $key => $value) {
81       $output[ $key + $offset ] = $value;
82     }
83
84     return $output;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
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));
93   }
94
95   protected function getController(TargetInterface $target, RouteWrapper $route) {
96     return $this->formConverter->get($target, $route['page arguments'][0])->render();
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function buildRouteDefinition(TargetInterface $target, RouteWrapper $route) {
103     $definition = parent::buildRouteDefinition($target, $route);
104     $definition->setDefault('_form', $this->getController($target, $route)->getName()->getAbsolutePath());
105
106     return $definition;
107   }
108
109 }