code snippets

Drupal Theme example

Just a quick example for Drupal template registration:

Declared in the .module

Template Tracers

This code for template tracers should be used with caution. They do expose where files live (less secure).
Opening tracer:

<!-- <template> <?=str_replace('var/www/',  '' , __FILE__  );?>  -->

Closing tracer:

<!-- </template> <?=str_replace('var/www/',  '' , __FILE__  );?>  -->

XML load 2-step

I had a module where I was previously loading and processing an xml file in one step using

simple_xml_load_file (URL of xml file)

 

Image Style URL

If you need to get the URL for an image file with a drupal style applied, this function is what you need to get the path for the image styles:

image_style_url($style_name, $path)

Parameters

$style_name: The name of the style to be used with this image.

$path: The path to the image. Path will look like "public://mypicture.jpg"

Return value

The absolute URL where a style image can be downloaded, suitable for use in an <img> tag. Requesting the URL will cause the image to be created.

Safe PHP foreach

A foreach thows non-fatal warnings if you pass it a non-existent array or a string or integer. It does not mind, if you toss it an empty array. So I use the following ternary logic within the declaration of the foreach. If $aResults is an array OR an object, then it uses that in the foreach ELSE pass it an empty array. This makes it fail without throwing any errors.

Changing the Date Format for the jQuery Datepicker

drupal_add_js('jQuery(document).ready(function(){
                jQuery( "#mmg-main-menu-events-start-eventmenu, #mmg-main-menu-events-end-eventmenu" ).datepicker({
                                      dateFormat: "D dd M yy"

Sort an array of objects

Here is a quick php bit of code to sort an array of objects.

Javascript GET

A quick non-library way to safely look for a GET value and do something with it.

Drupal: Check to see if a file or directory exists

Sometimes you want to see if a file or directory in the managed file system exists and base your logic off its existence.
Example: wanting to know if the directory /sites/web-dev.swirt.us/files/jqueryui_theme/redmond/ exists before including it
Use is_dir() http://php.net/manual/en/function.is-dir.php
The trick is to add the public:// stream wrapper

codit_sanitize_boolean() - Sanitize Boolean Values and control their format

This is a handy function to use to be able to pass it a variety of values and have the function return either of three flavors of boolean values.

Pages

Subscribe to RSS - code snippets