Sunday, 15 March 2015

Add CSS classes in the div of rows of drupal view

Purpose :


add a class name called 'test-row-class' in the div of each row of drupal view

Solutions :


There are two ways to do it. First, add the class via drupal admin; Second, customize view template file named 'views-view-unformatted.tpl.php'.

Via Drupal Admin:


  1. Find the machine name of view using 'Theme Developer' module. Enable the module, click the view you want to modify on the web page, and clieck the views_view on the pop-up window of 'Theme Developer'. After that, the machine name called 'price_location' can be found in the 'candidate template files' part  
  2. Search the machine name of view in the database table of 'views_view' and get the 'human name' of view which is 'Theme Price Location Sidebar' 
  3. Go to drupal admin page of 'admin/structure/views' and search the 'human name' of view. And then click edit of the specific view. 
  4. Switch to the display block that needs to be changed  
  5. Click the 'Format: Settings' in the 'FORMAT' section  
  6. Add the class name whichever you want in the input of 'Row Class' and the class name will show up on the front page. Here using 'test-row-class' as an example. 

Via Modifying Template Files:


  1. Enable 'Theme Developer' and click the view that needs to be change on the web page  
  2. Click 'views_view_unformatted' which is the descendant of 'views_view'. ('views_view' template is the top template file, while 'views_view_unformatted' is the template file displaying the rows of the view elements). And then the candidate tmeplate files will show up. 
  3. Choose one of the candidate template files you want to use. Here will take the most specific template file name of 'views-view-unformatted--theme-price-location--theme-price-location.tpl.php'. 
  4. Create the template file in the currently used theme folder and copy the code from 'views-view-unformatted.tpl.php' which is the default template file
  5. Change the code in the specific template file as you want and it won't affect other views

No comments:

Post a Comment