This project is not covered by Drupal’s security advisory policy.

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

Project information

Releases