Drupal 8/9: Make taxonomy description field mandatory

dub 10 2022

Recently, we've received a request from one of our clients to convert the built-in Description field from one of their taxonomies into a mandatory field. Sounds easy, right?

Since the taxonomy was created via the Drupal UI, that's where we've aimed to visit and configure it. However, because the description is a field that is automatically added by the taxonomy core module, the management UI for this field is missing.

The obvious solution is: hide & disable the Description field and create a new custom field that will be labelled Description. A discussion on this problem already exists at drupal.org and you can find it here. But what about when you're already managing thousands of taxonomy terms? What if some of these terms already have the field filled with data while some still have it empty?

The discussion referred in the link above has barely shown any activity. That made it clear to me that any official solution was likely miles away. It was necessary to get my hands dirty.

How to implement it using database

It didn't take long for me to realize, that the only thing lacking support is the Drupal UI. Drupal itself supports overrides for any field type. The most simple method to reach my goal was to simply edit the Drupal database.

Drupal 8 / 9 stores the core field overrides together with custom field configuration in database table named config. The name column corresponding to Description override is core.base_field_override.taxonomy_term.<vocabulary identifier>.description.

The data column stores the entire configuration in the output of the PHP serialize() function. However I had no idea what was the proper structure of this data array that had to be serialized.

I found that the fastest method to get the required data formating was to temporarily enable Drupal plugin Base Field Override UI (composer package drupal/base_field_override_ui). This plugin can quickly generate the override database entry and can be safely disabled afterwards.

I have no idea why doesn't this plugin implement the option to turn the Required property on/off. However, at this point I already had the required data structure inside the database, so it was simply a matter of changing the value of required from 0 to 1 inside the serialized data. 

Don't forget to purge all Drupal caches.