2

In a D7 instance, in Views under "Rewrite the output of this field" I have this in the textarea:

<a href="[field_downloads]" title="[field_downloads_1-description]" onClick="recordOutboundLink(this, 'Downloads', '[field_downloads_1-description]'); return false;">[field_downloads_1-description]</a>

All data is displaying except for the onClick attribute. Any ideas why?

2 Answers 2

5

Views rewrite uses the filter_xss_admin function to produce safe html. It's designed as a security measure and is in place even if the user has permissions to add PHP and unfiltered HTML in other contexts.

You can work around by using Views PHP for example. See this issue at drupal.org for discussion and more on workaround approaches.

2

After spending a few hours digging around, I found the code based solution. You can't add onClick attributes within the Views UI because of xss filtering.

This code can be used in a custom module.

// Adding custom link attributes to a views field for ajax calls.
// See around line 1330 in views_handler_field.inc.(drupal 7)

function MYMODULE_views_pre_render(&$view) {
  if ($view->name == 'my_expected_view_name' && isset($view->field['my_field_name'])) {
    $attributes = array('onclick' => "alert('Hello')");
    $view->field['my_field_name']->options['alter']['link_attributes'] = $attributes;
  }
}

Your Answer

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

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