Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Use arguments and parameters in url routes

By editor , 3 December, 2015
Question

How to get arguments form url ?
Use entity (node, user ...) as parameters in route.

Exemple 1 : Form with Node
in routing.yml
my.node.info:
  path: '/user/contactmail/{node}'
  defaults:
    _form: '\Drupal\myx_meet\Form\ContactUserForm'
    _title: 'Contact'
  requirements:
    _permission: 'access content'
  options:
    parameters:
      node:
        type: entity:node

In ContactUserForm

  public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
  // OR AccountInterface for $user
    dpm($node);
//............... Your form
}

To use a 'String' as parameters:
    parameters:
      parametername:
        type: String

Another Example with Options, Defalt values ...

mymodule.content.view:
  path: '/content/{type}/{node}/{id}/{title}/{offset}'
  defaults:
    _controller: '\Drupal\mymodule\Controller\ContentViewer::view'
    _title: 'View'
    offset: '0'
    title: 'untitled'
  requirements:
    _access: 'TRUE'
  options:
    parameters:
      node:
        type: entity:node
      title:
        type: String
      id:
        type: Integer

More details : https://www.drupal.org/node/2310425

Tags

  • Drupal 8
  • Code
  • Add new comment

Comments1

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Pete (not verified)

3 years 11 months ago

Thank you!

I've searched high and low for an example of a non-entity parameter type. Thanks so much for this example. I was trying int, but didn't try Integer.

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer