3 namespace Drupal\{{ machine_name }};
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\ParamConverter\ParamConverterInterface;
7 use Symfony\Component\Routing\Route;
10 * Converts parameters for upcasting database record IDs to full std objects.
13 * To use this converter specify parameter type in a relevant route as follows:
15 * {{ machine_name }}.{{ parameter_type }}_parameter_converter:
16 * path: example/{record}
18 * _controller: '\Drupal\{{ machine_name }}\Controller\{{ controller_class }}::build'
24 * type: {{ parameter_type }}
27 * Note that for entities you can make use of existing parameter converter
28 * provided by Drupal core.
29 * @see \Drupal\Core\ParamConverter\EntityConverter
31 class {{ class }} implements ParamConverterInterface {
34 * The database connection.
36 * @var \Drupal\Core\Database\Connection
38 protected $connection;
41 * Constructs a new {{ class }}.
43 * @param \Drupal\Core\Database\Connection $connection
44 * The default database connection.
46 public function __construct(Connection $connection) {
47 $this->connection = $connection;
53 public function convert($value, $definition, $name, array $defaults) {
54 // Return NULL if record not found to trigger 404 HTTP error.
55 return $this->connection->query('SELECT * FROM {table_name} WHERE id = ?', [$value])->fetch() ?: NULL;
61 public function applies($definition, $name, Route $route) {
62 return !empty($definition['type']) && $definition['type'] == '{{ parameter_type }}';