2

I start banging my head on the table, the wall or anything else... please help me.

Here is my my_module.routing.yml:

my_module.api.validate_coupon:
  path: '/api/v1/{coupon_code}/validate'
  defaults:
    _controller: '\Drupal\my_module\API\ValidateCouponController::validate'
  requirements:
    _permission: 'access content'
  options:
    parameters:
      coupon_code:
        type: coupon_code_converter

This is my services.yml

services:
  paramconverter.coupon_code_converter:
    class: Drupal\my_module\Routing\CouponCodeConverter
    tags:
      - { name: paramconverter }

This is my converter class - it is taken from Drupal\language\LanguageConverter and I added some breakpoints which should make the script stop in my IDE:

namespace Drupal\my_module\Routing;
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Symfony\Component\Routing\Route;

/**
 * Converts a coupon code to a fully loaded coupon
 */
class CouponCodeConverter implements ParamConverterInterface {

  /**
   * {@inheritdoc}
   */
  public function convert($value, $definition, $name, array $defaults) {
    if (!empty($value)) {
      return $this->languageManager->getLanguage($value);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function applies($definition, $name, Route $route) {
    return (!empty($definition['type']) && $definition['type'] == 'language');
  }
}

And this is my controller class:

namespace Drupal\my_module\API;

use Drupal\Core\Controller\ControllerBase;

class ValidateCouponController extends ControllerBase  {

  public function validate($coupon_code) {
    $container = \Drupal::getContainer();
    $kernel = $container->get('kernel');
    $services = $kernel->getCachedContainerDefinition()['services'];
    ksort($services);
    return array(
      '#markup' => 'hallo'
    );
  }
}

I can enter http://mypath.com/api/v1/1/validate and the controller gets the value '1' for $coupon_code. But I want my CouponCodeConverter class to do some magic before the parameter gets sent to the Controller... but the converter gets not called... what do I wrong?

1

I think the condition in applies() doesn't match with the route parameters, try this:

  public function applies($definition, $name, Route $route) {
    return (!empty($definition['type']) && $definition['type'] == 'coupon_code_converter');
  }
  • Thanks a lot... I placed a Breakpoint in the applies() method but the script does not stop there. Thank you – Tobias Krause May 31 '18 at 9:17
  • When do you expect this to happen? This is part of the route building process and usually doesn't run in a normal request. – 4k4 May 31 '18 at 9:35
  • Ah ok.. I though like the AccessController on every request this applies() function would be called. Thank you for that clarification. – Tobias Krause Jun 4 '18 at 6:32

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.

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