2

I'm having trouble setting a validation constraint on the name of the taxonomy term.

function MYMODULE_entity_bundle_field_info_alter(&$fields, $entity_type, $bundle) {
  if ($entity_type->id() === 'taxonomy_term' && $bundle === 'my_taxonomy_type') {
    kint($fields['name']);
    if (isset($fields['name'])) {
      kint('found name');
    }
  }
}

When I rebuild the cache with drush, kint() outputs the value of $fields['name'] as null.

When I use kint() to output the value of $fields, I see this:

array (4) [
    'parent' => Drupal\Core\Field\BaseFieldDefinition (7) (
        protected 'type' -> string (16) "entity_reference"
        protected 'propertyDefinitions' -> null
        protected 'schema' -> null
        protected 'indexes' -> array (0) []
        protected 'itemDefinition' -> Drupal\Core\Field\TypedData\FieldItemDataDefinition (3) (
            protected 'fieldDefinition' -> Drupal\Core\Field\BaseFieldDefinition (7) RECURSION
            protected 'definition' -> array (2) [
                'type' => string (27) "field_item:entity_reference"
                'settings' => array (3) [
                    'target_type' => string (13) "taxonomy_term"
                    'handler' => string (7) "default"
                    'handler_settings' => array (1) [
                        'target_bundles' => array (1) DEPTH LIMIT
                    ]
                ]
            ]
            protected 'typedDataManager' -> null
        )
        protected 'definition' -> array (7) [
            'label' => Drupal\Core\StringTranslation\TranslatableMarkup (5) (
                protected 'translatedMarkup' -> null
                protected 'options' -> array (0) []
                protected 'stringTranslation' -> null
                protected 'string' -> string (12) "Term Parents"
                protected 'arguments' -> array (0) []
            )
            'description' => Drupal\Core\StringTranslation\TranslatableMarkup (5) (
                protected 'translatedMarkup' -> null
                protected 'options' -> array (0) []
                protected 'stringTranslation' -> null
                protected 'string' -> string (25) "The parents of this term."
                protected 'arguments' -> array (0) []
            )
            'cardinality' => integer -1
            'provider' => string (8) "taxonomy"
            'field_name' => string (6) "parent"
            'entity_type' => string (13) "taxonomy_term"
            'bundle' => string (18) "my_taxonomy_type"
        ]
        protected 'typedDataManager' -> null
    )
    'field_int_count' => Drupal\field\Entity\FieldConfig (35) (
        protected 'deleted' -> boolean false
        ... cut
        protected 'id' -> string (56) "taxonomy_term.card_group_private.field_term_date_created"
        protected 'field_name' -> string (23) "field_term_date_created"
        protected 'field_type' -> string (8) "datetime"
        ... cut

"taxonomy_term.card_group_private.field_term_ref_author" protected 'field_name' -> string (21) "field_term_ref_author" protected 'field_type' -> string (16) "entity_reference" ]

The name field is nowhere to be found. How can I add a constraint to taxonomy term names?

2 Answers 2

4

Try a different hook for base fields:

/**
 * Implements hook_entity_base_field_info_alter().
 */
function mymodule_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'taxonomy_term') {
    $fields['foo']->addConstraint('Length', ['min' => 5]);
  }
}

If the field has bundle specific overrides you might need to use both hooks. See https://www.drupal.org/project/drupal/issues/3193351

Example for both hooks found in a contrib module https://git.drupalcode.org/project/node_title_validation/-/blob/8.x-1.x/node_title_validation.module

2

Field name in taxonomy is a baseField not a bundleField so you should rather use HOOK_entity_base_field_info_alter instead to add custom definition to it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.