Robert Kent


Personal blog of Robert Kent

Drupal 7: Adding autocomplete to search fields

In many cases when you have a search field it can be useful to provide a list of suggestions to the user as they type. This small interaction provides several benefits to the user: It affirms to the user that the site search is working It provides potential searches an alternatives to the user before they even complete a search … Read More


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: Keep destination parameter between user login and register

Let’s say you have an area of your website that should only be accessible to registered users. If somebody tries to access this area without being logged in we could either show them the standard ‘Access denied’ page or we could redirect them to log in or create an account first then bring them back. Drupal allows us to append … 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.