Skip to main content
Home
Drupal life hacks

Main navigation

  • Drupal
  • React
  • WP
  • Contact
  • About
User account menu
  • Log in

Breadcrumb

  1. Home

Migrating from node_type_get_names() in Drupal 11: Full Guide + Working Module Example

By admin, 20 September, 2025

As Drupal continues its evolution toward a cleaner, service-oriented architecture, developers must adapt to API changes that improve flexibility and maintainability. One such change in Drupal 11.3.0 is the deprecation of node_type_get_names(), a function long used to retrieve node type labels.

This article walks you through:

  • Why the function was deprecated
  • How to replace it with the new service
  • A working example of a custom module using the modern approach

❌ What Was Deprecated?

Previously, developers used this simple function to get all node type labels:

$type_names = node_type_get_names();

While convenient, it was limited to nodes and didn’t align with Drupal’s modern service-based architecture.


✅ The New Recommended Approach

Drupal now recommends using the entity_type.bundle.info service:

$type_names = \Drupal::service('entity_type.bundle.info')->getBundleLabels('node');

This method:

  • Works with any entity type, not just nodes
  • Is object-oriented and testable
  • Aligns with Drupal’s dependency injection system

🔧 Working Example: Custom Module

Let’s build a simple module called node_type_labels that displays all node type labels on a custom admin page.

📁 File Structure

node_type_labels/
├── node_type_labels.info.yml
├── node_type_labels.routing.yml
├── src/Controller/NodeTypeLabelsController.php

📄 node_type_labels.info.yml

name: 'Node Type Labels'
type: module
description: 'Displays node type labels using the new bundle info service.'
core_version_requirement: ^11
package: Custom

📄 node_type_labels.routing.yml

node_type_labels.list:
  path: '/admin/node-type-labels'
  defaults:
    _controller: '\Drupal\node_type_labels\Controller\NodeTypeLabelsController::list'
    _title: 'Node Type Labels'
  requirements:
    _permission: 'access content'

📄 src/Controller/NodeTypeLabelsController.php

namespace Drupal\node_type_labels\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;

class NodeTypeLabelsController extends ControllerBase {

  protected $bundleInfo;

  public function __construct(EntityTypeBundleInfoInterface $bundleInfo) {
    $this->bundleInfo = $bundleInfo;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.bundle.info')
    );
  }

  public function list() {
    $labels = $this->bundleInfo->getBundleLabels('node');

    $output = [
      '#type' => 'item',
      '#markup' => '<strong>Node Type Labels:</strong><br>' . implode('<br>', $labels),
    ];

    return $output;
  }
}

🧪 Result

Once enabled, this module adds a page at /admin/node-type-labels that lists all node type labels using the new service. No deprecated functions, no legacy code — just clean, modern Drupal.


📊 Quick Comparison

Featurenode_type_get_names()entity_type.bundle.info Service
ScopeOnly node typesAny entity type
ArchitectureProceduralObject-oriented, service-based
Future compatibilityDeprecated in 11.3, removed in 13Fully supported
TestabilityHarder to mockEasy to inject and test

💬 Final Thoughts

Deprecations aren’t just obstacles — they’re opportunities to modernize your codebase and embrace best practices. Migrating to entity_type.bundle.info not only future-proofs your modules but also makes them more flexible and maintainable.

Tags

  • #Drupal Planet
  • EntityTypeBundleInfo

Comments

About text formats

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Powered by Drupal