My idea:
I am trying to use a module to manage all the js and css with #attached method.
#attached
method I found from:
- Adding CSS and JS to form with attachments
- https://www.drupal.org/docs/7/api/javascript-api/managing-javascript-in-drupal-7
My module folder containing:
testing.info
testing.module
js/mask.js
In testing.info, it's including file testing.module
files[] = testing.module
In testing.module, I'm trying to use the #attached method to attach "jquery mask" to all form tags, including add/edit node, webform field.
Here is my code but it does not work. -___-
function testing_init() {
$build['#attached']['js']['https://cdn.testing.com/jquery.mask.min.js'] = array('type' => 'external');
$form['#attached']['js'][] = drupal_get_path('module', 'testing') . '/js/mask.js';
}
Question:
Am I using the wrong method to attach js to form tag?
I don't want to load these files on all pages, what is the better way?
$form['#attached']['js'][] = drupal_get_path('module', 'testing') . '/js/mask.js';
is never going to work inside of hook_init. Use hook_form_altertesting_form_alter(&$form, &$form_state, $form_id) {
remember to flush cache after to see changes.