Bostonbuilt.org - The built in Boston initiative

Earlier today we launched Boston Built in collaboration with our friends at Bocoup, UpStatement, and SignedOn

Basically, Boston built allows you to “represent” by either adding a graphic logo or a 1x1 tracking pixel to your site via Javascript. Then, the bostonbuilt.org site will pick you up and list your favicon along side the other sites that are including the tracking code.

BostInnovation has a nice write up and a poll at The ‘Built in Boston’ initiative.

Happy Friday!

TrackYourImpact.com Launched!

Recently we’ve launched a new site for a client called Purpose Beverages: http://trackyourimpact.com . We’ve received great feedback from users so far. The site uses a wide range of technologies. It is built on http://symfony-project.org">symfony and uses the http://www.apostrophenow.com/">Apostrophe CMS to manage the main parts of the site. It integrates with a SMS provider to allow you text into it to find out more about your purchase.

Tēvolution is a new brand of tea on the market that does good with each purchase. Every time someone buys it they done a specific amount(for example 25 cents) to a specific charity. In order to find out how large of a donation and what charity your bottle goes to you can actually text the code found on the bottle to the website, or you can login in on your phone browser or regular browser and enter the code. You will find out how much and to whom you just donated money to!

Right now Tēvolution is just coming to the market so keep your eyes peeled for it! It’s a great product that does good!

jQuery UI confirm

I was looking around earlier for a jQuery plugin to allow me to use jQuery UI’s dialog() widget to popup a confirm dialog. Didn’t have any luck finding one so I whipped something up. It’s not pretty but it works. You could even go as far as to overload window.confirm() but thats probably a bad idea.

jQuery.confirm = function(options){
	
	var opts = jQuery.extend( { message: "", ok: function(){}, cancel: function(){ } }, options );
	
	jQuery("<div class='span-10'><div class='ui-confirm-message'>" 
				+ opts.message + "<img class='loader' style='padding-left: 10px' src='/images/loader.gif' />" 
				+ "</div></div>").dialog({
					autoOpen: true,
					modal: true,
					autoOpen: false,
					resizable: false,
					draggable: false,
					title: "",
					width: "400px",
					buttons: {
					    "Cancel": function(){ 
							opts.cancel.call( this ); 
						},
						"Ok": function(){ 
					    	opts.ok.call( this );
					    }
					}
	}).dialog("open");
	
	
};

// use it 

		jQuery.confirm( {
			"message": "Are you sure?",
			"ok": function( ){
				
				$(this).find(".loader:first").show();
				
				// do stuff
                               $(this).dialog("close");
			},
			"cancel": function( ){ 
				$(this).dialog("close");
			}
		});

Drupal 7: Batch insert nodes with Drush

Well D7 has been out for a little while now and we finally got a chance to use it on a site this week.

Anyway, this site is one of the heavier Drupal sites we’ve done and it involved loading ~200+ nodes of data just to set things up. This presented two problems, how to batch load data and then how to load custom content types with several custom fields.

The Drupal module documentation has example code for adding a node with drupal_exeucte here but it doesn’t deal with how to set custom fields on your content type. On top of this, drupal_execute has been renamed to drupal_form_submit in Drupal 7 and the function signature has changed a bit.

Anyway, I dug around a bit and finally managed to get this working. You’ll obviously need Drush installed for the following code to work but you could rip it out and use it outside a Drush command. I was looking to basically replicate the “load-data” task from Symfony so that I could seed my Drupal database with Nodes at any point so I chose to make this a Drush command.

Here’s what you need:

  • You’ll need a module to hold the Drush task. I used Module Builder to generate my scaffolding.
  • Create a file named [modulename].drush.inc in your module directory
  • Here is the code I’m using for [modulename].drush.inc Replace “cm” with the name of your module:
<?php

/**
* Implementation of hook_drush_command().
*/

function cm_drush_command() {

        // callback is the function that will be called when the command is executed
	$items['load-rep-data'] = array(
	    'callback' => 'cm_load_rep_data',
            'description' => 'Loads the representative data.',
            'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
	);
	
	return $items;
}

function cm_load_rep_data( ){
   // you need this to autoload the functions to create nodes
   module_load_include('inc', 'node', 'node.pages');

  // do some business logic to load your data from where it is
  foreach( $arr as $res ){
    
    // define the custom node type
    $node = array('type' => 'representative');
   
   // set up the form array
   $form_state = array();

   // set the title of the node   
   $form_state['values']['title'] = $res["title"];

   // set a custom field that is a text type
   $form_state['values']['field_first_name']['und']['0']['value'] = $res["first_name"];

   // set a long text field and enable full_html - NOTE you'll need to allow anonymous users to use this for Drush to work
   $form_state['values']['body']['und']['0']['format'] = 'full_html';
   $form_state['values']['body']['und']['0']['value'] = $res["bio"];

  // set some custom select fields
  $form_state['values']['field_house_committees']['und'][ 0 ] = 34;
  $form_state['values']['field_house_committees']['und'][ 0 ] = 37;

  // can't leave this out or the form wont save
  $form_state['values']['op'] = t('Save');

  // actually try and "submit" the form
  drupal_form_submit('representative_node_form', $form_state, (object)$node);
  
  // printing from Drush is easy 
  drush_print( $res["title"] );
  }  
}

Thats about it.

With Firebug, it’s really easy to see the field names and the values that you can set by just looking at a form to create whatever type of node you want.

drupal_form_submit can also be used to “submit” any other type of form in Drupal.

An open question is how to “fill out” an ImageField field via the command line since nothing is actually going to be uploaded.