Subscribe to Syndicate

Drupal Developer and Systems Administrator

Support for your Drupal Website

Code snippets

paulbooker's picture

Create a custom template file for a custom block in drupal

//implementation of hook_block_info
function mymodule_block_info() {
  $blocks = array();
  $blocks['myblock'] = array(
    'info' => t('My Block Title'),
  );

  return $blocks;
}

//implementation of hook_block_view
function mymodule_block_view($delta='') {
  $block = array();

  switch($delta) {
    case 'myblock' :
      $variables = array(); 
      $block['content'] = theme('mytemplate', $variables);
      break;
  }
  return $block;
}


//implementation of hook_theme
function mymodule_theme() {
  return array(
    'mytemplate' => array(
paulbooker's picture

Updating a user role when a class (group) is flagged as finished

function mymodule_training_class_node_form_submit($form, &$form_state) {

  if ($form_state['input']['field_class_is_finished']['und'] == 1) {
    $nid = $form_state['values']['nid'];  
		
    $query = db_select('og_membership', 'ogm')
      ->condition('ogm.gid', $nid, '=')
      ->fields('ogm', array('etid'));				
    $result = $query->execute();
		
    foreach ($result as $record) {
      $uid = $record->etid;
      _mymodule_training_class_assign_alumni_role($uid);
    }	
  }
}

function _mymodule_training_class_assign_alumni_role($uid){
paulbooker's picture

Updating tables and columns belonging to a custom field

/**
  * Returns all fields created on the system of the type defined in mymodule.
  */
function mymodule_get_mymodule_fields() {
  $types = array_keys(mymodule_field_info()); // field types defined in mymodule
  $fields = array();
  foreach (field_info_fields() as $field) {
    if (in_array($field['type'], $types)) {
      $fields[] = $field;
    }
  }
  return $fields;
}
/**
 * Convert column(s) to double precision.
 */
function mymodule_update_7101() {
  $fields = mymodule_get_mymodule_fields();
  foreach ($fields as $field) {
paulbooker's picture

Implementation of hook_field_schema().

/**
 * Implements hook_field_schema().
 */
function mymodule_field_schema($field) {
  return array(
    'columns' => array(
      'fid' => array(
        'description' => 'The {file_managed}.fid being referenced in this field.',
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
      ),
      'title' => array(
        'description' => "Photo title text",
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
      ),
    ),
    'indexes' => array(
      'fid' => array('fid'),
    ),
paulbooker's picture

Update Script Using Batch API: Implementation hook_update_N()

/**
 * Implements hook_update_N().
 * Removes aliases for content types that do not need them.
 */
function mymodule_update_7002(&$sandbox) {
  // Set default patterns to empty so that aliases are not generated for every
  // node. You will need to set the pathauto pattern for every content type that
  // you do want to have a generated alias.
  variable_set('pathauto_node_pattern', '');
  // Remove existing aliases for node types: marquee, news_feed
  $types = array(
    'marquee',
    'news_feed',
  );
 
  if (!isset($sandbox['max'])) {

Pages

Shell command snippets

curl -X POST -i -H "Content-type: application/json" -c cookies.txt -X POST http://example.com/service/user/login -d '
    {
        "username":"username",
        "password":"password"
    }
    '
curl -i -H "Content-type: application/json" -b cookies.txt -X POST http://example.com/service/comment -d '
    {
        "nid":"123",
        "subject":"This a test comment",
        "comment_body": { "und": [ { "value": "This a test comment, please ignore" } ] }
    }
    '
curl -i -H "Content-Type: application/json" -b cookies.txt -X POST http://example.com/service/user/logout
mkdir ~/.drush
vim ~/.drush/aliases.drushrc.php
// local alias
$local_sites = '/users/paulbooker/Sites/';
$aliases[demo.local'] = array(
  'root' => $local_sites . 'demo',
  'path-aliases' => array(
    '%dump-dir' => $local_sites . 'drush.dbdumps',
    '%files' => $local_sites . 'demo/sites/default/files'
  )
);
// remote alias
$remote_sites = '/var/www/sites/';
$aliases['demo.dev'] = array(
  'remote-host' => 'www.somewhere.com',
  'remote-user' => 'paulbooker',
  'root' => $remote_sites . demo.developmentbranch.com',
  'path-aliases' => array(
    '%dump-dir' => '/home/paulbooker/drush.dbdumps',
    '%files' => $remote_sites . demo.developmentbranch.com/sites/default/files'
  )
);
drush sql-sync @demo.dev @demo.local
$drush rsync @demo.dev:%files/ @demo.local:%files
drush make patches.make
drush make --no-core --projects=rules patches.make
aptitude install php-pear
pear upgrade --force pear
pear install Console_GetoptPlus
pear install Console_Table
pear channel-discover pear.drush.org
pear install drush/drush
sudo mkdir /home/backup
sudo chmod 700 /home/backup
#  ~/.drush/aliases.drushrc.php
$aliases['live'] = array(
  'uri'  => 'http://example.com',
  'root' => '/home/example.com/www',
);
#!/bin/sh

# Daily backup of the database, using Drush
# /usr/local/bin/daily-backup.sh
#

# Backup directory
DIR_BACKUP=/home/backup

log_msg() {
  # Log to syslog
  logger -t `basename $0` "$*"

  # Echo to stdout
  LOG_TS=`date +'%H:%M:%S'`
  echo "$LOG_TS - $*"
}

DAY_OF_WEEK=`date '+%a'`
BACKUP_FILE=$DIR_BACKUP/backup.$DAY_OF_WEEK.tgz

log_msg "Backing up files and database to $BACKUP_FILE ..."

drush @live archive-dump \
  --destination=$BACKUP_FILE \
  --preserve-symlinks \
  --overwrite 

RC=$?

if [ "$RC" = 0 ]; then
  log_msg "Backup completed successfully ..."
else
  log_msg "Backup exited with return code: $RC"
fi
chmod 755 /usr/local/bin/daily-backup.sh

.. add the script to cron..

30 2 * * * /usr/local/bin/daily-backup.sh
ls /home/backup/

scp or rsync backup files to another server

git clone --branch 7.x-1.x http://git.drupal.org/project/myproject.git
git checkout -b 12345678-myproject-needs-fixing .
git format-patch 7.x-1.x --stdout > myproject-needs-fixing-12345678-1.patch .

Pages

Subscribe to Drupal Developer and Systems Administrator RSS