1

I have created a view for content type xyz with fields F1, F2,F3,F4.

I open the group "advanced", clicked on "Theme: Information".

There are couple of template files, that are used by view.

I chosen the desired template and copy it into to the template folder.

Now, from views module, in theme folder, I opened views-view-grid.tpl.php and copy the content of this file to the file I have created in my template folder.

But output is not coming in grid format.

Is there any way to customize the fields of view in html structure?

2 Answers 2

1

what if I want to use html and php

Ok, now that I remember there is another method.

Add the following code to the bottom of your template.php file (located in /sites/all/themes/yourtheme/ folder)

function themename_preprocess_views_view_fields($vars){

      if ($vars['view']->name == "view_name" && $vars['view']->current_display == "block_1"){
        $vars['fields']['title']->content = // do php or whatever you want to it here
      }
    }
  • replace themename with your theme name
  • replace view_name with your view name
  • replace block_1 with your machine name

To add html, Ex:

function themename_preprocess_views_view_fields($vars){

  if ($vars['view']->name == "view_name" && $vars['view']->current_display == "block_1"){
  $a = $vars['fields']['title']->content;
  $vars['fields']['title']->content = "<div class='others'>" . $a . "</div>";
  }
}

With this method your grid should remain intact.

enter image description here

1
  • @SugandhKhanna you're welcome, happy to help.
    – No Sssweat
    Commented Feb 2, 2016 at 23:29
0

Is there any way to customize the fields of view in html structure?

There are various methods to add html to your fields.

One of them if you just want to add divs to your fields

Another would be to REWRITE RESULTS. You need to use the replacement pattern.

ex: <div class="cool"><b>[field_example]</b></div>

enter image description here

Note: If you have more fields, they are not showing because they are below the body. If you want to show in the replacement patterns, you must in views drag and place the body field below them.

Also:

Make sure your format is Grid

enter image description here

3
  • But I want to achieve it only through template. (custom view tpl file). Commented Feb 2, 2016 at 9:20
  • Just so you know the REWRITE method it's just as good as the .tpl method if you are only using html. IMO the only time the .tpl method is worth doing is if you're gonna use PHP.
    – No Sssweat
    Commented Feb 2, 2016 at 9:41
  • and what if I want to use html and php both, (say) foreach(view[field]) add html div with some class? That's the only reason, i'm chasing tpl method. Because we can not use php itself to generate output in views. Commented Feb 2, 2016 at 9:59

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.