Skip to content

Instantly share code, notes, and snippets.

@thsutton
Created June 15, 2012 08:59
Show Gist options
  • Save thsutton/2935509 to your computer and use it in GitHub Desktop.
Save thsutton/2935509 to your computer and use it in GitHub Desktop.
Generate an empty Drupal module, with hooks.
mkmodule.sh
===========
This is a short script to create empty Drupal modules. Given a short name and
a list of hooks, it'll create a brand new module with placeholders of the
hooks you want.
sh mkmodule.sh example install uninstall requirements schema menu
#!/bin/sh
#
# Create the framework of a Drupal module.
#
set -e
MODULE="$1"
if echo $MODULE | egrep -vq "^[a-zA-Z0-9_]+$"; then
echo "Not valid"
exit -1
fi
shift
TIMESTAMP=$(date +"%Y-%m-%d")
AUTHOR=$(whoami)
mkdir $MODULE
MODFILE="${MODULE}/${MODULE}.module"
INSFILE="${MODULE}/${MODULE}.install"
cat <<EOF > "${MODULE}/${MODULE}.info"
name = "${MODULE}"
description = ""
version = "7.x-0.1"
core = 7.x
EOF
cat <<EOF > "${MODFILE}"
<?php
/**
* @file
* ${MODULE}.module - hooks and public code for ${MODULE} module.
*
* @created ${TIMESTAMP}
* @author $AUTHOR
*/
EOF
cat <<EOF > "${INSFILE}"
<?php
/**
* @file
* ${MODULE}.install - hooks and public code for ${MODULE} module.
*
* @created ${TIMESTAMP}
* @author ${AUTHOR}
*/
EOF
#######################################################################
## Hook implementations
#######################################################################
hook_disable () {
install_hook disable
}
hook_enable () {
install_hook enable
}
hook_install () {
install_hook install
}
hook_menu () {
HOOK="menu"
cat <<EOF >> "${MODFILE}"
/**
* Implements hook_${HOOK}().
*/
function ${MODULE}_${HOOK}() {
\$items = array();
\$items['example/path'] = array(
'page callback' => 'function_goes_here',
'page arguments' => array('an', 'example', 1),
'title' => "An example page",
'type' => MENU_NORMAL_ITEM,
);
return \$items;
}
EOF
}
hook_requirements () {
HOOK="requirements"
cat <<EOF >> "${INSFILE}"
/**
* Implements hook_${HOOK}().
*/
function ${MODULE}_${HOOK}(\$phase) {
switch (\$phase) {
case 'install' :
return array();
break;
case 'update' :
return array();
break;
case 'runtime' :
return array();
break;
}
}
EOF
}
hook_schema () {
HOOK="schema"
cat <<EOF >> "${INSFILE}"
/**
* Implements hook_${HOOK}().
*/
function ${MODULE}_${HOOK}() {
\$schema = array();
return \$schema;
}
EOF
}
hook_uninstall () {
install_hook uninstall
}
# Default install file hook implementation.
install_hook () {
HOOK="$1"
cat <<EOF >> "${INSFILE}"
/**
* Implements hook_${HOOK}().
*/
function ${MODULE}_${HOOK}() {
}
EOF
}
# Default module file hook implementation.
default_hook () {
HOOK="$1"
cat <<EOF >> "${MODFILE}"
/**
* Implements hook_${HOOK}().
*/
function ${MODULE}_${HOOK}() {
}
EOF
}
#######################################################################
## Add hooks
#######################################################################
for hook in $*; do
if type "hook_${hook}" &> /dev/null; then
eval "hook_${hook}"
else
default_hook $hook
fi
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment