2

I'm trying to add JS to content create pages using the second function from @kiamlaluno's answer

However, this is not working for me.

I created a module with his folder structure:

add_js add_js.info add_js.module custom.js

add_js.info:

name = Add JS to admin pages
description = Add js to admin pages
core = 7.x

add_js.module:

<?php

    function add_js_init() {
      if (arg(0) == 'admin') {
        $path = drupal_get_path('module', 'add_js');
        drupal_add_js($path . '/custom.js');
      }
    }

custom.js:

jQuery(document).ready(function ($) {
    $("html").addClass("hello");
});

Also tried this .module file:

<?php

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'my_form':
      $form['#after_build'][] = 'mymodule_after_build';
      break;
  }
}

function mymodule_after_build($form, &$form_state) {
  $path = drupal_get_path('module', 'mymodule');
  drupal_add_js ("$path/mymodule.js");
  return $form;
}

Although I am pretty inexperienced still, I did remember to enable the module and flush the caches... :D

1 Answer 1

4
/**
 *  Implements hook_form_BASE_FORM_ID_alter.
 */
function mymodule_form_node_form_alter(&$form, &$form_state, $form_id) {
  $form['#attached']['js'][] = drupal_get_path('module', 'mymodule') . '/js/custom.js';
}

In your case would be:

/**
 *  Implements hook_form_BASE_FORM_ID_alter.
 */
function add_js_form_node_form_alter(&$form, &$form_state, $form_id) {
  $form['#attached']['js'][] = drupal_get_path('module', 'add_js') . '/js/custom.js';
}

Note: This will add the JS to all of your content types node edit pages.


If you need to target a specific content type, all you gotta do is add an if statement, see:

How can I find the form ID of a form?

/**
*  Implements hook_form_BASE_FORM_ID_alter.
*/
  function add_js_form_node_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'my_form_id') {
      $form['#attached']['js'][] = drupal_get_path('module', 'add_js') . '/js/custom.js';
    }
  }

and I believe you could alternatively do this, put the my_form_id directly in the hook:

/**
*  Implements hook_form_BASE_FORM_ID_alter.
*/
  function add_js_form_my_form_id_alter(&$form, &$form_state, $form_id) {

      $form['#attached']['js'][] = drupal_get_path('module', 'add_js') . '/js/custom.js';

  }
4
  • 1
    Perfect! Note: for the my_form_id will originally be in my-form-id format. All you got to do is change the hyphens into underscores. Oct 25, 2016 at 1:15
  • What is the difference between mymodule_form_node_form_alter and mymodule_form_alter? Dec 21, 2017 at 1:58
  • 1
    @KimberlyW thanks for asking, I have fixed my implement comment. I actually used hook_form_BASE_FORM_ID_alter. So node_form being the base. Not to confuse with hook_form_FORM_ID_alter or hook_form_alter
    – No Sssweat
    Dec 21, 2017 at 10:28
  • @KimberlyW all of them basically do the same, the only diff is some of these pass the ID directly in the hook; thus, no need to write an if statement.
    – No Sssweat
    Dec 21, 2017 at 10:33

Your Answer

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

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