2

I have a route with optional parameter that has some default value:

# file @hello.routing.yml
hello.sayHi:
  path: '/hello/{name}'
  defaults: 
    _title: 'Say Hi'
    _content: 'Drupal\hello\Controller\SimpleCallback::sayHiAction'
    name: 'there'
  requirements:
    _permission: 'access content'

I'd like to make there value for {name} parameter configurable. Let's assume that this default value for {name} is available in simple config variable.

  • Have you tried adding a default value to the controller method (sayHiAction)? – Eyal Jan 19 '16 at 15:01
  • Remove the name form route and use it in controller. why are you trying to complicate things for yourself? :D – user21641 Jan 19 '16 at 17:02
  • @IvanJaros because I want to go by the book, be consistent and maybe use parameter upcasting? – SiliconMind Jan 19 '16 at 18:52
  • Either you haven't explained what you want correctly, or I didn't understood what you want or you don't understand what "I want to go by the book" means :D .. as I'm reading the answer from cilefen I think you might want to have a look at \Drupal\Core\Routing\RouteSubscriberBase – user21641 Jan 19 '16 at 19:56
  • OK, so symfony allows you to use config parameter values in yaml file by referencing them with a % sign. For example: stackoverflow.com/a/18782406 I'm looking for similar behavior that will work for Drupal's simple config. BTW it would be awesome if we could use simple config variables in yaml files like we can with symfonys native config parameters. "By the book" I mean the best practice approach, like with symfonys service: you don't use default value in a service constructor but instead you inject the value in your service definition. – SiliconMind Jan 19 '16 at 21:17
1

You must implement a route alter. In the alter code, extract the setting and set the value on the route definition.

  • This looks promissing. I'll give it a shoot. I'd be happy to accept your answer, but can you update it with code example just for sake of stackexchange rules? – SiliconMind Jan 19 '16 at 18:57
  • No. SE has their rules and I have mine. It is well-documented at the link. – cilefen Jan 19 '16 at 21:56
0

Implement two routes one with an argument and the second without.

hello.routing.yml

hello.sayHi:
  path: '/hello/{name}'
  defaults: 
    _title: 'Say Hi'
    _content: 'Drupal\hello\Controller\SimpleCallback::sayHiAction'
  requirements:
    _permission: 'access content'
hello.sayHi.default:
  path: '/hello'
  defaults: 
    _title: 'Say Hi'
    _content: 'Drupal\hello\Controller\SimpleCallback::sayHiAction'
  requirements:
    _permission: 'access content'

hello.config.yml

default_name: there

hello.services.yml

hello.config:
  factory: ['@config.factory', get]
  class: Drupal\Core\Config\Config
  arguments: ['hello.config']

Drupal\hello\Controller\SimpleCallback

protected $configuration;

public function __constructor(Config $configuration) {
  $this->configuration = $configuration;
}

public static function create(Container $container) {
  return new static($container->get('hello.config'));
}

public function sayHiAction($name = NULL) {
  $name = is_null($name) ? $this->configuration->get('default_name') : $name;
  return [
    '#type' => 'inline_template',
    '#template' => 'Hi {name}',
    '#context' => [
      'name' => $name
    ]
  ]
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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