0

I'm learning Drupal8 (for the first time, no Drupal7 or previous knowledge) on a local Vagrant VM and have come up against this little problem. I'm sorry if it's kind of a noob question but I wasn't able to solve it and also couldn't find any specific hepful info out there.

I can access my route ("http://172.28.128.10/drupal-project/web/policeman/7") and no error is shown, but this is what my navigator (Chrome) displays:

plateNumber = $plate_number; } public function getPlateNumber() { return $this->plateNumber; } }

This is kinda weird in the sense that it seems like Drupal is having trouble with its internal calls but I don't quite understand why it's returning that and displaying it instead of throwing some kind of exception.

I know the routing works with other controllers I've created that don't use custom ParamConverters like "http://172.28.128.10/drupal-project/web/hello/3" which returns just fine the name of the user with said ID. Don't know if knowing other routes work is relevant for you.

ow_hello.routing.yml:

ow_hello.policeman:
  path: '/policeman/{policeman}'
  defaults:
    _controller: \Drupal\ow_hello\Controller\PolicemanController::view
  requirements:
    _access: 'TRUE'
  options:
    parameters:
      policeman:
        type: policeman

ow_hello.services.yml:

services:
  ow_hello.policeman_param_converter:
    class: Drupal\ow_hello\Routing\PolicemanParamConverter
    tags:
    - { name: paramconverter }

Policeman.php:

<?

namespace Drupal\ow_hello;

class Policeman {

  private $plateNumber;

  public function __construct($plate_number) {
    $this->plateNumber = $plate_number;
  }

  public function getPlateNumber() {
    return $this->plateNumber;
  }

}

PolicemanParamConverter.php:

<?php

namespace Drupal\ow_hello\Routing;

use Drupal\Core\ParamConverter\ParamConverterInterface;
use Drupal\ow_hello\Policeman;
use Symfony\Component\Routing\Route;

class PolicemanParamConverter implements ParamConverterInterface {

  public function applies($definition, $name, Route $route) {
    return !empty($definition['type']) && $definition['type'] == 'policeman';
  }

  public function convert($value, $definition, $name, array $defaults) {
    return new Policeman($value);
  }

}

PolicemanController.php:

<?php

namespace Drupal\ow_hello\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\ow_hello\Policeman;

class PolicemanController extends ControllerBase {

  public function view(Policeman $policeman) {
    return [
      '#type' => 'markup',
      '#markup' => $this->t('Hello my plate number is: @plate_number', ['@plate_number' => $policeman->getPlateNumber()]),
    ];
  }

}
  • If it's not a copy and paste error, Policeman.php is missing <?php in the first line. – 4k4 Jun 14 '18 at 10:13
  • Wow I feel so dumb. Thank you very much for pointing that out! I blame phpStorm, after all it should do all my work for me (????) xD Indeed now it works just fine, thanks!! – Naggash Jun 14 '18 at 10:38
  • What should I do about the question in a case like this? Can I somehow close it? Do I respond to myself with the solution and mark it as solved? Do I leave it as is? – Naggash Jun 14 '18 at 10:39

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.