Drupal 7 modifying field output before rendering

Snippet

Or, how to add a # to a tag vocabulary field, making it a #hashtag

#hashtag!

Okay, there are times when you want to modify the text or value of a field before dumping it on the page. Sometimes, for whatever reason, you can't do it in a place other than just before rendering. Take, for example, the case of tags. Maybe you want to add a # in front of the tag text so that it looks like a #hashtag.

In this case, you want to use the template_preprocess_field function.

To use, add the following HTML to your theme's template.php file, changing THEME to your theme's name, FIELDNAME to the name of the field you want to modify, and the modification you want to do.

/**
 * template_preprocess_field()
 * https://api.drupal.org/api/drupal/modules%21field%21field.module/function/template_preprocess_field/7
 *
 * @param array $vars
 * @param string #hook
 */
function THEME_preprocess_field(&$vars, $hook) {
  $element = $vars['element'];
  if (isset($element['#field_name'])) {
    if ($element['#field_name'] == 'field_FIELDNAME') {
      foreach ($vars['element']['#items'] as $i => $e) {
        // this is where the manipulation actually happens.
        $vars['items'][$i]['#title'] = '#' . $vars['items'][$i]['#title'];
      }
    }
  }
}

Dry Bones

Book Notes

Walt Longmire, Book 11

Well, that didn't take me long to read. Being sick means you can sit around and read. I will admit, however, I would have liked to have listened to this book, as sometimes when sick even reading is difficult.

This particular Longmire mystery involves, HEY, A DEAD GUY. It also involves a dinosaur, which is nifty. We also find out in this book that there are 2483 people living in (the fictional) Absaroka County, Wyoming. Seriously, if there were that many murders in that small of a county that I was living in, I sure as hell would move away from that county. Of course, hell isn't really sure, so maybe I'd stay because I loved living there. Who knows.

This book was more of an action-packed conversation than a mystery. We have the dead body in the first few pages, and not so much of a hunt for the killer as a confusing twist of related actions that make sense in the end just sorta happen along the way. The dialogue is still great, I laughed a number of places, and was engaged throughout the book. This isn't the best Longmire book that Johnson has written, but it was entertaining enough to enjoy and keep reading the series.

So, if you're a Longmire fan, keep reading. If you're not, read one of the earlier books to see if you like them before reading this one.

Automate Front-end Performance (devObjective)

Here are my slides for Automating Front-end Performance with Grunt that I gave at devObjective Conf 2015. I'll likely write it up, to give a transcript. I think I have the only recording of the talk. I gave it while sick, having had a fever on the day of and the day before (and the day after, it turns out), so it wasn't the best talk I've ever given, but I did enjoy it. devObjective is a fun conference with very approachable people both running it and attending it.

Shock Wave

Book Notes

Virgil Flowers, Book 5

Once again, no intention of reading a book today, much less a book in its entirety, but travelling to devObjective today meant I had time on my hands, so I spent it reading (I know, shock). It helps to have the first seven books of the series in some variation of paper form. I have to say I'm rather glad I have a physical copy of these book instead of just digital. While I appreciate the digital format for convenience, I do so love the experience of reading books in physical form.

This Virgil Flowers book had a lot of death in it, though less than the previous one, and a lot of explosions. And, at the risk of giving away the ending, not a lot of sex. I found this last part to be somewhat of a relief, to be honest, since, well, Reacher had a different woman in every book and at what point do you just roll your eyes and think, "Really? Just how many STDs DO you have?"

Anyway, the book. The plot was great. I really liked the reveal in this one, as it had a number of clues but not enough that you the reader, nor Flowers the hero, realized what was going on until pretty much the end. Enough redirection that you knew something was up, but not enough to give up reading. The humour in this one was good, still with the quick wit, which I enjoyed. I like how Sandford is portraying the religious angst of a preacher's son who believes in a god that doesn't particularly care about the day-to-day of his believers, and really like a number of the biblical references included in the books. I've looked up a number of the quotes, reading them in the KJV translation, and thought about them. Completely fascinating, when they are taken 2000 years and who knows how many translations later.

Bad Blood

Book Notes

Virgil Flowers, Book 4

Okay, I hadn't actually been intending on reading any fiction book this weekend. Or at least, not starting any new books. That didn't go over very well, as I picked up the fourth Virgil Flowers book last night at 10pm and started reading. And kept reading. So much for finishing off books I've already started.

Again, I enjoyed this Flowers book. It was a bit different than the previous ones in that who did what was figured out pretty much in the middle of the book. Such a different take than most mystery books which have the bad guy revealed near the end, with only a small wrap-up after the reveal. No, Sandford has the Flowers books' mysteries figured out in the middle, maybe 2/3 of the way through, and lots of fumbling around, working to prove the case, wrapping up the details, and dealing with the aftermath of the arrest or solution.

It's a different twist on things. No idea how realistic it is in real life. Real life is mostly boring, with interesting things between. Digression, though.

I enjoyed the book, and will keep reading the Flowers books.

Pages