You are here

public function URL::rewrite in Drupal 7 to 8/9 Module Upgrader 8

Tries to rewrite the original function call.

Parameters

\Pharborist\Functions\FunctionCallNode $call: The original function call.

\Drupal\drupalmoduleupgrader\TargetInterface $target: The target module.

Return value

\Pharborist\Node|null If the original function call is returned (determined by object identity), the function call is not replaced. If a different node is returned, it will replace the original call. And if nothing is returned, the original call is commented out with a FIXME.

Overrides FunctionCallModifier::rewrite

1 call to URL::rewrite()
L::rewrite in src/Plugin/DMU/Converter/Functions/L.php
Tries to rewrite the original function call.
1 method overrides URL::rewrite()
L::rewrite in src/Plugin/DMU/Converter/Functions/L.php
Tries to rewrite the original function call.

File

src/Plugin/DMU/Converter/Functions/URL.php, line 61

Class

URL
Plugin annotation @Converter( id = "url", description = @Translation("Rewrites calls to url()."), fixme = @Translation("url() expects a route name or an external URI."), dependencies = { "router.route_provider" } )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions

Code

public function rewrite(FunctionCallNode $call, TargetInterface $target) {
  $arguments = $call
    ->getArguments();
  if ($arguments[0] instanceof StringNode) {
    $path = $arguments[0]
      ->toValue();

    // If the URL has a scheme (e.g., http://), it's external.
    if (parse_url($path, PHP_URL_SCHEME)) {
      return ClassMethodCallNode::create('\\Drupal\\Core\\Url', 'fromUri')
        ->appendArgument(clone $arguments[0]);
    }
    elseif ($this
      ->routeExists($path)) {
      $route = $this->routeProvider
        ->getRoutesByPattern('/' . $path)
        ->getIterator()
        ->key();
      return ClassMethodCallNode::create('\\Drupal\\Core\\Url', 'fromRoute')
        ->appendArgument(StringNode::fromValue($route));
    }
  }
}