Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). Varbase Media Header 9.2.1 Minor update available for module varbase_media_header (9.2.1). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

This module generates PHP enum or class constant files based on your Drupal site's taxonomy vocabularies, allowing you to easily define named constants for your terms. It provides options for namespace, output format (enum or constants), and ID casting.

Enum Generator provides a means of generating enums or class constants files from your site's entities/entity types. At initial release it only supports taxonomy vocabularies/terms, but the hope is to extend it to entity types in the future. The module provides a form from which you can input/select:

  • The namespace to use for the generated file
  • Whether to generate an enum file or class of constants. This might be helpful depending on your PHP version.
  • Whether to cast the IDs to strings or integers.

Generators are available from /admin/structure/enum-generator. For a vocabulary called "Animals" containing three terms, the generated files would look like:

<?php

namespace Drupal\my_module\Enum;

/**
 * Defines enums for terms in the Animals taxonomy.
 */
enum AnimalsTerm: int
{
	/** Represents the "Cat" term. */
	case CAT = 37;

	/** Represents the "Dog" term. */
	case DOG = 36;

	/** Represents the "FISH" term. */
	case FISH = 35;
}
<?php

namespace Drupal\my_module\ConstEnum;

/**
 * Defines class constants for terms in the Animals taxonomy.
 */
class AnimalsTerm
{
	/** Represents the "Cat" term. */
	public const CAT = 37;

	/** Represents the "Dog" term. */
	public const DOG = 36;

	/** Represents the "Fish" term. */
	public const FISH = 35;
}

The file contents are generated by https://github.com/nette/php-generator, so they do not follow Drupal style guidelines at the moment.

Shameless Self Promotion

A list of modules that I maintain:

  • Cache Register: A module aimed at improving developer quality of life surrounding caching.
  • Enum Generator: A developer utility that generates enums and class constants from a site's entities.
  • Views Hooks Extras: Extends Views' native hooks to more specifically target particular views and their displays
  • Media Entity Remote Image: Provides a simple media type + field for remote image urls

Activity

Total releases
1
First release
Jan 2026
Latest release
5 months ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Active

Releases

Version Type Release date
1.0.1 Stable Jan 26, 2026