I want to use a custom function in views-view-fields--field-name.tpl.php
template file. Where do I have to put it? Is it possible to use it in template.php in some preprocess functions?
-
Add your function in any file and include the file in views-view-field--field_name.tpl.php.– Sumit MadanCommented Aug 3, 2013 at 11:19
-
You should include the file in views-view-field--field_name.tpl.php using module_load_include. Example : module_load_include('file_type', 'module_name', 'base_file_name'); api.drupal.org/api/drupal/includes!module.inc/function/…– InizioCommented Aug 3, 2013 at 22:36
Add a comment
|
2 Answers
You can use a preprocess function in your template.php
file. For example:
function YOURTHEME_preprocess_views_view_fields__field_name(&$vars) {
// Do your changes for the field here
}
In Drupal 7, you can achieve this using module_load_include OR include_once.
- Create a .inc file in the custom module, which will keep all the template files functions, or if you have only one function then just add the function in your custom module.
- Go to your template file (views-view-fields--field-name.tpl.php) and include the file using
module_load_include()
orinclude_once()
- If it is a .inc file, use
module_load_include('file_type', 'module_name', 'base_file_name');
- If it is a function in a custom module, use
include_once 'FILE_PATH';
- If it is a .inc file, use
-
If it is a function in a module, then there isn't any need of using
include_once()
since every enabled module is loaded when Drupal completes its bootstrap. It's only on early bootstrap phases that the enabled modules are still to be loaded.– avpaderno ♦Commented Aug 5, 2013 at 6:19