0

This is tricky to explain properly...

I have a view, and I want to override the output of one of the fields, so I created a template.tpl.php file, called:

views-view-field--frontpage-fold--page--field-video-background-files.tpl.php

I at first had the logic in that file, and everything worked as expected. Great!

Then to conform with best practice, I moved the logic part (that was at the start of my .tpl.php file), to a theme function override thingy. This looks like:

function MY_THEME_views_view_field__frontpage_fold__page__field_video_background_files(&$variables) {

And that works fine... With an exception.

THEY DON'T WORK TOGETHER!

Depending on how I fiddle around with clearing caches, and using views 'Rescan Template Files' button, I either get one working, or the other. Not both.

The result of this is that it's impossible to separate my logic into a template.php file and pass variables to my .tpl.php file.

Does anyone have any idea why this might be?

I'm fairly new to Drupal, and have found the following pieces of information, but don't quite understand what they're saying...

https://stackoverflow.com/questions/10433853/drupal-7-preprocess-views-not-working https://stackoverflow.com/questions/2383865/how-do-i-use-theme-preprocessor-functions-for-my-own-templates/2387782#2387782 https://www.drupal.org/node/939462

Can anyone clear this up for me, and suggest a solution?

1 Answer 1

0

I think what you are looking for is:

function MY_THEME_views_view_field__frontpage_fold__page__field_video_background_files_preprocess(&$variables, $hook) {

Note the addition of _preprocess at the end of your function name.

https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_preprocess/7

4
  • I thought _preprocess should go between MY_THEME_preprocess_THEME_OVERRIDE_SUGGESTION? Also, I thought that _preprocess could be omitted when dealing with a theme override suggestion?
    – Jack_Hu
    Sep 21, 2015 at 8:14
  • _preprocess is to perform all the logic you need on the variables before you use them in the template. So for your scenario it's the correct function. Sep 21, 2015 at 9:51
  • I know that, but it goes between the theme name and the override function... THEMENAME_preprocess_THEMEFUNCTION. Not, THEMENAME_THEMEFUNCTION_preprocess... My issue is to do with the suggestions. It's a known bug in drupal core, but I don't understand the work around: drupal.org/node/939462
    – Jack_Hu
    Sep 21, 2015 at 10:03
  • You're right about the order of_preprocess. However if you're trying to move logic out of your template file (which as you say is best practise) then the preprocessor is the right function for that. Sep 21, 2015 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.