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?

  • 1
    Not sure why I've been voted down for asking this question. Clearly I looked at the docs first.
    – Camsoft
    Mar 8, 2011 at 12:40
  • Update, just found this excellent page regarding JavaScript and Drupal 7: drupal.org/node/756722
    – Camsoft
    Mar 8, 2011 at 16:26

2 Answers 2


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');
  • Doesn't answer how I then load that on node/edit pages? I'm looking for best practices here as there are always multiple ways of doing things?
    – Camsoft
    Mar 8, 2011 at 12:38
  • You are right, I read the question quickly and misunderstood its intend. Mar 8, 2011 at 14:04
  • 3
    Note: You can use MYMODULE_form_node_form_alter() in Drupal 7.
    – Berdir
    Mar 8, 2011 at 14:37

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.