1

I'd like to execute some jQuery code on a particular element in the node add/edit page of a particular content type. Currently I do that by putting

$(document).ready(function() {
    if (window.location.href.indexOf("node/add/CONTENT_TYPE") > -1) {
        ACTION;
    }
});

in the theme's .js file. It works, but I think the approach is probably not appropriate, as the test unnecessarily runs everywhere.

Could you please suggest an appropriate way?

1

First, you need to convert your script to be a behavior. Drupal will call defined behaviors, so document ready is not necessary.

Second, you can add this script to specific form pages with drupal_add_js and hook_form_alter.

  1. Use hook_form_alter to hook into node add and node edit forms. You can do a check on the $form_id argument to ensure it is a node form.
  2. Use drupal_add_js to add your script file.

Example:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (preg_match('/node_form/i', $form_id) {
    drupal_add_js(drupal_get_path('module', 'my_module') . '/script.js');
  }
}

This file will only load when these forms are built, so, you can also remove the line that is checking the window href in the javascript.

The code above is untested, but a general start in the right direction. This code can be placed in a custom module, or, if your admin uses the same theme, you can place it in the template.php of the theme. I strongly recommend a simple custom module.

Edit

I believe the form id is preprended by the content type in 6. So, to isolate it to one content type:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'content_type_node_form') {
    drupal_add_js(drupal_get_path('module', 'my_module') . '/script.js');
  }
}

Or you can use hook_form_FORM_ID_alter to target specific forms too:

function mymodule_form_content_type_form_alter(&$form, $form_state, $form_id) {
  drupal_add_js(drupal_get_path('module', 'my_module') . '/script.js');
}

Where content_type is the name of any given content type (article, page, etc).

Example behavior (mymodule/script.js):

(function ($) {
  // you can define multiple behaviors

  Drupal.behaviors.mymodule_behaviorname_one = function(context) {
    // this code should fire automatically when the page has loaded
    console.log('Hello!');
    alert('Hello!');

    // attaching to an element
    $("a", context).click(function(e) {
      e.preventDefault();
      alert('You clicked a link.');
    }
  }

  Drupal.behaviors.mymodule_behaviorname_two = function(context) {
    // this code should fire automatically when the page has loaded
    console.log('Behavior 2');
  }
}(jQuery));

More on JS in Drupal (there are some examples in here for 6.x): https://www.drupal.org/docs/7/theming/working-with-javascript-and-jquery

  • 1
    I am not sure you need to worry about the field, you just want to get the script loaded in the page. After that, you can target js for elements with the standard jQuery selector. – Kevin Feb 28 '17 at 14:31
  • 1
    The module will literally only consist of the hook_form_alter code, and the js script file. It will be a great entry point into both learning how to make a custom module, and interact with Drupal in one of its most powerful hooks. – Kevin Feb 28 '17 at 14:32
  • 1
    The form id is not the literal form attribute - it is the Drupal identifier of the form. You can see what the form ID is by 'print $form_id; exit;' - or by using the devel module to debug. – Kevin Feb 28 '17 at 14:39
  • 1
    Here is another question that is similar regarding understanding what form id is, even though it is for D8, it still applies: drupal.stackexchange.com/a/229641/57 – Kevin Feb 28 '17 at 14:40
  • 1
    Without a debugger it is virtually impossible to develop in Drupal (or any platform for that matter). Devel is a major help. Otherwise it is like flying blind. I will add an example behavior. – Kevin Feb 28 '17 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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