-
Bulk Add URL Redirects in Drupal 7
Here’s a quick example showing how to add URL redirects (for the redirect module) in bulk.
Instead of a separate page for each list of “fellows”, we built a single view with exposed filters to choose a semester.
The original paths looked like “/fall-2000-fellows” and the new path looks like “/fellows/list?semester=5493”.
123456789101112131415161718192021222324252627282930313233343536// Semester will be passed as a query param. Redirect uses strings if you save// a redirect through the URL Redirects gui, so we'll use strings too.$redirects = array('former-fellows-year' => array('path' => 'fellows/list'),'fall-2010-fellows' => array('path' => 'fellows/list', 'semester' => '5433'),'fall-2011-fellows' => array('path' => 'fellows/list', 'semester' => '5411'),'fall-2012-fellows' => array('path' => 'fellows/list', 'semester' => '5408'),'fall-2013-fellows' => array('path' => 'fellows/list', 'semester' => '5405'),'fall-2014-fellows' => array('path' => 'fellows/list', 'semester' => '5403'),'fall-2015-fellows' => array('path' => 'fellows/list', 'semester' => '5401'),'spring-2010-fellows' => array('path' => 'fellows/list', 'semester' => '5434'),'spring-2011-fellows' => array('path' => 'fellows/list', 'semester' => '5410'),'spring-2012-fellows' => array('path' => 'fellows/list', 'semester' => '5409'),'spring-2013-fellows' => array('path' => 'fellows/list', 'semester' => '5406'),'spring-2014-fellows' => array('path' => 'fellows/list', 'semester' => '5404'),'spring-2015-fellows' => array('path' => 'fellows/list', 'semester' => '5402'),'spring-2016-fellows' => array('path' => 'fellows/list', 'semester' => '5400'),);foreach ($redirects as $src => $dest) {$redirect = new stdClass();$redirect_options = array();if ($dest['semester']) {$redirect_options = array('query' => array('semesters' => $dest['semester']));}module_invoke('redirect', 'object_prepare', $redirect,array('source' => $src,'source_options' => array(),'redirect' => $dest['path'],'redirect_options' => $redirect_options,'language' => LANGUAGE_NONE,));module_invoke('redirect', 'save', $redirect);} -
Random Mouse Distance in Keyboard Maestro
Here’s one way to choose a random item in a select list using just mouse movements and a random number. If you’re trying to do this in a browser there are cleaner ways to do it (javascript, chrome/safari actions, etc.), but for general usage across many applications this is a pretty universal solution.
I was surprised to find that you can type variables into the tiny coordinate text fields. They get bigger when you do.
When executed, this will select a random item (excluding “- None -“) from the select list.
-
Update Field Value for All Nodes
I recently added a “Show in Listing” boolean on/off checkbox field to a content type (default value is to be “checked”). There were 133 nodes when I created that field. I needed to update all of those nodes to be “checked”.
Here’s how I updated all of them using entity_metadata_wrapper(). It’s much quicker than any alternative I could think of (feeds, rules, etc.).
1234567891011$nids = db_query('SELECT nid FROM {node} WHERE type = :type', array(':type' => 'blogpost'))->fetchCol();foreach ($nids as $nid) {$node = node_load($nid);if ($node) {$wrapper = entity_metadata_wrapper('node', $node);if (isset($wrapper->field_islisted)) {$wrapper->field_islisted->set(1);$wrapper->save();}}}In this particular case I’m targeting a single content type. I could’ve used a similar query and some slight modifications to set the value on any node whose bundle (content type) supports that field_islisted field.
Lastly, if you have a lot of nodes, you might consider using node_load_multiple() instead of node_load() .
-
Adding a “Filter by year” Facet for a Date Field in Apachesolr for Drupal 7
A Drupal 7 site I’m working on makes use of the Apache Solr Search module and FacetAPI. Some of the content has a date field named field_s_date. I was tasked with providing a sidebar “Filter by year” facet block to filter the content by the year value of this date field. I’m pretty sure I could have done this a few different ways, but I settled on adding the year value as a separate field to the solr index and adding an associated facet using hook_facetapi_facet_info() .
First, to add the field, I used hook_apachesolr_index_document_build() in a custom module as follows:
-
Rendering an Image in Apache SOLR Search results (Drupal 7)
Introduction
There are a few ways to get extra information into the output of your Apache Solr Search results. The most efficient ways involve getting the information into the solr index and pulling it directly from the solr index. We do not want to introduce extra node_load() calls to render our search results. We do not want to have to use Views either.
So, using just apachesolr and some functions, I’m going to show how to get an image field into the search index and pull it back out in our result output. I know I’m not handling alt and title attributes in this example. I’ll let you figure that one out.
This is a step-by-step breakdown of my experience getting this to work. In this example I’m attempting to render an image field called field_forum_image for the forum (content type) nodes in my search results. You can use the same procedure for adding other fields, but please make sure you understand the solr prefixes (e.g., ss, ts, tm, etc.) before doing so. You can find these in the schema.xml file with in the apachesolr module.
-
Use Keyboard Maestro to Hide Instead of Close an Application
I use SnagIt to capture screenshots. The editor window that appears after you capture takes a few seconds to open if the application isn’t running. I realized that, while the capture tools remain open, the editor goes away when I hit ⌘W to close the window. Instead of using ⌘W to close the editor window, I sometimes remember to use ⌘H to hide the window, which hides the window but leaves the application open. The editor window pops open much quicker if the application is still open.
Using Keyboard Maestro I’m able to intercept the ⌘W that I do all the time (muscle memory), and turn that into a ⌘H.
-
Launch Center Pro actions for Day One
Here are my Launch Center Pro actions for the Day One app on iOS. I’m not here to teach you how to use Launch Center Pro (a huge topic), so if you need to learn that I suggest a lot of Googling and perhaps a visit to their official documentation and FAQs. Basically LCP will step you through prompts as it builds the output to move into a new Day One journal entry. Day One supports markdown, so you can make entries that look nice, like this:
Use this LCPEncoder to convert the templates you see below into URLs. After you generate the URL, get it into the “url” field of the LCP action.
-
New Drush Commands to Identify and Compress Large Images
https://github.com/agileadam/large-images-drush
I released a simple drush command file that provides two drush commands:
large-images-list
will list the largest images (by file size) found in a directory (and its subdirectories by default)large-images-compress
will compress (shrink file size of) images and update the associated record in Drupal’s file_managed table
-
Recursively Finding and Operating on the Largest n Number of Files of a Particular Type
If you want to reverse-engineer this, you can work from left to right, adding one command at a time to see what each step does to the output.
1du -ah . | grep -v "/$" | sort -rh | grep "jpg" | head -25 | cut -f 2 | while read -r line ; do ls -alh "$line"; doneThis example just lists the files we found, which is pointless given that’s what we already had before introducing the cut command.
How about a practical example?
Here’s how you can resize the 25 largest jpg files using mogrify (part of ImageMagick) to reduce the quality to 80:
1du -ah . | grep -v "/$" | sort -rh | grep "jpg" | head -25 | cut -f 2 | while read -r line ; do mogrify -quality 80 "$line"; done;What if you wanted to only grab files that are larger than a specific size? One way to do it is with the find command. For example, find all files greater than 8M:
1find . -type f -iname "*.jpg" -size +8M -exec du -ah {} \; | sort -rh | etc... -
Extracting Image “src” Attributes from an HTML String using PHP
Here are a few ways to build an array of image sources extracted from an HTML string. I’m sure there are other ways, but these seem to be reliable.
1234567$doc = new DOMDocument();@$doc->loadHTML($html);$tags = $doc->getElementsByTagName('img');$image_srcs = array();foreach ($tags as $tag) {$image_srcs[] = $tag->getAttribute('src');}1234567$images = array();preg_match_all('/<img[^>]+>/im', $html, $images);$image_srcs = array();foreach ($images[0] as $image) {preg_match('/src="([^"]*)"/im', $image, $matches);$image_srcs[] = $matches[1];}