3

I'd like to add some Javascript into Drupal's admin area (content creation to be specific) and was wondering what's the best approach to do it: I'm using the admin theme Rubik. Shall I just add the JS as part of the Rubik theme? Should I create a sub-theme? But its just one file...

  • Looks like I missed the "content creation" part of your question. I updated my answer. – Fabian Iwand Oct 8 '15 at 13:01
6

@kiamlaluno explain answer very good way

Using a module, you can follow two methods:

Implementing hook_init() Implementing hook_form_alter() The first method would allow you to add the extra JavaScript (or CSS) files to any administration pages, while the second method would allow you to add those files only to pages containing forms.

function mymodule_init() {
  if (arg(0) == 'admin') {
    $path = drupal_get_path('module', 'mymodule');
    drupal_add_js($path . '/mymodule.js');
    drupal_add_css($path . '/mymodule.css');
  }
}

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if (arg(0) == 'admin') {
    $path = drupal_get_path('module', 'mymodule');  
    $form['#attached']['css'][] = $path . '/mymodule.css';
    $form['#attached']['js'][] = $path . '/mymodule.js';
  }
}

Replace "mymodule" with the short name of your module; replace "mymodule.js" and "mymodule.css" with the actual names of the JavaScript and CSS files.

more detials

4

If your script is adding functionality, a module might be an even better place (we usually keep a "backend_helper" module around for that purpose).

To add your script to every admin page, use:

/**
 * Implements hook_page_build().
 */
function mymodule_page_build(&$page) {
  // Alternatively we could check for the current theme.
  if(path_is_admin(current_path())) {
    $page['mymodule']['#attached'] = array(
      // ...
    );
  }
}

If you only plan to attach your scripts to particular forms, use hook_form_FORM_ID_alter(). For node add forms, this would be:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_node_form_alter(&$form, &$form_state) {
  $node = $form['#node'];

  // Case 1: Only attach on "Add" forms.
  if(empty($node->nid)) {
    // ...
  }

  // Case 2: Limit to certain content types.
  // This can also be achieved by using the "my_content_type_node_form" form ID.
  if($node->type === 'my_content_type') {
    // ...
  }

  // Both conditions can of course be combined. Finally attach your script as described above.
  // You can also reference your theme, if this is where your script is located.
  $form['#attached']['js'][] = drupal_get_path('module', 'mymodule') . '/path/to/script.js';
}

Note: Do not use hook_form_alter(), as it will affect all forms. In most cases using the form specific hook is the preferred method.

  • what about if I don't want a module? This is a small temp teak – WagnerMatosUK Oct 6 '15 at 8:32
  • 1
    Why do you ask for best practices if this is only a temporary change (which would be undone later, if I understand you correctly)? In any case, a module would be far more light-weight than a child theme. – Fabian Iwand Oct 6 '15 at 8:37
  • I wanted also as reference, i.e.: know the best approach but also the quick fix. So the options are either create a module (best approach) or create a sub-theme (heavier than a sub-them), is that correct? – WagnerMatosUK Oct 6 '15 at 8:39
  • Besides using contrib modules that provide similar functionality, a custom module is the only method that I would recommend. – Fabian Iwand Oct 6 '15 at 8:42
2
function custum_form_alter_init() {
  if (arg(o) == 'admin') {
    drupal_add_js(drupal_get_path('module', 'custum_form_alter') .   '/custum_form_alter.js');
  }
}

and add custum_form_alter.js file in your module file

  • Note to future readers: In Drupal 7 a module should use #attached, instead of calling drupal_add_js() or drupal_add_css(). – kiamlaluno Apr 19 '16 at 18:18

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.