I am still new to all the Symfony semantics. What would be the easiest way to change node canonical urls to be /article/[node-title]
instead of /node/[node-id]
?
Following this documentation I tried the code (which I expected to produce at least a url structure like /article/[node-id]
)
<?php
/**
* @file
* Contains \Drupal\example\Routing\ExampleRoutes.
*/
namespace Drupal\example\Routing;
use Symfony\Component\Routing\Route;
/**
* Defines dynamic routes.
*/
class ExampleRoutes {
/**
* {@inheritdoc}
*/
public function routes() {
$routes = array();
// Declares a single route under the name 'example.content'.
// Returns an array of Route objects.
$routes['entity.node.canonical'] = new Route(
// Path to attach this route to:
'/article/{node}',
// Route defaults:
array(
'_controller' => '\Drupal\node\Controller\NodeViewController::view',
'_title' => 'Hello'
),
// Route requirements:
array(
'_permission' => 'access content',
)
);
return $routes;
}
}
?>
But unfortunately it is not changing anything…
hook_entity_type_alter()
, and useEntityTypeInterface::setLinkTemplate
to override the entity type's canonical URL template. You may also need a custom parameter upcaster to get the node resolved from its title rather than the ID