Welcome to the Web Mechanic!

My name is Matthew McKinzie, and the Web Mechanic is my web development blog. The blog posts here cover challenges I've faced in web development as well as other interesting issues I've found. I hope you find something useful here.

If you are interested in anything you've read here or would like to hire a freelance web developer, feel free to contact me on my Contact page.

For code samples and a portfolio of sites I've built, please check out my bio page.

Credit to Michael Dziedzic and Alexander Sinn for header images.

Web Mechanic Blog

Setting the Target ID on a Taxonomy Reference Field in Drupal

Recently on a Drupal website, I was tasked with adding a taxonomy reference field to a content type. This taxonomy reference field replaced an existing select list. I was also tasked with writing a script to programmatically assign the value of the existing select field to the new taxonomy reference field.

But how do you programmatically set the value for a taxonomy reference field? It should be pretty straightforward and usually is. Most examples you find online use one of these two methods:

Sorting by Taxonomy Terms in Views

Have you ever had to sort a view of nodes by a referenced taxonomy term?  In our system, each staff member is entered as a separate node, containing the staff member's name, position and email address, and also containing the department the staff member works in.  The department is a taxonomy reference field.  We then have a view that pulls in each staff member's node, organized by department, and the departments are listed alphabetically.


Notes on Building a Drupal 8 Module, With a Little AngularJS

Note: This post was written 3/20/2016 and refers to a block on this site that displayed links to posts on my Drupal 7 site (webmech.biz). Since that Drupal 7 site has now been taken down, the block of Drupal 7 posts discussed below is no longer available. However, the discussion of how I used processed the data from webmech.biz and used AngularJS to display it may still be of interest.

The Magical (Dis)appearing $this Psuedo-Variable

At the office, we recently upgraded our staging server to PHP 7.  For the most part, this caused no problems at all and worked very well (and faster!).  But our event pages were failing, just giving us a white screen.  I looked in the "Recent log messages," and saw this:  "Error: Using $this when not in object context."  The code that triggered this error was a non-static class method that was being called statically from within a different class.  In PHP 5, this code only gives a "Deprecated" notice that non-static classes should not be called statically.


Drupal Redirect Paths May Conflict With Existing Directories

If you are familiar with the Drupal Redirect module, you know it can be a convenient way of creating redirects from URLs that don't really exist.  For example, you can create a redirect for yoursite.com/music to redirect to another website dedicated to your favorite music. 

But what if the URL you want to use as a redirect does already exist?  What if you want yoursite.com/testfolder to redirect to drupal.org?  It won't work--you will get an error message.   If you have already created a folder called testfolder, as in the image below, then your redirect won't work. 


PHP Timeout on Windows/IIS

Have you ever run a PHP utility script on a Windows/IIS platform and had it unexpectedly timeout? Your IIS settings for script timeouts might be conflicting with your PHP settings.

Let's say your script is querying a remote, third-party web service for share data on 3000 articles on your site, and for whatever reason, the web service only allows you to query share data for one URL at a time. This could take a while. So on your Windows/IIS server, you set up a scheduled task to run at 1:00 am to retrieve all this data and put in your database for convenient access.


A State Machine With an Unknown Outcome

Recently, I was tasked with creating an event registration process for an existing Drupal site. This project would move the user through a number of possible paths to the final outcome of registering (or not registering), so building it as a finite state machine (using the state design pattern) seemed a good fit and would provide a flexible, easily modified, object-oriented structure for event registration.

Drupal tightens up node_load()

The other day we had an error on our Drupal 7 site where users would receive error messages whenever they tried to register for a conference. The site would register them, but the only thing the user would see is a JavaScript dialog box with an AJAX callback error message with some code in it; since the users didn't know that the site had registered them, they kept clicking the register button, getting registered multiple times. Not a desirable state of affairs.