-2

Basically 1. I am trying to edit a view.
--> For that I have copied the views-view.tpl.php file from the views module's theme folder
and placed it into my custom templates folder present inside theme folder.

--> Also I have renamed the file views-view.tpl.php file to views-view-[VIEW_NAME].tpl.php where [VIEW_NAME] is just placeholder for actual view name.
--> After this I have cleared the caches.
--> Now In my custom theme function I have a template.php file.
--> In that template.php file, I am using [THEME_NAME]preprocess[VIEW_NAME] function that is my_theme_preprocess_view_name(&$vars) function When I check the vars object I have found the following place where I need to put the html
for the field's html to change which is as follows :

$vars['view']->result[$key]->field_name[0]['rendered']['#markup']

which contains the html of the field I want to alter .So Using this vars variable I inserted the html as follows :
$vars['view']->result[$key]->field_name[0]['rendered']['#markup'] = $html; Where html is the variable which contains the altered html value for the field .
--> After this I again clear the caches. -->But the still the html value of the field does not change.
Can you please help me out on this ...
I tried doing this using hook_views_pre_render(&$vars) inside a custom module. It works
there but I wanted all the code inside the template.php file as I have read that
hook_views_pre_render() is not the proper way to alter the field value.

3

Views are complex in the way they render.

The best thing to do is override things at the right level.

If you want to override things at the view level, for example the title of your view, you would use views-view.tpl.php; if you want to override something at the row level, you might use views-view-fields.tpl.php; if you want to override an individual field, you would likely use views-view-field.tpl.php.

Basically, if you look in the views-view.tpl.php file, you will see that individual fields are not being printed, which indicates that this is probably not the place to override that.

In your case try using views-view-field.tpl.php.

Check out the "Theme: Information" section of your view edit screen for suggestions on what to name your file along the lines of views-view-field--view-name--field-name.tpl.php

As for whether or not it should be in the theme or in a module, a good question to ask is: If I change themes, would I want this same change in the new theme? If so, it is more functional and probably should go in a module; if the answer is no, it probably should go in the theme as it is specific to that theme.

That's good as a basic guide, anyway; sometimes there are exceptions to rules.

  • That's it: If you need that functionality independently from the theme currently used, the code should go in a module. That is well said. – kiamlaluno Sep 2 '13 at 18:54

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.