Question

I've defined a library of JavaScript files and CSS files required for my module to function using hook_library().

How do I go about loading this library on node/edit pages?

Was it helpful?

Solution

I would call drupal_add_library() in a hook_form_alter() implementation, like this:

function MODULE_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node_edit_form']) && $form['#node_edit_form') {
    drupal_add_library('MODULE', 'LIBRARY');
  }
}

This should ensure the library is loaded whenever a node form is displayed.

And using hints from Berdir to use hook_form_BASE_FORM_ID_alter and from kiamlaluno to use #attached:

function MYMODULE_form_node_form_alter(&$form, &$form_state) {
  $form['#attached']['library'][] = array('MODULE', 'LIBRARY');
}

OTHER TIPS

If you are modifying, or outputting a form, you can use the #attached attribute, which is described as "allows loading of CSS, Javascript, libraries, or custom types when the form is built".

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top