3

I've tried rewriting the row and also theming the Views row, but I'm not having any luck. Maybe I'm doing something wrong.

I have the format of my view as 'Unformatted View', and in the format settings there is an option to specify the 'Row Class', which is then added to each row. However, I need to add a data-attribute to my rows in addition to a class. The data-attribute needs to be one of the fields - the Taxonomy Term name field (in lower case).

For instance, my field is Taxonomy Term Name. Currently, the HTML markup of each row looks like this:

<div class="my-row">Term</a>

However, I need it to look like this:

<div class="my-row data-attribute=".term">Term</a>

I am finding this fairly difficult to accomplish, and can't find much about it online. Could anyone be of help?

  • 3
    this post on drupal.org shows how to do it. – 2pha Mar 11 '15 at 5:16
2

The easiest way that I found to create unique attribute per views row is to override the template file to exclude the outer div and output each row using Global: Custom text in the view excluding from display all fields and including them in the custom text field.

Here is in more detail

  1. go to your view and click Advanced > theme > information
  2. in style output line the bolded file name is the currently used template (let assume views-view-unformatted.tpl.php)
  3. get the name next to the bolded file name (could be something like this views-view-unformatted--VIEWNAME.tpl.php)
  4. copy /all/modules/views/theme/views-view-unformatted.tpl.php to your THEMENAME/views-view-unformatted--VIEWNAME.tpl.php
  5. edit it and modify so it does not output the outer row div. It could look like this for unformatted style

    <?php if (!empty($title)): ?>
      <h3><?php print $title; ?></h3>
    <?php endif; ?>
    <?php foreach ($rows as $id => $row): ?>
        <?php print $row; ?>
    <?php endforeach; ?>
    
  6. edit all view field and check Exclude from display

  7. add Global: Custom text and add all fields in the content using replacement pattern
  8. your outer div in Custom text could look like this

    <div class="views-row my-row" data-attribute=".[field_FIELDNAME]">[field_FIELDNAME]</div> 
    
  9. refresh the cache and make sure in steps 1,2, your new template is used

| improve this answer | |
0

not seeing that your problem is views module specific. Maybe here is a solution for your problem outside of views formatting: https://www.drupal.org/node/1462436#comment-5674366

| improve this answer | |
  • I'm referring to a field created in the views module :) not a form field, a views field. – caustic Mar 11 '15 at 15:18
  • 1
    Ok I see. Then your goal may be accomplished with the views php module. Then you can use a php function to attach the data attribute to your field markup. Unfortunately this php programming goes over my head to be of much help in completing this, but the idea may be a starting point. Good luck! – krueschi Mar 12 '15 at 8:51
  • Just a note: Views PHP is something everyone should strive to avoid... but circumstances abound :) – doublejosh Apr 28 '16 at 1:17
0

You can do this by overriding the field's tpl.php file .. The needed template and the proper file name can be found at view's setting page >> advanced >> information

| improve this answer | |
  • 1
    We shouldn't have to override the field.tpl.php file, though. Maybe if I get some time I can build a module to extend this to the Format Settings where you can set a row class, be able to add data attributes. – Kirkland Jun 30 '16 at 18:37

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.