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
This function will take a number and add “th, st, nd, rd, th” after it.
1 2 3 4 5 6 7 8 9 10 11 12 |
function ordinal($cdnl){ $test_c = abs($cdnl) % 10; $ext = ((abs($cdnl) %100 < 21 && abs($cdnl) %100 > 4) ? 'th' : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th')); return $cdnl.$ext; } // Test for ($i=1; $i<100; $i++){ echo ordinal($i).'<br>'; } |
As of PHP 5.3.0 there is built-in functionality for this using the NumberFormatter class.
1 2 3 |
$locale = 'en_US'; $nf = new NumberFormatter($locale, NumberFormatter::ORDINAL); echo $nf->format($number); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) { $theta = $longitude1 - $longitude2; $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); $miles = acos($miles); $miles = rad2deg($miles); $miles = $miles * 60 * 1.1515; $feet = $miles * 5280; $yards = $feet / 3; $kilometers = $miles * 1.609344; $meters = $kilometers * 1000; return compact('miles', 'feet', 'yards', 'kilometers', 'meters'); } |
Example usage:
1 2 3 4 5 6 7 8 |
$point1 = ['lat' => 40.770623, 'long' => -73.964367]; $point2 = ['lat' => 40.758224, 'long' => -73.917404]; $distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); foreach ($distance as $unit => $value) { echo $unit . ': ' . number_format($value,4) . '<br />'; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function mytheme_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'my_form') { // Add new form item. $form['help'] = [ '#markup' => "<p class='help'>Generic help message here.</p>" ]; // this value determines the position of the new element. $insert_index = 0; $element_to_insert = []; $element_to_insert['filter-help'] = [ 'value' => 'help', ]; $form_info_top = array_slice($form['#info'], 0, $insert_index); $form_info_bottom = array_slice($form['#info'], $insert_index); $new_form_info = $form_info_top + $element_to_insert + $form_info_bottom; $form['#info'] = $new_form_info; } } |
A quick guide to how you can output your content fields any way you like without hacking theme files.
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
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
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.
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
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