Can we please add an onclick attribute to this module?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

albertski’s picture

FileSize
2.79 KB

Here is a patch that implements this:

Mołot’s picture

Status: Needs review » Reviewed & tested by the community

Works for me and mines.

StephanieFuda’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.
Thanks!

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?
admin/structure/menu/settings

@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:

menu_attributes.admin.inc

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
  );
...... 

and

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
FileSize
3.2 KB

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

FileSize
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.