I tried to create a variable on hook_preprocess_page to achieve it, but I'm only getting the machine readable name so far.

$variables['node_type'] = FALSE;
if ($current_path = \Drupal::service('path.current')->getPath()) {
  $path_args = explode('/', $current_path);
  $node_type = $path_args[3];
  $variables['node_type'] = $node_type;


For some reason it works on node add form and node edit forms:

if ($current_path = \Drupal::service('path.current')->getPath()) {
  $node_type = \Drupal::entityManager()->getStorage('node_type')->load($node->getType())->get('name');
  $variables['node_type'] = $node_type;

I just need update the condition because it I'm getting the error on /node/add/ page (without content type)

2 Answers 2


I don't think there is a node object in the node.add route, it doesn't exist there yet. But you can still get it from the node_type parameter. Untested, but following the code from NodeController.php you should be able to get the label with something like this:

//get the machine name from route
$machineName = \Drupal::routeMatch()->getParameter('node_type');
//get the label via definition
$nodeTypeLabel = \Drupal::entityTypeManager()->getDefinition('node_type')->load($machineName)->label();

the question is not clear but as I understand you want to pass the node type readable name to the template and this is the easiest way :

function THEMENAME_preprocess_page(&$variables){
 //Get the current page rendered node(s).
  $node = \Drupal::routeMatch()->getParameter('node');

  if ($node instanceof \Drupal\node\NodeInterface) {
    // You can get any information about the
    // rendered node through the $node methods
    //just kint($node) then replace the label() method with another one.
    $variables['node_type'] = $node->label();
  • this code returns node_type bool FALSE (page--node--add.html.twig)
    – user97394
    Commented Jan 23, 2020 at 6:34
  • I HAVE edited it and it work now, just I have forget to put the &variables in function argument. Commented Jan 23, 2020 at 7:13
  • @MarwenAmri this won't work as expected. The OP wants the node bundle label not the node instance label. Check drupal.stackexchange.com/a/187983/57634. I've edited your answer with the proper code.
    – d70rr3s
    Commented Jan 23, 2020 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.