I'm developing a solution which allows users to create content through a custom AJAX-driven UI. The first step in the process requires users to pick a content type from a dropdown list, which will then trigger the relevant node form to be loaded in to the page via AJAX. The HTML for the node form is being built correctly, but the issue I'm experiencing is that required JavaScript libraries are not included in the page, e.g. system libraries such as drupal.ajax and jquery.form, and libraries provided by contributed modules such as CKEditor and the media browser provided by the Media module.

Is there an elegant way of determining what libraries are required by node forms for a range of content types before the form is loaded? My intention would be to include all of the libraries using drupal_add_library, drupal_add_js and drupal_add_css in the page callback function for the UI.

Thanks in advance to anyone who has any ideas!

1 Answer 1


The most elegant way to do that is define attachments in hook_form_FORM_ID_alter. You can specify css files, js files or libraries:

$form['#attached']['library'][] = array('system', 'jquery.cookie');
$form['#attached']['js'][] = drupal_get_path('module', 'ajax_example') . '/ajax_example.js';
$form['#attached']['css'][] = drupal_get_path('module', 'ajax_example') . '/ajax_example.css';
  • Thanks for your reply, but unfortunately it doesn't answer my question. I don't need to add additional attachments to the form, I'd like to know if there's a way of finding out what libraries etc. are already attached based on all of the fields that make up the node form for a specific content type.
    – user39539
    Jan 26, 2015 at 10:55

Your Answer

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