Allow to add custom library in Multichoice form

Created on 27 March 2023, over 1 year ago

Problem/Motivation

I need to add a custom javascript to the Quiz Question Multichoice form through a custom module, but adding my library to $form['#attached']['library'] won't work.

Steps to reproduce

Create a custom module and in your .module, add a function that implements the hook_form_FORM_ID_alter for quiz_question_multichoice form.

function mymodule_form_quiz_question_multichoice_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#attached']['library'][] = 'mymodule/multichoice';
}

In the .libraries.yml add the "multichoice" section with the reference to a JS (and/or CSS):

multichoice:
  js:
    js/multichoice.js: {}
  dependencies:
    - core/drupalSettings
    - core/jQuery

Activate the module, edit an existing question or add a new one.

The library won't load.

Note

Maybe this won't apply to many, because of the module priority. This happens only if the custom module runs before than quiz_multichoice.

Proposed resolution

In quiz_multichoice.module, function quiz_multichoice_form_quiz_question_multichoice_form_alter, the 'library' array item is set overwriting any previous value:

  $form['#attached']['library'] = ['quiz_multichoice/helper'];

Adding the library to existent ones should solve:

  $form['#attached']['library'][] = 'quiz_multichoice/helper';

Attaching patch.

🐛 Bug report
Status

Fixed

Version

6.0

Component

Code - Multichoice

Created by

🇮🇹Italy nessunluogo Tremestieri Etneo (CT)

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

Comments & Activities

Production build 0.69.0 2024