Weight Vocabulary - sort vocabulary - (not terms)

I need to sort the vocabularies in a page based on admin choice. To be clear, I am not talking about terms, I am talking about the vocabulary, the parent holding the terms.

As per what I read, Vocabulary seems not fieldable unfortunately. If it is, I am not sure how. I found a few db hacks but not sure I wan to implement this if not supported.

Can anyone share any tip to add a weight field to a vocabulary? Or maybe a custom solution/module that would allow me to register the order in which I want the vocabulary to be displayed on?

name: 'Vocabulary Weight'
type: module
description: 'Allows vocabularies to have a weight field for custom sorting.'
core_version_requirement: ^8 || ^9
package: Custom
dependencies:
  - taxonomy

Create a .module file (vocabulary_weight.module) to implement the hook that will add the weight field to vocabularies.

use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Core\Form\FormStateInterface;

function vocabulary_weight_form_vocabulary_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Add a weight field to the vocabulary edit form.
  $form['weight'] = array(
    '#type' => 'number',
    '#title' => t('Weight'),
    '#default_value' => isset($form['vocabulary']->get('weight')->value) ? $form['vocabulary']->get('weight')->value : 0,
    '#description' => t('Assign a weight to determine the order in which vocabularies are displayed.'),
  );

  // Adjust the form submission to save the weight.
  $form['actions']['submit']['#submit'][] = 'vocabulary_weight_save';
}

function vocabulary_weight_save(array &$form, FormStateInterface $form_state) {
  // Get the vocabulary entity.
  $vocabulary = $form_state->getFormObject()->getEntity();
  
  // Save the weight field.
  $vocabulary->set('weight', $form_state->getValue('weight'));
  $vocabulary->save();
}

In any custom query or view that retrieves vocabularies, you can now sort by this weight field. Here’s an example of how you might retrieve vocabularies sorted by weight:

$query = \Drupal::entityQuery('taxonomy_vocabulary')
  ->sort('weight')
  ->execute();

$vocabularies = Vocabulary::loadMultiple($query);

Use an Existing Module (Entity Reference Field + Sorting Module)

Alternatively, if you prefer not to code a custom module, you can work around this by using existing modules:

Entity Reference Field:

You can add a field to some other content type (like a block or page) to reference vocabularies and then use the Fieldable Panels Panes module or other content entity reference systems to display vocabularies in a specific order.

Entityqueue Module:

If you want more flexibility, consider using the Entityqueue module:

Entityqueue** allows you to create ordered lists of any entity type, including vocabularies.

  • The admin can manually order vocabularies within a queue, and you can use this queue to display vocabularies in a specific order on the front end.

You can install the Entityqueue module, create a new queue for vocabularies, and manage their order through the UI.

. Database Hacking Approach (Not Recommended)

Though you mentioned finding some database hacks, it is generally not a good idea to directly modify the database as it could cause issues with future updates or conflicts with other modules. Stick to extending functionality using hooks or modules to keep things Drupal-friendly.