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?
In my project, I have two list based fields called
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
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:
<?php $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.