Score:2

How to set a validation constraint for base fields like the taxonomy term name?

cn flag

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?

Score:4
cn flag

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

Score:2
cn flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.