You are here

public function Links::convert in Drupal 7 to 8/9 Module Upgrader 8

Performs required conversions.

Parameters

TargetInterface $target: The target module to convert.

Overrides ConverterInterface::convert

File

src/Plugin/DMU/Converter/Links.php, line 67

Class

Links
Plugin annotation @Converter( id = "links", description = @Translation("Converts Drupal 7's hook_menu() links to plugin definitions."), hook = "hook_menu", fixme = @Translation("@FIXME This implementation of hook_menu() cannot be automatically…

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Converter

Code

public function convert(TargetInterface $target) {

  // If the hook implementation contains logic, we cannot convert it and
  // that's that. So we'll leave a FIXME and bail out.

  /** @var \Pharborist\Functions\FunctionDeclarationNode $hook */
  $hook = $target
    ->getIndexer('function')
    ->get('hook_menu');
  if ($hook
    ->is(new ContainsLogicFilter())) {
    $hook
      ->setDocComment(DocCommentNode::create($this->pluginDefinition['fixme']));
    $target
      ->save($hook);
    return;
  }

  // Links are split out by group because there are separate config files
  // for each link type.
  $links = [
    'menu' => new LinkIndex(),
    'task' => new LinkIndex(),
    'action' => new LinkIndex(),
    'contextual' => new LinkIndex(),
  ];
  $hook_menu = new HookMenu($target, $this->routeConverters);
  foreach ($hook_menu
    ->getSourceRoutes()
    ->getAllLinks() as $path => $source) {

    /** @var \Drupal\drupalmoduleupgrader\Routing\LinkBinding\LinkBinding $binding */
    $binding = $this->linkBinding
      ->create($source, $hook_menu
      ->getDestinationRoute($path));

    // Skip if the converter wasn't able to find a destination.
    $destination = $binding
      ->getDestination();
    if (empty($destination)) {
      continue;
    }
    if ($binding instanceof MenuLinkBinding) {
      $links['menu']
        ->addBinding($binding);
    }
    elseif ($binding instanceof LocalTaskLinkBinding) {
      $links['task']
        ->addBinding($binding);
    }
    elseif ($binding instanceof LocalActionLinkBinding) {
      $links['action']
        ->addBinding($binding);
    }
    elseif ($source
      ->isContextualLink()) {
      $links['contextual']
        ->addBinding($binding);
    }
  }
  $links = array_map(function (LinkIndex $index) {
    return $index
      ->build();
  }, $links);
  foreach ($links['contextual'] as $link) {
    $link['group'] = $target
      ->id();
  }
  foreach ($links as $group => $data) {
    if ($data) {
      $this
        ->writeInfo($target, 'links.' . $group, $data);
    }
  }
}