4

I am working on converting a drupal 7 site to drupal 8 for some practice with 8. In my drupal 7 site, I have a custom hook_menu declaration with the following:

function solution_menu() {
  $items = array();
  $items['solution/%node/%node/%node'] = array(
    'title' => '',
    'page callback' => 'solution_build_solution',
    'page arguments' => array(1,2,3),
    'access callback' => 1,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function solution_build_solution($solution_node, $species_node, $problem_node) {
   //build solution
}

I am trying to accomplish the same thing in drupal 8 with a route. However, if I try something like the following i get an error:

wlh_decision.solution:
  path: '/solution/{node}/{node}/{node}'
  defaults:
    _content: '\Drupal\wlh_decision\Controller\SolutionController::displaySolution'
    _title: 'Solution:'
    _title_callback: '\Drupal\wlh_decision\Controller\SolutionController::solutionTitle'
  requirements:
    _permission: 'access content'

The error i get is:

LogicException: Route pattern "/solution/{node}/{node}/{node}" cannot reference variable name "node" more than once. in Symfony\Component\Routing\RouteCompiler::compilePattern() (line 106 of /Users/brianwilliams/Sites/MAMP/drupal8/core/vendor/symfony/routing/Symfony/Component/Routing/RouteCompiler.php).

but if i change it to use names other than node, I don't get the autoloading.

Anybody know how I am able to handle this?

1 Answer 1

9

You can specify the upcast type for parameters manually:

wlh_decision.solution:
  path: '/solution/{solution_node}/{species_node}/{problem_node}'
  defaults:
    _content: '\Drupal\wlh_decision\Controller\SolutionController::displaySolution'
    _title: 'Solution:'
    _title_callback: '\Drupal\wlh_decision\Controller\SolutionController::solutionTitle'
  requirements:
    _permission: 'access content'
  options:
    parameters:
      solution_node:
        type: entity:node
      species_node:
        type: entity:node
      problem_node:
        type: entity:node

See Using parameters in routes for a slightly more in-depth explanation.

2
  • How about this path: '/{some_string}/a/b <- is this even possible? not as I can see?
    – Raf A.
    Commented Nov 8, 2017 at 18:25
  • 1
    No @RafA, the first part of the path can’t be wild. I believe that’s inherited from Symfony, but it was the same in drupal 7
    – Clive
    Commented Nov 8, 2017 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.