Create a simple timer object


/**
 * A Timer object.
 */
function Timer() {
	
	var time = 0;
	$('#timer').html(time);
	
	var timer = window.setInterval(function() {
      time += 1;
      $('#timer').html(time);
	}, 1000);
	
	this.stop = function() {
      clearInterval(timer);
      $('#timer').html(time);
	}
	
}

Alter user passwords


	require_once 'includes/password.inc';
	$uids = db_select('users', 'u')
	->fields('u', array('uid'))
	->execute()->fetchCol();
	
	foreach($uids as $uid) {
		$user = null;
		if ($uid != 0) {
			$user = user_load($uid);
			$user->pass = user_hash_password('test');
			user_save($user);
		}
	}

HTML5 Geolocation - Locate a user's position


// Does the browser support geolocation?
// @see http://www.w3schools.com/html/html5_geolocation.asp.
if(navigator.geolocation) {
	// When clicked on deny, error function is triggered.
	navigator.geolocation.getCurrentPosition(success,error);
} else {
	console.log('Browser doesn\'t support geolocation, default geolocation unsupported action.');
}

function success(pos) {
	console.log('User allows, location specific action.');
}
function error(error) {
	console.log('User denies, default geolocation supported action.');
}

A class used to read CSV files


/**
 * CsvProcessor.
 * 
 * @author Dribbit info@dribbit.eu
 *
 */
class CsvProcessor {

  /**
   * Process csv files and return as an associated or indexed array
   * depending if headers are set.
   * 
   * @param $file
   *  String File path.
   * @param $length
   * Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). 
   * By setting to 0, the maximum line length is not limited, which is slightly slower. 
   * @param $delimeter
   *  String containing the CSV delimeter.

A class used to read textfiles


/**
 * TextProcessor.
 * 
 * @author Dribbit info@dribbit.eu
 *
 */
class TextProcessor {

  /**
   * Process textfiles and return as an associated array.
   * 
   * @param $file
   *  String File path.
   * @param $length
   * Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), 
   * or on EOF (whichever comes first). If no length is specified, 
   * it will keep reading from the stream until it reaches the end of the line. 
   *  
   * @return array
   * 
   */

Export all user mail addresses to CSV



/**
 * Implements hook_menu().
 */
function export_mails_menu() {
  return array(
    'export_mails.csv' => array(
      'page callback' => '_export_mails',
      'access callback' => 'user_is_logged_in',
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function export_mails_block_info() {

  $blocks['export_mails'] = array(
    'info' => t('Export mails'),
  );

  return $blocks;

}

/**
 * Implements hook_block_view().
 */
function export_mails_block_view($delta) {
  switch ($delta) {
    case 'export_mails' :
      return array(

How to render an item list

To use the theme_item_list function, a $variables array needs to be declared which contains an items array, a title which can be empty, a type element (ul or ol) and an attributes array. When a title is passed, a h3 element is added to the HTML markup.

Create a content type programatically Drupal 8

​Basic creation of a content type in Drupal 8. Content type is created in code in a custom module called dribbit_create_content_type.

module_directory_node.png

As seen on the left, an install directory located in the config directory is needed to store the content types yaml file. Human readable data is used to preconfigure the content types settings.

Pages

Subscribe to Dribbit.eu RSS