You are here

public function ContentRoute::buildRoute in Drupal 7 to 8/9 Module Upgrader 8

Builds the Drupal 8 route, making any needed changes to the original module and/or callback.

Overrides RouteConverterInterface::buildRoute

1 method overrides ContentRoute::buildRoute()
FormRoute::buildRoute in src/Plugin/DMU/Routing/FormRoute.php
Builds the Drupal 8 route, making any needed changes to the original module and/or callback.

File

src/Plugin/DMU/Routing/ContentRoute.php, line 162

Class

ContentRoute
Plugin annotation @Converter( id = "default", description = @Translation("Converts a menu item to a _controller route."), dependencies = { "router.route_provider", "plugin.manager.drupalmoduleupgrader.rewriter" } )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Routing

Code

public function buildRoute(TargetInterface $target, Drupal7Route $route) {
  $definition = $this
    ->buildRouteDefinition($target, $route);
  $map = $this
    ->buildParameterMap($target, $route);
  $map
    ->applyRoute($definition
    ->unwrap());
  $indexer = $target
    ->getIndexer('function');
  foreach ($map
    ->toArray() as $function_name => $parameters) {
    if ($parameters && $indexer
      ->has($function_name)) {

      /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
      $function = $indexer
        ->get($function_name);
      foreach ($parameters as $parameter_name => $info) {
        $parameter = $function
          ->getParameterByName($parameter_name)
          ->setName($info['name'], TRUE);
        if (isset($info['type'])) {
          $plugin_id = '_rewriter:' . $info['type'];
          if ($this->rewriters
            ->hasDefinition($plugin_id)) {
            $this->rewriters
              ->createInstance($plugin_id)
              ->rewrite($parameter);
          }
        }
      }
    }
  }
  $class_indexer = $target
    ->getIndexer('class');
  if ($class_indexer
    ->has('DefaultController')) {
    $controller = $class_indexer
      ->get('DefaultController');
  }
  else {
    $controller = $this
      ->getController($target, $route);
    $class_indexer
      ->addFile($this
      ->writeClass($target, $controller));
  }
  if ($indexer
    ->has($route['title callback'])) {
    if (!$controller
      ->hasMethod($route['title callback'])) {
      $indexer
        ->get($route['title callback'])
        ->cloneAsMethodOf($controller);
    }
  }
  if ($indexer
    ->has($route['access callback'])) {
    $func = $indexer
      ->get($route['access callback']);
    $returns = $func
      ->find(Filter::isInstanceOf('\\Pharborist\\ReturnStatementNode'));
    foreach ($returns as $ret) {
      $call = ClassMethodCallNode::create('\\Drupal\\Core\\Access\\AccessResult', 'allowedIf')
        ->appendArgument($ret
        ->getExpression());
      $ret
        ->replaceWith(ReturnStatementNode::create($call));
    }

    // The access callback always receives an $account parameter.
    if ($func
      ->hasParameter('account')) {
      $func
        ->getParameter('account')
        ->setTypeHint('Drupal\\Core\\Session\\AccountInterface');
    }
    else {
      $account = ParameterNode::create('account')
        ->setTypeHint('Drupal\\Core\\Session\\AccountInterface');
      $func
        ->appendParameter($account);
    }
    if (!$controller
      ->hasMethod($route['access callback'])) {
      $func
        ->cloneAsMethodOf($controller);
    }
  }
  if ($indexer
    ->has($route['page callback'])) {
    if (!$controller
      ->hasMethod($route['page callback'])) {
      $indexer
        ->get($route['page callback'])
        ->cloneAsMethodOf($controller);
    }
  }
  $this
    ->writeClass($target, $controller);
}