Robert Kent

Personal blog of Robert Kent

PHP: Fix EXIF orientation in images

Sometimes when a user uploads an image to a website, usually a photograph, it can appear rotated. This can happen even if the image appears fine before uploading. Often this is due to the ‘orientation’ data in an image’s EXIF tag. This happens because modern cameras save metadata along with an image. This data can include: the time the picture … Read More

PHP: Add (th, st, nd, rd, th) to the end of a number

This function will take a number and add “th, st, nd, rd, th” after it.

As of PHP 5.3.0 there is built-in functionality for this using the NumberFormatter class.


PHP: Distance between two points

This snippet uses the latitude and longitude of two locations and calculates the distance between them. Results are given in both miles and metric units.

Example usage:


Drupal 7: Inserting text with the Form API

It can be helpful to insert content into form markup such as help text for users. Here’s how you can do this even if you didn’t build the form markup using hook_form_alter. In your template.php add the following:


Drupal 7: Creating a custom field formatter

A quick guide to how you can output your content fields any way you like without hacking theme files.

Drupal: Custom simple admin area

Often your custom module will output HTML. Instead of printing out hardcoded values, which would require a developer to update, in some cases it can be useful to provide an admin area to supply these values. This is a short example of how you can create a basic settings page in the Drupal admin area for your module. We will … Read More

Custom user permissions in Drupal 7

In Drupal we can use permissions to restrict what actions a certain user role can or cannot do. In a recent project I was tasked with implementing a webform that would only be accessible to users who: Had registered a site account Had completed a payment via Paypal For the first criteria you could simply use the core permissions to … Read More

Drupal: Send custom form submission by POST

Sometimes in addition to having a site form log submissions to the database and send emails we want to POST the data remotely. For example, sending a form submission to a CRM like SalesForce.

Drupal: Creating a View of files associated with the current node

In Drupal you can create a content type with a File field that allows multiple values. This means that when a new node is created you can upload several files to attach to the node. When this list is displayed on the front end it is shown as a list in the order the files are in on the edit … Read More

Drupal: Custom ordering of Views with drag and drop

Views in Drupal 7 are a great way of displaying lists of content such as news articles, blog posts or image galleries. By default Views comes with a lot of useful ways to sort these lists: alphabetically by title, chronologically by post date, e.t.c but sometimes there are cases where the order items are displayed in is arbitrary and a … Read More