I am using View template files to do custom theming of my views.

My custom code is a bit complex, so I want to put it into a function in a custom module (or even my template.php* file). That way, I can keep my template file clean and re-use the same code between multiple Views templates.

In a Views template files, fields are called with the following code:

   <?php  print $fields['field_name']-> content ?>

However, this code cannot be used inside a module.

How can I call a Views field inside a module/template.php file?

At the moment, I have created Variables in my function. Then when I call the function inside a views template, I assign the Views fields to the variables. However, this makes the code quite complex. It would be better if I could just call the fields from the module.

*By template.php I mean the file called template.php and not a tpl.php file.


I have a field called body text. In my views tpl.php file, I would render it with the following code:

 <?php  print $fields['field_body_text']-> content ?>

How can I call that Views field inside a module function, and then call that function in the Views tpl.php file, so the field renders?

Extra info:

In my project, I have two list based fields called call_to_action_setting & second_cta_setting. I also have several text fields, but not all of these are rendered on every page. In my views-view-fields--view-name.tpl.php file, I check the value of the list fields and then display the appropriate text field, using views_field_compare_value.

Here is the code that I use in my views-view-fields--view-name.tpl.php.

It is quite complex for a template file. Plus, I am using it in several template files, so I want to make it a function that can be stored in a module.

Here is how I have put it into a module. I can then call it inside views-view-fields--view-name.tpl.phpn using the following function:

$cta_settings = $fields['field_call_to_action_setting'];
$cta_settings_2 = $fields['field_second_cta_setting'];
$custom_text =  $fields['field_call_to_action_button']->content;
$custom_link = $fields['field_call_to_action_link']->content;
$custom_prompt =  $fields['field_call_to_action_prompt']->content;
$custom_text_2 = $fields['field_second_custom_button_text']->content;
$custom_link_2 = $fields['field_second_cta_link']->content;
legendarysettings_call_to_action($cta_settings, $cta_settings_2, $custom_text, $custom_link, $custom_prompt, $custom_text_2,  $custom_link_2); 

This function, though, is quite complex, so it would be better if I could call the Views fields directly from the module.

  • I would look into the various Views preprocessing hooks, check out api.drupal.org/api/views/views.api.php/group/views_hooks/7 for a good list and then if you have any specific questions, edit your question and we can help, but at this point, it is pretty wide ranging and a bit vague. Also, if you look over here (to the right -->) you can see many related questions that may be of help as well. – Jimajamma Feb 21 '14 at 14:39
  • re: your added Example: when you ask, "How can I call that Views field inside a module function?" what do you mean? Do you want to see what it is inside your module? Do you want to change it so it renders differently in the resulting View? What do you want to do? – Jimajamma Feb 21 '14 at 22:53
  • @Jimajamma Thanks for your help!, I have added more info. – big_smile Feb 22 '14 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.