Drupal’s API method taxonomy_get_term_by_name() chokes and fails to return the taxonomy term when the term name has encoded characters. The trouble is that term names are usually encoded by the time you pull them from the $variables array in a hook. This means that a taxonomy term with an ampersand would not get looked up properly, since the ampersand is encoded as “&” So doing a lookup of a taxonomy term “Help & Resources” actually ends up feeding “Help & Resources” to the method.
The solution is to sanitize the term name before the lookup, like so:
$term_name_sanitized = str_replace('&', '&', $variables['title']);
$term_by_name = taxonomy_get_term_by_name($term_name_sanitized, 'article_category');
where article_category is the vocabulary name, and $vars[‘title’] is the taxonomy name (accessed from the variables array in a hook).
This is not a common situation, but occasionally you may find yourself having force-committed a bunch of things (usually a bad sign to begin with, but that’s a whole ’nother post) and a few files that should be ignored sneak into your repo. You might have dozens of commits from that point on and not be aware of it, because your .gitignore file dutifully doesn’t track those files anyway.
So: how to remove those files from the Git cache? My research led me to rewriting commits, which is usually filed in the “Contents Dangerous. Open With Caution.” section of Git. Here’s some links:
So here was my process:
- Remove the files from the Git cache. I used an rm -rf command because I was getting rid of directories. For example:
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch <path to file or directory, from root level of your repo>' HEAD
- Prune Git. For example:
rm -rf .git/refs/original/ && git reflog expire --all && git gc --aggressive --prune
- Force-push your edits, otherwise they will be rejected because you’ve rewritten history. (Couldn’t figure this part out until I read Thomas Hunter’s post linked above.)
git push origin master --force
If you’ve got a bunch of files, I would suggest atnan’s gist linked above, or David Underhill’s script (second link). David’s script also prunes Git (my understanding is you can still have file references hanging around that need to be garbage-collected).
Ideally you would never end up in this situation, and hopefully you catch this before you’ve pushed changes to a remote and others have pulled the dirty repo down. In my case there didn’t seem to be ill effects introduced by rewriting commits and pushing to a remote, but it’s something I’d like to avoid as much as possible.
My morning usually goes something like this: I wake at 6:15am (ish). I read, pray, and then use the bathroom (which I try to do as silently as possible, because if I don’t wake the kids there is the slenderest chance that I might crawl back in bed next to my wife).
But on this particular morning Sophie—my two-year-old—is already up. I pick her up and change her. Before I even set her down on the floor, her legs are already churning: a wind-up toy awaiting release. When I ask her to grab a book, however, she stops running immediately. She chooses one, crawls up onto the couch, and offers it to me. “This one?” she requests.
Today it’s one of her Olivia books, one we’ve read hundreds of times. I start turning the pages. I make it about two pages in before she wrests the book away, saying, “I read a you”.
That’s…new. Sometimes she wants to read by herself, but this is the first time she’s asked to read to me. I fold her in my arms and gently rest my chin on her head while she reads. She mimics my vocal inflections from past readings. She can’t actually read, of course. But she’s memorized the book’s rhythms so well that she doesn’t miss a detail.
And then she’s done, scooting off the couch to get on with her day. I close my eyes for a minute, balancing on the razor-thin line between wakefulness and sleep. Sometimes a life unfolds so gently, you could almost mistake it for a dream.