Drupal Hook Preprocess Views View Fields
A Drupal Tutorial
Drupal Hook Preprocess Views View Fields Transcript
Hello everyone welcome to Lunch n’ Learn. Today we'll be talking about the very robust hook, called Hook Preprocess Views View Fields. It's used in a lot of places in Drupal and in a lot of modules and themes and so forth, so you come across it pretty often. However I want to talk today about a couple of general reasons why you would use this and a couple of specific examples of where this Hook could come up.
It could be very effective for a couple of really quick solutions to common problems. So, to start with, we have a basic D7 site here that is called Lunch n’ Learn - just features recipes on it. Basically, all we've got here is a simple content type called “recipe” which includes an image, title, and body, and this is just a simple view that is done - a simple page - it's done using the fields of this node and it shows off the published content in order of creation date (pretty basic) set to the front page.
Nothing really special about this site, just aside from that. The only thing that I did different here just for simplicity in setting up this example was I made the Bartik theme, that's the core theme, a custom theme so that I could alter it without having to feel bad about hacking the core and what not.
So let's take a look over at our template.php file for Bartik and we can actually take a look at the Hook in question for today. So as usual, we have the Bartik name at the front of our Hook here, which is the pattern for all Hooks, and so since we're in the template.php of the theme we can use the Bartik to replace that theme there. And the API for this Hook gives you, like, a lot of preprocessing Hooks; gives you a reference to the vars variable and this will have all the data for the view in question.
So, today, obviously we're going to be targeting our page view here and a couple of fields here. So, to start with, like with a lot of preprocessing views field, you'll probably want to localize it to something less robust. Here I'm just using the name view. And when you're preprocessing fields using this Hook you are actually going to go through and it's gonna hit every single field that you have in views.
So the first thing you're typically going to want to do is go through and check that you're currently looking at the view you want to alter and you're looking at the right display of that view. So to find that - usually pretty simple - hopefully you named your views yourself. If you didn't, you can of course double check the name description, and it's usually named something similar to something that was human-readable that became the machine name, which in this case is “recipes.” If that's not the case you can check it out up in the URL here, then you'll see that it says views view, it'll have the machine name there, so it's easy for us to find it.
Second thing you'll need to know is the current display. In this case it's page - got pretty lucky there since there's only one view - and that can be found under the advance tab. And then there's the machine name right there, page (and those are usually easy to guess anyways), page block, page one, block one and so forth.
So the first thing I'm checking there is if I'm talking about the right view now, and in my case since I only have one view I could theoretically just not check that, but who knows how big my Lunch n’ Learn site will eventually become. The next thing I want to go through is that I want to actually investigate the fields they're coming in, so in this case we're really gonna be focused on the title and altering the title.
So I want to see that I'm actually looking at the current field that I'm looking at is the title. So, when you get that vars variable up at the top, you're actually getting all the fields there. So you're not going through, like, one at a time so it kind of brings us to what's great about this Hook.
One of the best things about it is since you have all the fields there you can actually start to do associated data stuff. So you can base the alteration of one field based on the value of that other field - and we'll take a look at a couple examples of that in a moment here - but that's one reason I like this Hook because I can use fields together in concert usually in a pretty quick way and I can also do more PHP (if that's such a word) stuff to them. So let's start with a really simple example.
First example we're going to look at is how we get to altering anything. So in this case, I'm going to wait. I'm going to go through my forage loop here and get the ID of the current field. And I'm going to say if the field is currently title, and again you can easily get field names. You can check your sql output (if you have that setting turned on) if you want to, you probably already know the names of your fields, probably based on when you were altering the node. But you can also check here, you can look in inspector really quick and I can see this pattern inside of the views row where it says views field, field image and that's the pattern, is “views field - something”.
In this case title, and above it field image, and of course title body don't have the additional word field in front of it because of their special, special nature of being part of core. So in this case, I see the ID sql title then I go in and I just get the content for it so this should seem pretty straightforward based upon the loop that we're in here.
I get the content and now I can set the content to whatever I want so in this case I'm going to make all the titles say only “tacos.” Hit refresh and of course I can quickly see that I now have everything labelled as only tacos. Okay, probably not the most effective use of this Hook but just shows you really quickly the syntax that you would use to alter most of your views.
You are going to find out if you’re in the right view, if you get the right page or right display, go iterate through the fields, check the fields based upon their field name, check there, and then do something to them.
So that's a really basic example. Again not very effective but I wanted to bring up a pretty common thing that I see which actually has to do with labeling. Commonly, what you'll see in a lot of situations is you'll need to put some sort of meta tagging there around content.
So this can be something like the schema.org system, or in the example I'm gonna give here, the Aria Labelledby system, Aria Label. Aria labels when they're essentially there for accessibility reasons. And so Labelledby can often come up when you need to label something, such as - in this case I'm going to give a fake example of “read more” - a lot of times you have lists of blocks or a bunch of blocks of teaser content - something like that - and you always have “read more,” let's say at the bottom, it's a pretty standard thing for a lot of blogs.
And maybe I can have it for “recipes” in this case. But that “read more” isn't really indicative for someone who can't see the screen. So what you'll do is you'll apply a meta label that'll be in this case Labelledby that will tell you what a screen reader might tell you, what the content is for “read more” there.
So if you “read more about this” so instead of just “read more” generically so that's the motivation for this and that Labelledby needs to be right next to the content that's talking about. So it can't be high up in the views row or something like that, and so this brings us to the second point about when you would want two primary points about when you'd wanna use this Hook which is I wanna look up some associated content that I have, and I also want to alter something at the lowest possible level.
So the content that you're putting out, the content that we're altering as you just saw in the tacos example is the final thing that's coming out. So there's not gonna be all these wrappers around it. So I wanna be close to the content and I also wanna look up additional stuff about the content and apply that to another field.
Now you could do this in a lot of ways, you could do this with insider views but you might have something, this might be a more general thing, “read more” is pretty - say you had a field “read more” everywhere throughout your site - maybe it's a little bit easier to go in and add this Hook in there rather than going into every single view and then rewriting the output, for example.
So in this case, I'm making the assumption that title's in there - we've already kind of seen that - I get its content, I've localized it to something friendly, I go and I look up and again we're just assuming we have a “read more” field, I go and I reset it's content wrapped in a span with my Ariel Labelledby, hope I get that syntax right, and I add my title in there, and then I print it out, then I complete the tag.
And then this would replace that content for that “read more” field. Okay so that's a really common one and also really effective and can be used as a global tool or a section tool or something like that rather than having to go and put everything in the gooey.
This is also really helpful if you need to grab stuff because it's not hidden in the database. Another common thing I see is to insert additional, take content and maybe transform it into something that isn't as fun for Drupal to handle. So in this case you would have a YouTube link again making that up and instead of outputting the content of just the link text, I want to actually go ahead and wrap it in YouTube standard iFrame format.
Okay, so under user experience, they only have to go in and put in the length for YouTube, and then you go ahead and you format it. Again, this is something you could apply globally, this is also another way to set rules within your site so you would say, okay cool, our videos are this size, they follow the iFrame format or something like that.
Okay that way you don't have YouTube videos in WYSIWYG, you don’t have to mess around with that. You don't have six different sizes in your blog post. Or something along those lines you can just say okay simplest thing for my users. If your users may not have much excitement about messing with WYSIWYG simple thing there they might want to just fetch the URL put it in there, and then you take care of the hard part and your site looks nice and clean.
Now obviously this isn't very Drupalish right here but I just want to it to serve as an example. So this function “best recipe,” in this case since we have a recipe site we could actually be in the module some place or another part of the template file we call best recipe. But what we could do with this is if we assumed for a moment that best recipe was something that did a little bit more complex logic than just say, I don't know, has the most number of votes, has the most number of PDF downloads or something like that. In my case I've replaced that logic with just saying whether or not the index is zero, just to give us a short example. The point is, is that the “best recipe” in this case imagines that we're actually going and we’re asking other parts of the system to do work for us that maybe it isn't appropriate to do in views, or maybe it's been built out for other parts of the site, and we can leverage that functionality for say, doing a custom display.
So in this case I'd want to highlight my best recipe by wrapping it in H2’s so it'll look bigger than the other titles. This could also be something like an additional CTA, little stars and fireworks on the sides, or what have you. But the point being is I want to go through and change the output of this title based upon information that may exist some place else, it doesn't easily integrate into views.
So I took something really simple before, I'm looking for the title, I now have this other condition, and at this point I'm just turning at the row index, the row index just being a simple thing that I could throw in there that would actually allow this example to work because I didn't actually write a best recipe module or anything like that.
So point being go through, I hit save and now “Creme Brulee” is now bigger than the others. So again it totally depends upon what your domain is and why you’d actually be doing this. But the important part is that you are leveraging other data services throughout your site. So to wrap up the importance of Hook Preprocess Views View Field is that it's really useful when you need to get right next to the content, as close as you can to the final output, and you're usually doing something that involves some dependency or associated data with it.
So that's usually the core of why you're messing with these. Good three reasons you might do that: You might want to wrap content meta data, you might want to add a easier way of entering content for your users, if they’re messing with more complicated content entry stuff. And finally you might want to generate output that is based upon some other data in your site or other services in your site.
Hopefully you found this useful and have a good day.