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
| Feature | node_type_get_names() | entity_type.bundle.info Service |
|---|---|---|
| Scope | Only node types | Any entity type |
| Architecture | Procedural | Object-oriented, service-based |
| Future compatibility | Deprecated in 11.3, removed in 13 | Fully supported |
| Testability | Harder to mock | Easy 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.
Comments