10

I have a local links.task.yml file to display the tab for the node data , But it seems to have a problem passing argument to the path . As you can see I am extending the node's basic fetaures like edit,delete.revision with an additional function using tab. I have module_name.links.task.yml

entity.node.check_backend:
  route_name: module_name.my_route
  base_route: entity.node.canonical
  title: My title

And I have the route in module_name.routing.yml as follows:

module_name.my_route:
  path: '/node/{arg1}/function_name'
  defaults:
    _controller: '\Drupal\module_name\Controller\ControllerName::function_name'
    _title: 'My title'
  requirements:
    _permission: 'access admin tab'

But it throws me errors like:

Symfony\Component\Routing\Exception\MissingMandatoryParametersException: Some mandatory parameters are missing ("arg1") to generate a URL for route "module_name.my_route".

How can I pass variables in links.task.yml file ?

4
  • 1
    I already attempted to answered this to you in Convert hook_menu in drupal 8. Also have a look at the linked resource (halfway in the asnwer): Parameters in Routes and the sub-pages. Commented May 12, 2016 at 15:14
  • I think you mistaken the question , its to use parameters in links.yml not in routing.yml
    – KTM
    Commented May 12, 2016 at 15:27
  • Links are only references to routes. The error comes from your routing. Commented May 12, 2016 at 15:34
  • 1
    I guess, because you are working with nodes, your variable should be called {node} so the existing node routing can work with it. Can you try that in your routing file? Commented May 12, 2016 at 15:41

4 Answers 4

1

The problem is that you are using {arg1} in your route and your links.task.yml file is registering that as an item that should use base route entity.node.canonical. As you can read in this change record, the value for entity.node.canonical will become node/[nid].

In your situation, you are providing a variable arg1, when the node routing system is expecting a node variable.

Just make sure that all sub-pages share the same base path as their parents.

19

In your module_name.links.task.yml file you can add a route_parameters: array that lists the parameters you want to pass, in your case it would end up:

entity.node.check_backend:
  route_name: module_name.my_route
  base_route: entity.node.canonical
  title: My title
  route_parameters:
    arg1:  'value_for_arg1'

You can find this by looking at the code in \Drupal\Core\Menu\LocalTaskManager which parses this file.

1
  • 2
    How would you get the node id to pass it as argument?
    – Elin Y.
    Commented Sep 22, 2020 at 15:01
5

You can use a class to generate dynamic tabs, and take example on the core tracker module which supports this.

Let's take a look in the tracker.links.task.yml file :

tracker.users_recent_tab:
route_name: tracker.users_recent_content
title: 'My recent content'
base_route: tracker.page
class: '\Drupal\tracker\Plugin\Menu\UserTrackerTab'

And then the \Drupal\tracker\Plugin\Menu\UserTrackerTab class :

<?php

namespace Drupal\tracker\Plugin\Menu;

use Drupal\Core\Menu\LocalTaskDefault;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Provides route parameters needed to link to the current user tracker tab.
 */
class UserTrackerTab extends LocalTaskDefault {

  /**
   * Current user object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Gets the current active user.
   *
   * @todo: https://www.drupal.org/node/2105123 put this method in
   *   \Drupal\Core\Plugin\PluginBase instead.
   *
   * @return \Drupal\Core\Session\AccountInterface
   */
  protected function currentUser() {
    if (!$this->currentUser) {
      $this->currentUser = \Drupal::currentUser();
    }
    return $this->currentUser;
  }


  /**
   * {@inheritdoc}
   */
  public function getRouteParameters(RouteMatchInterface $route_match) {
    return ['user' => $this->currentUser()->Id()];
  }

}

So you should create your own class like this one and implement the getRouteParameters method to return the array with the right parameters to be used. Simple like that :)

Reference to a well written article about this : https://medium.com/@joshirohit100/dynamic-tabs-in-drupal-8-8a76386e212c

0

You have to use "route_parameters" key in module_name.links.task.yml and pass the variables with value. Here is the working code : In module_name.routing.yml file

sg_custom.loading_form:
  path: '/form/loading_form/{page}'
  defaults: 
    _form: '\Drupal\sg_custom\Form\LoadingForm'
    _title: 'Loading Form'
  requirements:
    _permission: 'access content'

And in module_name.links.task.yml file

sg_custom.tab_1:
  route_name: sg_custom.loading_form
  title: Loading Start
  base_route: sg_custom.loading_form
  route_parameters: 
    page: start_loading

sg_custom.tab_2:
  route_name: sg_custom.loading_form
  title: Loading End
  base_route: sg_custom.loading_form
  route_parameters: 
    page: end_loading

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.