Archive for the ‘Drupal’ Category

h1

Put your Content Types with CCK fields into code [Drupal]

April 18, 2011

Putting a content type into code is a good thing to do since it then follows your module and means you don’t have to recreate content types on each instance of the site (e.g. dev, test and live)

To create basic content types in code, you can use the following code pattern in your mymodule.install file:

function mymodule_install() {
_mymodule_install_content_types();
}

function _mymodule_install_content_types() {
// Create Video Content type
$video_node_type = array(
'type' => 'vzaar_video_page',
'name' => t('Vzaar Video Page'),
'module' => 'node',
'description' => t('A Page containing a video stored on Vzaar'),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
);
node_type_save((object)_node_type_set_defaults($video_node_type));
}

Now on installation of the module, mymodule, you will get a new content type called Vzaar Video Page. This only has the Title and Body fields though so isn’t very useful. Using CCK we can add new fields in the interface but how do we get those fields out into code?

One way is to use the content_fields function to get an instance of a field as an array. You can then use that array in your install file to recreate the content. However, running this function is hardwork becaue you’ll need to run it in drupal then copy and paste the data into your file. An alternative is to use Drush, which is a full drupal environment on the command line to execute the function for you, then redirect the output into you install file.

drush ev "var_export( content_fields( 'field_myfield_name' ) );" >> mymodule.install

Now edit mymodule install and you’ll see the array at the end of the file. Move it into your install function and wrap it in the function content_field_instance_create. To test it works, make an update function as well in your install file, delete the content type from your drupal and then run update.php (or drush updb)

You’re final install function might look something like this …

/**
* Implementation of hook_update_N
*
* Add Vzaar Video content type
*/
function mymodule_update_6201() {
_mymodule_install_content_types();
}

function mymodule_install() {
_mymodule_install_content_types();
}

function _mymodule_install_content_types() {
// Create Video Content type
$video_node_type = array(
'type' => 'vzaar_video_page',
'name' => t('Vzaar Video Page'),
'module' => 'node',
'description' => t('A Page containing a video stored on Vzaar'),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
);

node_type_save((object)_node_type_set_defaults($video_node_type));

// Create CCK field for media_vzaar
$vzaar_media_field = array (
'field_name' => 'field_vzaar_video_url',
'type_name' => 'vzaar_video_page',
'display_settings' =>
array (
'label' =>
array (
'format' => 'above',
'exclude' => 0,
),
'teaser' =>
array (
'format' => 'default',
'exclude' => 0,
),
'full' =>
array (
'format' => 'default',
'exclude' => 0,
),
4 =>
array (
'format' => 'default',
'exclude' => 0,
),
),
'widget_active' => '1',
'type' => 'emvideo',
'required' => '0',
'multiple' => '0',
'db_storage' => '1',
'module' => 'emvideo',
'active' => '1',
'locked' => '0',
'columns' =>
array (
'embed' =>
array (
'type' => 'text',
'size' => 'big',
'not null' => false,
'sortable' => true,
),
'value' =>
array (
'type' => 'varchar',
'length' => 255,
'not null' => false,
'sortable' => true,
),
'provider' =>
array (
'type' => 'varchar',
'length' => 255,
'not null' => false,
'sortable' => true,
),
'data' =>
array (
'type' => 'text',
'size' => 'big',
'not null' => false,
'sortable' => false,
),
'status' =>
array (
'description' => 'The availability status of this media.',
'type' => 'int',
'unsigned' => 'TRUE',
'not null' => true,
'default' => 1,
),
'version' =>
array (
'description' => 'The version of the provider\'s data.',
'type' => 'int',
'unsigned' => true,
'not null' => true,
'default' => 0,
),
'title' =>
array (
'type' => 'varchar',
'length' => 255,
'not null' => false,
'sortable' => true,
),
'description' =>
array (
'type' => 'varchar',
'length' => 255,
'not null' => false,
'sortable' => true,
),
'duration' =>
array (
'description' => 'Store the duration of a video in seconds.',
'type' => 'int',
'unsigned' => true,
'not null' => true,
'default' => 0,
),
),
'widget' =>
array (
'video_width' => '425',
'video_height' => '350',
'video_autoplay' => 0,
'preview_width' => '425',
'preview_height' => '350',
'preview_autoplay' => 0,
'thumbnail_width' => '120',
'thumbnail_height' => '90',
'thumbnail_default_path' => '',
'thumbnail_link_title' => 'See video',
'meta_fields' =>
array (
'title' => 0,
'description' => 0,
),
'providers' =>
array (
'vzaar' => 'vzaar',
),
'default_value' =>
array (
0 =>
array (
'embed' => '',
'value' => '',
),
),
'default_value_php' => NULL,
'label' => 'Vzaar Video URL',
'weight' => '28',
'description' => '',
'type' => 'emvideo_textfields',
'module' => 'emvideo',
),
);

// Add the new content type to drupal
content_field_instance_create($vzaar_media_field);

}

Advertisements
h1

Rename files in directory with some regex magiks

April 12, 2011

When using drupal I often want to clone a module as a starting point for a new module. I then have to rename all the files in the directory to the new structure. This can be painful if there is more than one file. All files in drupal follow the file naming convention of :
modulename.type
So the following bash script will niftily go through a directory renaming all the files with module1 and replace with module2…


for f in *; do mv $f `echo $f | sed 's/module1\(.*\)/module2\1/g'`; done

h1

Building a Drupal installation profile

February 22, 2011

Setting up a new Drupal site can be a chore, especially if you need to do it a lot when doing testing or trying out new features to see if they are what you need without having to do it in a live environment or messing up existing dev environments.

There are a couple of drupal tools that can help, drush and drush_make. As someone who enjoys working on the command line, drush is a superb tool and can just be used on its own to speed up the development of building drupal sites. Drush make adds an additional make command to drush which will take an installation profile file that you’ve previously crafted and build you a drupal site out of it.

Below is both an example installation profile and a bash script I created to quickly roll out a standard drupal site with simpletest and admin_menu built in for Drupal 6 which most of our clients are still using. Assuming you’ve setup drush and drush_make If the files simpletest.make and simpletest.modsenabled exist in your web root (e.g. /var/www) then you can run the drush_make.sh script as shown below and a new site will be created at /var/www/simpletest. If you edit the drush_make.sh script to include your mysql database credentials then the script will setup the database for you as well.

./drush_make.sh simpletest

Installation profile simpletest.make

; Get Drupal 6
core = 6.x
api = 2
projects[drupal][type] = "core"
; Simpletest patch - note I'm hosting this locally, its in the simpletest zip from drupal.org/projects/simpletest
projects[drupal][patch][]="http://localhost/D6-core-simpletest.patch"
; Fix for PHP4 deprciated warnings in update/fetch.inc
projects[drupal][patch][]="http://drupal.org/files/issues/555362-1.update_parse_files.D6.patch"
; Drupal contributed modules
projects[admin_menu][type] = "module"
projects[admin_menu][version] = 1.5
projects[devel][type] = "module"
projects[devel][version] = 1.23
projects[simpletest][type] = "module"
projects[simpletest][version] = 2.11

List of modules which are enabled in new drupal site: simpletest.modsenabled


admin_menu,menu,user,devel,simpletest

drush_make.sh script

#!/bin/bash
if [ ! -n "$1" ]; then
echo "You need to provide a name which matches the name of the make file"
exit 1
fi
if [ ! -f $1.make ]; then
echo "Could not find the make file $1.make"
exit 1
fi
if [ ! -f $1.modsenabled ]; then
echo "Could not find the file $1.modsenabled";
exit 1
fi
modslist=`cat $1.modsenabled`
echo About to install $1.make
# extract the site from the make file
if [ ! -n "$2" ]; then
drush make $1.make $1
else
echo skipping because of $2
fi
# go into the new dir
if [ -d $1 ]; then
echo "Directory exists"
cd $1
else
echo "Directory does not exists"
exit 1
fi
# make the new db schema
echo DROP SCHEMA IF EXISTS $1\; CREATE SCHEMA $1\; | mysql -u root -p
# setup the new site - put your db password in here
drush site-install --db-url=mysqli://root:password@localhost/$1 --site-name=$1 -y
# Enable the modules
drush en -y $modslist
exit 0

h1

Drupal Drush: Changing the output of pm-list into something which can go into an installation profile

February 22, 2011

I like the command line and so drush is a great tool I found when building sites in drupal: http://drush.ws

It has a plugin called drush_make which can be used with an installation profile to automatically download and install a drupal and associated modules

The following command passed through sed will get all the non-core modules from a drupal installation and their version numbers and output it formatted in the way it can go into a drush make installation profile file.

drush pm-list --no-core --status=enabled | sed -r 's/(.{35})(.{63})Module 6.x-(.*)/\2|\3/g' | sed -r 's/[^(]*\(([^)]*)[^|]*\|([^ ]*)/projects[\1][version] = \2/g'