Keywords not alphabetized when adding a Reference

Created on 19 October 2022, over 2 years ago
Updated 6 December 2024, 4 months ago

Problem/Motivation

On the Add Reference (of any type) the keywords are not alphabetized when on Manage Form Display they are displayed as a Check boxes/radio buttons or a Select List.

Would love to know how I can list this in alphabetical order. Any help greatly appreciated.

💬 Support request
Status

Active

Version

2.0

Component

Code

Created by

🇨🇦Canada avo webworks Ottawa, Ontario, Canada

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇦Canada avo webworks Ottawa, Ontario, Canada

    So I just wanted to post a fix for this so that anyone else struggling with this.
    Make sure to target the correct form ID to the forms you want to set.
    In a custom module add this code in the mymodule.module file:

    <?php
    use Drupal\Core\Form\FormStateInterface;
    
    /**
     * Implements hook_form_alter().
     */
    function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
        // List of relevant form IDs for Bibcite.
        $target_form_ids = [
            'bibcite_reference_book_form',
            'bibcite_reference_book_chapter_form',
            'bibcite_reference_journal_article_form',
            'bibcite_reference_book_edit_form',
            'bibcite_reference_book_chapter_edit_form',
            'bibcite_reference_journal_article_edit_form',
        ];
    
        // Check if the current form is one of the target forms.
        if (in_array($form_id, $target_form_ids)) {
            // Check if the Keywords field exists in the form.
            if (isset($form['keywords']['widget']['#options']) && is_array($form['keywords']['widget']['#options'])) {
                // Sort the options alphabetically.
                asort($form['keywords']['widget']['#options']);
            }
        }
    }
Production build 0.71.5 2024