Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Another question, sorry.
Since there are templates for each view, does that mean there are dynamically generated preprocessors somewhere? I think not, since I can't find any code which looks like it might be doing that. To illustrate my example:
* view named my_first_view
* template is views-view--my-first-view.tpl.php
Following that logic through, one would expect to find (dynamically generated, of course):
function template_preprocessor_views_view__my_first_view() {
...
}
Which, following on again, would imply I can extend that with this in my own module:
function my_module_preprocessor_views_view__my_first_view(&$variables) {
$variables['new_var'] = 'foo';
}
Comments
Comment #1
merlinofchaos CreditAttribution: merlinofchaos commentedThere aren't dynamically generated preprocess functions; instead, all of the theme functions use the base preprocessor automatically. So
views-view--my-first-view.tpl.php
still usestemplate_preprocess_views_view(&$vars)
as its preprocessor function.You can add preprocess functions for specific views with the exact same naming conventions. Note that that it's named
*_preprocess_
notpreprocessor_
.Note that currently Drupal's design requires that you have the .tpl.php file in order to have the preprocess function (though you could drop in a generic preprocess function and do switches based upon information contained within the $vars['view'] variable. There's an issue for this which is fairly easily found by going to the Drupal queue and searching in the 'theme system' queue.
Comment #2
greg.harveySorry to be slow, but I don't want to spend hours scratching my head because I misunderstood you... So you mean I can do this in my module?
I've just been staring at the issue queue for half an hour and I don't see it. =(
It's late, I need my dinner, I'm going home. To be continued tomorrow.
Thanks again for your time!
Comment #3
merlinofchaos CreditAttribution: merlinofchaos commentedYou would use your theme name in place of template and as long as you have views-view--my-view.tpl.php yes.
Note that if using linux, another trick you can use is symbolic links so that you can have the same template without copying it, if you only want to change stuff in the preprocess function. =)
Comment #4
greg.harveyOoooo, nice idea! =)
Or junction in Windows. But I've had horrible issues with that in the past. Anyway, the dev and stage servers are Fedora and production server is Redhat, so we're good there.
Thanks *again*!
Comment #5
WorldFallz CreditAttribution: WorldFallz commentedinteresting issue-- just as an fyi the issue merlin is referring is i believe: Themes cannot have a preprocess function without a corresponding .tpl.php file
Comment #6
greg.harveyThanks WorldFallz. =)
Merlin, FYI, I've blogged a summary of my day researching here:
http://www.drupaler.co.uk/blog/theming-views-drupal-6x/67
If you find five minutes to read it and I've got anything wildly wrong, let me know.
I'm also wondering if this is possible on a module level. My experimentation indicates not, but I find this odd. If
themename_preprocess_views_view__my_view
works, I would expectmodulename_preprocess_views_view__my_view
to work too, but it seems this is not the case?Comment #7
merlinofchaos CreditAttribution: merlinofchaos commentedIt can be made to work but only if you use hook_theme() to register a .tpl.php file, which can then be overridden later in the the theme itself. The reason this is necessary is that discovery of the template happens during the theme section, which happens after modules; so the module's preprocess hook isn't looked for because that variant of the theme function does not (yet) exist.
Comment #8
greg.harveyAhhh, gotcha. Thanks! =)
I'd prefer this in a module because then if I switch theme my custom vars will still be available.
Comment #9
gollyg CreditAttribution: gollyg commentedsubscribing
Comment #10
merlinofchaos CreditAttribution: merlinofchaos commentedGreg;
There is one quibble with your article I have:
Not all view tpl files; only variants of the views_view theme. For example, this won't handle views-view-list.tpl.php. But the basic point is correct: phptemplate_preprocess_views_view_list() will handle all *variants* of views-view-list.tpl.php such as views-view-list--myview--displayid.tpl.php
Also, one mention is that you can pretty safely override views-view-field.tpl.php (an individual field) not as a template. Earlier I mentioned that Views wants everything to be a template but this is one place that I actually fibbed slightly, due to misremembering.
Fields are actually handled by theme function for performance reasons:
However, to make it work automatically when you implement a field template, I include a preprocess function that isn't used unless a template is discovered:
For performance reasons, not using a template for individual fields is actually a good idea. Sorry about the misinformation, and thank you for writing that so I realized my error.
(both functions, above, are in theme/theme.inc)
Comment #11
greg.harveyThanks for reading and I will update it now. =)
I won't bother documenting the additional point about theming fields, as I link to this issue anyway from that article.
Comment #12
greg.harveyChanged to read:
Comment #13
tayzlor CreditAttribution: tayzlor commentedHi Merlin;
following on from your suggestion:
i tried setting up the following in my module:
preprocessor function :
theme function:
I also have a tpl file in my theme directory - views-view-fields-VIEWNAME.tpl.php
I tried passing up additional variables from the preprocessor to the template file but it doesnt seem to invoke the preprocessor function at all :(
Comment #14
Anonymous (not verified) CreditAttribution: Anonymous commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #15
greg.harveyHi,
Setting this active again because of #13. I tried it too and couldn't get it to work. And I can't find a comprehensive guide to this. I would've expected the code in #13 to work, and I did create a hook_theme() too, but nothing. Any pointers? =(
Comment #16
greg.harveyTo follow on, printing out the $info array in the theme() function confirms the module preprocess is failing to register:
For this to work I think preprocess functions needs to look like this:
This is the code, which should work according to other module examples and the docs, but doesn't:
Even tried this, to try and force our preprocess to stick, and it didn't do anything either:
Comment #17
merlinofchaos CreditAttribution: merlinofchaos commentedWhat if you add an 'original hook' directive in there? I'm not sure that actually works though.
Comment #18
greg.harveyWill try on Monday and report back. =)
Comment #19
greg.harveyMixed results on this. With the following code:
I get the following $info output from theme():
As you can see, the Views module's default table preprocess gets dropped. So this seems to work, if I do this in my preprocess:
Interestingly, if I do this:
Then $info in theme() looks like this:
Which is almost correct, except the structure of the rows output changes (it's an object instead of an array and the data is raw instead passed through the correct handlers) so I guess this approach misses something somewhere.
Finally, I tried this, since I now know what the structure should be - I hard-wired it in my module:
This approach works fine. I'm not sure why this third approach would result in properly themed fields and the second one would not, but the first and third approaches both seem to work, while the second one works to an extent but for some reason the view output is not "finished".
Comment #20
merlinofchaos CreditAttribution: merlinofchaos commentedOk, I understand the problem and what's wrong.
This code works:
HOWEVER, there is a caveat. It ONLY works if the module weight > views; because it needs the pre-existing views theme function to exist for the 'original hook' directive to work.
In place of it, you can add the preprocess function manually, like Greg did above.
Comment #21
moshe weitzman CreditAttribution: moshe weitzman commentedTheme developer module helps a ton for understanding what .tpl.php file names you can use to customize your output. It also helps you know the variables that get passed into those templates. But knowing the available preprocess functions could be useful. I made a patch for theme developer at #312061: Show which preprocess functions were called on a theme call. Feedback welcome.
Comment #22
greg.harveyFiddled around with this some more this morning. I find the easiest way to make this work is to have NO 'original hook' at all (the red herring in this was I accidentally used hyphens instead of underscores in my 'original hook', so it wasn't doing anything anyway). This definitively works for me:
hook_theme()
preprocess function
Whereas trying to use the 'original hook' takes me back to the problem I had with the second approach in #19, the data is unfinished and unthemed.
Also, we tried to mirror this approach with
mymodule_preprocess_views_view_fields__view_name()
and could not get it to work at all, in any combination. Weird! =(Comment #23
merlinofchaos CreditAttribution: merlinofchaos commentedGreg: Read #20. I promise you, it works 100% *if* you set the module weight > views.
Comment #24
greg.harveyOk, will try this properly. I confess I didn't try your module weight tip, because I probably misunderstood this and tried to do the same again:
It seems this does not work in all cases.
Comment #25
merlinofchaos CreditAttribution: merlinofchaos commentedBTW, I plan to take what this issue is generating and write up a documentation page about it (and putting Views in a module in general) either today or tomorrow. I probably was being too lazy when I said 'above' since you tried a lot of things. The one I was pointing at that seems like it should work reliably if you are unable/unwilling to change the module's weight:
Note that I removed the erroneous 'original hook' designation. Also I should add that 'arguments' is wrong there too, and that could have unintended consequences (like, theme() shouldn't be able to figure out how to translate the arguments to variables without that).
Comment #26
greg.harveySounds great - look forward to reading the page! =)
Comment #27
merlinofchaos CreditAttribution: merlinofchaos commentedThis is now documented in the advanced help.
Comment #28
Anonymous (not verified) CreditAttribution: Anonymous commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #29
moshe weitzman CreditAttribution: moshe weitzman commentedMight be me, but I do not see this in the Advanced help for Views. Looking in HEAD.
Comment #30
merlinofchaos CreditAttribution: merlinofchaos commentedhttp://views-help.doc.logrus.com/help/views/api-default-views
Comment #31
greg.harveyI'm still having some problems with this - figured I'd post here so the thread continues, rather than starting a new one, but let me know if that's a bad idea...!
Following the docs, my module's weight > than Views' weight, check!
hook_theme() looks like this:
Template is in the root of the module (which seems to be where it's wanted - Drupal complains if it isn't there) and is named 'views-view-unformatted--gallery-contents.tpl.php'
Preprocess simply looks like this:
Nada! Firefox says "No document" or "No data" or somesuch.
But if I ditch the 'original hook' part everything is fine! WTF?! =(
I mean, it's cool because it works, but clearly I would rather understand why 'original hook' in hook_theme() seems to cause Drupal 6.9 to crash...
Comment #32
momo18 CreditAttribution: momo18 commentedI'm using Organic Groups and I want to customize the default og_ghp_ron view that comes with it. By default the view uses the "unformatted" view style. I changed the style to a table style and all the data I need is in there correctly.
However, essentially what I want to do is pluck out the fields and arrange them however I want in a table. In order to do that properly, I thought I should use a preprocessing function to redefine the field names, so that I could just drop them into a template wherever I want. Some of the fields are CCK fields.
I created a module and defined the preprocessing function there but when I did that the table view got messed up and for example, the dates lost their format and were not expressed the way they should.
I'm not a PHP whiz, so I'm finding it hard to find some concrete examples on what the correct development path to correctly achieve this basic task is.
Should I be creating a preprocess function per view (preferred), or just change the default views preprocess functions (field, unformatted, table, grid, etc.)? If I do, then how do I correctly redefine variable names and format the date (which is getting messed up) through the preprocessing function? How about changing the date format then through a .tpl.php file?
Which style should I be using (table, list, unformatted, grid) to extract the fields and why use one style and not the other?
What's the difference between the two row styles (fields, nodes)?
Some of the template files for example use code in them (such as content-field.tpl.php)? Should they be moved to the preprocess function instead to separate presentation from logic?
Are there any complete examples of calling embedded views from within a .tp.php template file, and how do you call one view from another view? Wouldn't it be easier to write a view like this and customize it in code rather than through an interface?
Unfortunately there aren't any beginning to end examples or best practices to perform common views tasks such as this out there, because I'm sure a lot of people have run into it this issue.
Thanks for any help,
Mo
www.itwrite.com | Having fun with Drupal
Comment #33
pixou CreditAttribution: pixou commentedHi guys,
I'm having troubles with adding a preprocess function for a specific display.
So, I have a view, with a particular display.
In my module I have this code :
Then I wrote this function :
Unfortunately, my function gets a NULL $rows, so does my template...
Any idea ?
Thanks
Comment #34
davebv CreditAttribution: davebv commentedI am trying to make a module to implement a new style and the preprocess function is in the theme registry but it is not executed. I changed the weight of the module but it still does nothing. I see this thread is quite old, but I cannot find any resource doc to clearify why is this happening.
I followed the steps here http://groups.drupal.org/node/10129 and http://views-help.doc.logrus.com/help/views/api-default-views but it still does not work.
Comment #35
JeremyFrench CreditAttribution: JeremyFrench commentedI have just got this working locally. I thought I would post my process here as it is not quite straigtforward.
First make sure your modules weight is > views
Secondly copy the template you would like to add a preprocessor to to your modules directory. Rename it to be something in the list of templates in theming information.
Edit hook theme like this (but change to use the existing view that you need to override.
You should then be able to write your preprocess code in the function mymodule_myfunction.
I had issues before this with the args array being wrong, using the above method your args should keep up with any changes in the views args.
Comment #36
andreiashu CreditAttribution: andreiashu commentedHi everyone,
About the mymodule_preprocess caveat that merlin mentions in #20 regarding module weight: why can't one just add the preprocess function in a hook_theme_registry_alter?
Something like this:
It seems to work pretty nice.
Any thoughts?
Comment #37
andreiashu CreditAttribution: andreiashu commentedComment #38
dagmarNeeds review is only to review patches.
Comment #39
greg.harveyI guess we ought to close this and direct people to the Drupal 6 theming system. From my experience this all works ok now. It was only a support request when we raised it and any discussion/suggestions about approaches to using templates and preprocess functions ought to occur in the appropriate handbooks:
http://drupal.org/node/223430
http://drupal.org/node/352970
Btw, on my travels I found this, which might be useful - I don't know if it's still an issue:
http://drupal.org/project/preprocess_order_corrector
Comment #40
kyle_mathews CreditAttribution: kyle_mathews commented#36 worked perfectly for me and is a heck of a lot simpler than the other options fwiw.
Comment #41
couloir007 CreditAttribution: couloir007 commentedI have a situation with hundreds of view tpl's and corresponding preprocess functions. I inherited this, and I'm working to tame this wild ecosystem. In the meantime, it has been suggested to me that I should maintain the views in a module. Do I need to register each preprocess in the module? I've tested one view according to the advanced help, it works, as far as the view residing in the module, I've updated the weight to 11, but I can't get the preprocess functions to be read.
Thank you.
Comment #42
couloir007 CreditAttribution: couloir007 commentedIs there any reason why this is a bad idea? It works.
Comment #43
KarimB CreditAttribution: KarimB commentedComment #44
KarimB CreditAttribution: KarimB commentedYes! #36 works like a charm!
Comment #45
andreiashu CreditAttribution: andreiashu commentedFor whoever is interested I created a module to fix this issue so that you don't have to do the
hook_theme_registry_alter
manually for each of your views: views_preprocessors. The dev snapshot for D6 should appear on the project page within the next 12 hours and I should have the 7.x version up by then too. If you are impatient do a git checkout.Cheers,
Andrei
Comment #46
nymo CreditAttribution: nymo commented#36 works for me.
Comment #47
liquidcms CreditAttribution: liquidcms commented#36 causes D7 view page to whitescreen
Comment #48
foredoc CreditAttribution: foredoc commented#25 works fine for me. And the only problem I have is that, I need to keep :
views-view-table--insights-authors.tpl.php
in my current theme folder, in order to make it work properly.
Any ideas how to support the template file in my custom module's folder?
Thanks.
Comment #49
foredoc CreditAttribution: foredoc commentedWhat I did:
However, seems to me that the 'path' does not work, if I remove the template in my theme folder, and only keep it in my module's folder, then I will have following error:
PHP Fatal error: Call to a member function sanitize_columns() on a non-object in ../sites/all/modules/contributed/views/theme/theme.inc on line 323
Any pointers?
Thanks.
Comment #50
hongquan CreditAttribution: hongquan commentedThanks
Comment #51
joey-santiago CreditAttribution: joey-santiago commentedI'm trying to achieve the same result, but can't get it work. I'm working on this project where i have two themes: one developed in many years that would be the main theme for desktops and has a huge amount of custom templates, the other one is a fresh one that i would like to be as lightweight as possible avoiding the use of tpls, and especially avoiding rewriting the same tpls as the main theme.
So i'm trying to move the tpl files to a custom module in order to have them shared by the two themes.
Here's what i did until know:
1: made a symbolic link from my main theme templates directory to my new module's dir.
2: inserted this hook in my module:
What i get is the preprocess function is called, but i can't get why the template file is not:
is an empty array and the output is empty.
Can someone help me understand what happens here?
Would it be possible not only to make this work, but to extend this concept to all of the views i have, so that if on the templates directory i have a tpl for that view, then the code would use it?
Thanks for helping me out with this.