1

I have a piece of content with an 'Asset Type' field value of Vidyard. I am using the Views module to render this content on another page. I have added those fields to the View, but is it possible to add them as a data-attribute to the HTML. For example, the Views module outputs:

<li class="views-row views-row-2 views-row-even views-row-last">
    etc.
</li>

How can I add my "Asset Type" field as a data-attribute to the list item. Making my output:

<li class="views-row views-row-2 views-row-even views-row-last" data-asset="vidyard">

Is there a way to do this without altering the template?

1 Answer 1

1

There are some limitations if you can't change the output templates for the view (and I'd assume if that's the case a views pre render is also out of scope). If you can accept them within the <li> (instead of on the <li>) you can rewrite the field to use a token. Make sure the field that is just data is listed before the field it will be attached to, enabled rewriting on the field, and use the field tokens to attach the value as an attribute. You'll most likely want to suppress the field from output and do some data cleaning to make sure invalid characters don't end up in the token (like ").

Views editing screen shot for rewrite

You can attach classes based on the row value to the li by using the same token you would use on the rewrite. Drupal will ensure it's safe for output, but then your data is a class and not the attribute you are seeking.

You'll have more control over the output, and you'll be able to build more elegant solutions if you use hook_views_pre_render() to modify the output more directly.

0

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.