Can we please add an onclick attribute to this module?

#11 onclick-1923740-3.patch3.27 KBjpoika
#8 onclick-1923740-2.patch3.2 KBjpoika
FAILED: [[SimpleTest]]: [MySQL] 111 pass(es), 1 fail(s), and 0 exception(s). View
#1 onclick-1923740-1.patch2.79 KBalbertski
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch onclick-1923740-1.patch. Unable to apply patch. See the log in the details link for more information. View
Members fund testing for the Drupal project. Drupal Association Learn more


albertski’s picture

2.79 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch onclick-1923740-1.patch. Unable to apply patch. See the log in the details link for more information. View

Here is a patch that implements this:

Mołot’s picture

Status: Needs review » Reviewed & tested by the community

Works for me and mines.

Stephanie_42’s picture

This patch does works for me, I've got an issues with my onclick quotes getting encoded (javascript based click tracking), but this may not be the patch - I'mnot sure yet.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 1: onclick-1923740-1.patch, failed testing.

Diane Bryan’s picture

Issue summary: View changes

Very interested to see this fixed! I'll ask our Tom to look into it.

joelpittet’s picture

Considering adding this feature if it was disabled by default, and maybe add a tests for the encoding that @Stephanie_42 mentioned in #3

Would turning it off by default, and then just allowing people to enable it through the menu settings work for you?

@Stephanie_42 is it the double quote or single quote encoding that is not working? I would assume that double quotes would be encoded because you'd break the attribute. Also a bit concerned for security here... but discuss that later unless some one can speak to that?

limonazzo’s picture

4 drupal 6 i use Menu attributes and modify:

function menu_attributes_admin() {

  // onclick Attribute
  $form['menu_attributes_onclick'] = array(
    '#type' => 'fieldset',
    '#title' => t('Onclick Attribute'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE
  $form['menu_attributes_onclick']['menu_attributes_onclick_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable setting the onclick attribute'),
    '#default_value' => variable_get('menu_attributes_onclick_enable', 1)
  $form['menu_attributes_onclick']['menu_attributes_onclick_default'] = array(
    '#type' => 'textfield',
    '#title' => t('Onclick Attribute Default'),
    '#default_value' => variable_get('menu_attributes_onclick_default', NULL),
    '#required' => FALSE


function menu_attributes_form_alter(&$form, $form_state, $form_id) {
  if ((isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) || ('menu_edit_item' == $form_id)) {
    if ($form['#node']->type .'_node_form' == $form_id) { // It's the node edit form
      $item = $form['#node']->menu;
    else {
      $item = $form['menu']['#item'];
    if (isset($form['menu'])) { // Check to see whether the menu form exists
      $form['menu']['options'] = array(
          '#type' => 'fieldset',
          '#title' => t('Menu item attributes'),
          '#access' => user_access('administer menu attributes'),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#tree' => TRUE,
          '#weight' => 50,

    //  ->      
      if (variable_get('menu_attributes_onclick_enable', 1)) {
        $form['menu']['options']['attributes']['onclick'] = array(
          '#type' => 'textfield',
          '#title' => t('Onclick attribute'),
          '#description' => t('Enter onclick methods'),
          '#default_value' => isset($item['options']['attributes']['onclick']) ? $item['options']['attributes']['onclick'] : ($item['mlid'] ? NULL : variable_get('menu_attributes_onclick_default', NULL)),
          '#required' => FALSE,
    // <- 

..... code

on menu_attributes.module.

jpoika’s picture

Version: 7.x-1.x-dev » 7.x-1.0-rc3
Status: Needs work » Needs review
3.2 KB
FAILED: [[SimpleTest]]: [MySQL] 111 pass(es), 1 fail(s), and 0 exception(s). View

I have made the changes to the rc3 version of menu_attributes. Can this be part of the standard?

Status: Needs review » Needs work

The last submitted patch, 8: onclick-1923740-2.patch, failed testing.

joelpittet’s picture

I'm not sure we want to do this but you can add this with hook_menu_attribute_info() in your own custom module quite simply.

jpoika’s picture

3.27 KB

Corrected the error, in case you change your mind.

joelpittet’s picture

@jpoika maybe you could create a new module called "menu_attributes_events" and add all the JS event attribute handlers. onmouseover,onblur, etc?

malcomio’s picture

Status: Needs work » Closed (works as designed)

Yes I think it's better to keep event handling out of this module and create a separate module for people who would want that.

For me, Javascript code belongs in a module or a theme so it can be under version control, and allowing code to be added via the CMS is an extra attack vector.

This module allows adding an id to a link, so it's very straightforward to target any menu link.