A module can define field type categories in a MODULE_NAME.field_type_categories.yml
file contained in the module's base directory.
Each plugin has the following structure:
CATEGORY_NAME:
label: STRING
description: STRING
weight: INTEGER
libraries:
- STRING
Setting the field type category as a translatable label in annotation is deprecated:
Before
/**
* @FieldType(
* id = "decimal",
* label = @Translation("Number (decimal)"),
* ...
* category = @Translation("Number"),
* )
*/
class DecimalItem extends NumericItemBase { ... }
After
/**
* @FieldType(
* id = "decimal",
* label = @Translation("Number (decimal)"),
* ...
* category = "number",
* )
*/
class DecimalItem extends NumericItemBase { ... }
Where number
is the category ID defined in core.field_type_categories.yml
:
...
number:
label: 'Number'
description: 'Field to store number. I.e. id, price, or quantity.'
weight: -40
libraries:
- module_name/library_name
...
The library allows loading assets within the Field UI when the categories are displayed. This allows the category to provide an icon for itself:
.field-icon-number {
background-image: url("...");
}
BC layer
Your module can add a backwards compatibility layer for Drupal 10.1 and previous versions by adding the following hook implementation to your .module file.
use Drupal\Core\StringTranslation\TranslatableMarkup;
// ...
/**
* Implements hook_field_info_alter().
*
* @todo Remove once minimum version supported is at least 10.2.0.
*/
function MODULE_NAME_field_info_alter(array &$info): void {
// Allow module to work with versions of older versions of Drupal.
if (\version_compare(\Drupal::VERSION, '10.1.9999', '<')) {
// @todo Replace 'decimal' with the field type and "Number" with the previous category string.
$info['decimal']['category'] = new TranslatableMarkup("Number");
}
}
The field type category info can be accessed with the help of the plugin methods getLabel()
, getDescription()
, and getWeight()
, etc. These methods are available on the plugin instances retrieved using the YAML-based plugin discovery system.