technonaturalist

image link to hive image link to ko-fi

drupal 7

Drupal 7 notes - Apache config

/etc/apache2/apache2.conf - find the directory bits and make sure the /var/www one looks like:

<Directory /var/www/> 
  Options Indexes FollowSymLinks 
  AllowOverride All 
  Require all granted 
</Directory>

sudo a2enmod rewrite

sudo a2enmod headers

Make sure all the site configuration files in /etc/apache2/sites-available have .conf on the end (especially if upgrading from previous Apache and it doesn’t seem to be backwards compatible despite what the Linode library says).

Don’t forget to sudo a2ensite all-the-sites.conf

Drupal 7 notes - making javascript work in a modal

Or more accurately in my case, making a jquery gallery work inside a modal. I am really bad at javascript so I have no idea if my hackjob is a good way to do anything but it did what I wanted.

What we needed in a recent project was for a gallery view with a thumbnail slider to launch from a gallery index comprising of thumbnails made up fo the first image in a multiupload image field. We used Gallery Formatter (the only one that would show up in the Colorbox Node at all).

Drupal 7 install profile stuff that everyone else magically knows

Seeing as I found little to nothing when searching for how to configure individual role permissions, editor settings and enabling contrib filters with an install profile, I made something on the off-chance it helps some other poor sap like me who doesn’t automagically know where to look for these things.

Enabling filters provided by contrib modules

Search in the .module files for _filter_info. Some modules use the same format as Drupal filters of $filters['modulename_filter'], others don’t. Typogrify doesn’t:

Drupal 7 notes: node or page template by path

Because I always seem to manage to completely forget how to do something this basic.

template.php in theme_preprocess_page(&$variables) or theme_preprocess_node(&$variables)

<?php
  $path = drupal_get_path_alias();

  if (strpos($path, 'part_of_url') !== false) {
    $variables['theme_hook_suggestion'] = 'page__whatever'; // or node__whatever if using in a node preprocess
  }
?>

Drupal 7 upgrade from HELL

Modded to be slightly more useful as I wrote the other one while slightly delirious.

  • upgrade to latest D6
  • drush arb default --destination="wherever/you/like"
  • mysql update systemsetstatus='0' where filename like 'sites%'
  • delete 6.x, retain /files and /sites directories, remember any mods to .htaccess
  • drop in 7.0 files
  • drush updb
  • if no error, press y
  • if error occurs, make a note of what you need to do to fix it. If this is a repeat run, fix all errors noted
  • rename/move your Drupal directory (or delete it but in the event you need it later)
  • from above where the Drupal directory should be, drush arr [backup-filename-without-square-brackets].tar
  • repeat from 3 til everything works
  • drush up -y
  • go do all the other little bits and pieces you need to do with the theme and modules

Problems I hit

  • managed file duplicates: mysql select * from `files` where `filepath` like `%string%'; delete from `files` where `fid`='number'; I only had a handful to deal with at any one time so didn’t mind doing them one by one, you’ll probably want a script if you have a lot :)
  • comment stupidity: drop index comment_uid on comments; drop index last_comment_uid on node_comment_statistics;

Drupal notes: backup process with drush

There’s probably a script for this. Or maybe drush is the script. Is all good, some things are done much faster with commandline :)

drush vset maintenance_mode 1 
drush cc all 
drush arb default --destination=../[site]-[date].tar

And if there’s updates to be done:

drush up -y

At the end:

drush vset maintenance_mode 0

Sharing is...

…privacy concerning if you’re one of those people that object to things like tracking you around the internet, and not a big deal if you’re not.

It bugs me a little bit (especially as I realised recently that I’d forgotten to mention tracking cookies and scripts etc that would have been coming from the share module I was using in my privacy thing) so in the event it bugs anyone who happens to drop by I’ve replaced the module I was using for sharing with [Social Share Privacy](Social Share Privacy). Anywhere you see +1/Like/Pin/Tweet/etc buttons on a website, it’s set scripts and cookies and things in your browser already (unless you have a Fortified Firefox). What this module does is load the button image for the socnet in question but doesn’t set the cookie or the scripts etc. Your first click will activate the button so it can plant its cookies and scripts and do its little tracking thing. Your second click will launch the sharing thing.

Drupal 7 notes: html5 placeholder text

Fairly straightforward. Mostly here as a reminder for how to do webforms; (example has a theoretical nid of 1 and a theoretical form field called “message” and the “submitted” part stymied me for a bit).

template.php

<?php 
function mytheme_form_webform_client_form_1_alter(&$form, &$form_state, $form_id) {
    $form['submitted']['message']['#attributes']['placeholder'] = t('Type your message here...');
} 
?>

Could also use a switch in hook_form_alter(). One of these days I’ll think about finding out if there’s any difference performance-wise in using a big switch with many form alterations or several smaller hook_form_form_id_alters.

theAbysmal Calendar Drupal module

I think it’s done now.

  • fixed an issue where the years weren’t being calculated properly (?!)
  • I may be slightly out of sync with the official documentation, at least according to the tests I’ve done on my local so far. This year should match up, and so will key dates (New Year Day and Leap Day). I’ve shifted leap years to fall on multiples of 4 so calculating would be a simple case of if mod 4 is 0, it’s a leap year, if mod 128 is 0, it’s not a leap year, so some of my years will be a day or two out

theabysmal_calendar.info

name = theAbysmal Calendar 
description = Converts Gregorian date to theAbysmal date 
core = 7.x 
package = Date/Time

theabysmal_calendar.module

<?php
/**
* @file
* Converts date from Gregorian to theAbysmal Calendar
* 
* theAbysmal Calendar in depth: http://theabysmal.wordpress.com/theabysmal-calendar/
* Currently uses months from the Tranquility Calendar 
* (http://www.mithrandir.com/tranquility/tranquility.html) and days in
* highly informal/bastardised lojban as that's how I use it.
* 
* @author ryivhnn
*/

/**
* Implements hook_help.
*
* Displays help and module information.
*
* @param path
*   Which path of the site we're using to display help
* @param arg
*   Array that holds the current path as returned from arg() function
*/

function theabysmal_calendar_help($path, $arg) {
switch ($path) {
    case "admin/help#theabysmal_calendar" :
    return '<p>' . t("Converts date to theAbysmal Calendar") . '</p><p>&nbsp;</p><p>';
    break; 
    } 
} 

function node_created_date() { 
    // grab node create date 
    $query = db_select('node', 'n') -> fields('n', array('nid', 'created')) -> condition('nid', arg(1)) -> condition('status', 1) -> execute() -> fetchAssoc(); 
    $date = $query['created']; 
    return $date; 
} 

function today() { 
    // grab today's date 
    $date = time(); 
    return $date; 
} 

function date_converter($date_ce) { 
/** 
    * doing the calendar converty magic 
    * owe the algorithm to the software engineer husband 
    */
    
// set the zero date and time for theAbysmal Calendar 
$zero_date = mktime(0, 0, 0, 12, 21, 2012); 

// this is New Year Day on theAbysmal Calendar 
$zero_day = date('z', mktime(0, 0, 0, 12, 22, 2012)); 

// this is first day which is counted from 0 
// calculate theAbysmal year 
if ((date('m', $date_ce) == 12) && (date('d', $date_ce) > 20)) { 
    $year = date('Y', $date_ce) - date('Y', $zero_date); 
} 
else { 
    $year = (date('Y', $date_ce) - date('Y', $zero_date)) - 1; 
} 

/** 
    * I think theAbysmal Calendar may be following Gregorian leap years to a degree. 
    * I couldn't find anything explicit in the documentation on the website. 
    * Because it counts from 0 every 4th number ends up being slightly weird (3, 7, 11, 15). 
    * I've decided to modify slightly to make leap years occur on multiples of 4 like they do 
    * on the Gregorian which then makes the not observing the leap year every 128 years 
    * slightly easy to remember, and also takes an operator out of the maths. 
    * Yes I can write an entire rationale in the comments but cbf putting in one extra operator. 
    */ 

if ($year % 4 == 0) { 
    $ly = 1; 
    
    if ($year % 128 == 0) 
    { $ly = 0; 
    } 
} 
else { 
    $ly = 0; 
} 

// because the leap years are different, we need to do maths with Gregorian 
$gly = date('L', $date_ce); 

// "buffers" for the leap year calculations 
if ($ly == 0 && $gly == 0) { 
    /** 
        * the way the $year_length calculation works, years where it's not a Gregorian 
        * or theAbysmal leap year come up a bit short, so this pads it out 
        */ 
        
    $leap_buffer = 1; 
}
elseif ($ly == 1 &amp;&amp; $gly == 1) { 
    /** 
        * conversely if the leap years coincide it makes the $year_length a bit longer 
        * than it should be, so this adjusts 
        */ 
        
    $leap_buffer = -1; 
} 
else { 
    $leap_buffer = 0; 
} 

/** 
    * working out how long the year is 
    * need to +1 to make it an absolute value 
    * add theAbysmal leap year and subtract the Gregorian leap year as required 
    */ 
    
$year_length = date('z', mktime(0, 0, 0, 12, 31, $year_ce)) + $ly - $gly + $leap_buffer + 1; 

// what day number are we up to in the Gregorian, + theAbysmal leap year + the leap buffer 
$gregorian_day_of_year = date('z', $date_ce) + $ly + $leap_buffer; 

// how many days have passed since theAbysmal start date, plus tA leap year day, less Gregorian leap year day 
$days_from_start = ($gregorian_day_of_year - $zero_day + $year_length) % $year_length; 

// theAbysmal month converted to an integer 
$month_number = intval($days_from_start / 28); 

// what day in the month it is 
$mday = $days_from_start % 28; 

// lojban day names assigned to month names 
$days = array( 
    'xundei' => array('0', '7', '14', '21'), 
    'najdei' => array('1', '8', '15', '22'), 
    'peldei' => array('2', '9', '16', '23'), 
    'ri\'ordei' => array('3', '10', '17', '24'), 
    'cicnydei' => array('4', '11', '18', '25'), 
    'bladei' => array('5', '12', '19', '26'), 
    'zirdei' => array('6', '13', '20', '27'), 
); 

foreach($days as $key =&gt; $value) {
    if (in_array($mday, $value)) { 
        $weekday = $key; 
    } 
} 

// Tranquility calendar month names 
// the 14th "month" is a 2 day intercalary period for New Year Day and Leap Year Day 
$months = array( 
    '0' => 'Archimedes',
    '1' => 'Brahe',
    '2' => 'Copernicus', 
    '3' => 'Darwin',
    '4' => 'Einstein', 
    '5' => 'Faraday',
    '6' => 'Galileo', 
    '7' => 'Hippocrates',
    '8' => 'Imhotep', 
    '9' => 'Jung', 
    '10' => 'Kepler', 
    '11' => 'Lavoisier', 
    '12' => 'Mendel', 
    '13' => '', 
); 

foreach ($months as $key =&gt; $value) { 
    if ($key == $month_number) { 
        $month = $value; 
    } 
} 

/* 
    * intercalary days 
    * 
    * check to see if it's December Solstice (Dec 21) 
    * if it is, it's New Year Day 
    * if not, it's a weekday 
    * 
    * if it's a leap year, Dec 20 is Leap Year Day 
    */
    
if ($month == '') { 
    $weekday = ''; 
    
    if (date('d', $date_ce) == 21 && date('m', $date_ce) == 12) { 
        $mday = "New Year Day"; 
    } 
    
    if ($ly == 1 && date('d', $date_ce) == 20 && date('m', $date_ce) == 12) { 
        $mday = "Leap Day"; 
    } 
} 

// leading zero silliness 
$yrabs = abs($year); 
$yrlen = strlen($yrabs); 

if ($date_ce < $zero_date) { 
    $era = 'btAT'; 
} 
elseif ($year == '0000') { 
    $era = 'tAT'; 
} 
else { 
    $era = 'tAT'; 
} 

if ($yrlen == 1) { 
    $year = '000' . $yrabs; 
} 
elseif ($yrlen == 2) { 
    $year = '00' . $yrabs; 
} 
elseif ($yrlen == 3) { 
    $year = '0' . $yrabs; 
} 

$daylen = strlen($mday); 

    if ($daylen == 1) { 
        $mday = '0' . $mday; 
    } 

    return $year . $era . ' ' . $month . ' ' . $mday . ' ' . $weekday; 
}

function date_shorten($date) { 
    /** 
     * can't help but feel this may be unnecessarily long winded :) 
     * 
     * Months and days are put into an array 
     * the first letter in each is grabbed 
     * the $date string is searched and the full name replaced with the letter 
     * then spaces are stripped 
     * 
     * I also continued with negative years in the array even though Not Jack 
     * isn't a huge fan og negative years as the $era was making my short dates 
     * longer :) 
     */
     
    if ($era = 'btAT') { 
        $neg = '-'; 
    } 
    
    $month = array( 
        'Archimedes', 
        'Brahe', 
        'Copernicus', 
        'Darwin', 
        'Einstein', 
        'Faraday', 
        'Galileo', 
        'Hippocrates', 
        'Imhotep', 
        'Jung', 
        'Kepler', 
        'Lavoisier', 
        'Mendel', 
    ); 
    
    foreach ($month as $value) { 
        $m = $value[0]; 
        $date = str_replace($value, $m, $date); 
    } 
    
    $wday = array( 
        'xundei', 
        'najdei', 
        'peldei', 
        'ri\'ordei', 
        'cicnydei', 
        'bladei', 
        'zirdei', 
    ); 
    
    foreach ($wday as $value) { 
        $wd = $value[0]; 
        $date = str_replace($value, $wd, $date); 
    } 
    
    $date = str_replace(' ', '', $date); 
    $date = str_replace('btAT', '', $date); 
    
    return $neg . $date; 
}

/** 
 * implements hook_block_info() 
 */
 
 function theabysmal_calendar_block_info() { 
    $blocks['today'] = array( 
        'info' => t('Today\'s theAbysmal date'), 
        'cache' => DRUPAL_NO_CACHE, 
    ); 
    
    $blocks['posted'] = array( 
        'info' => t('Converted post date'), 
    ); 
    
    return $blocks;
}

/** 
 * implements hook_block_view() 
 */ 
 
 function theabysmal_calendar_block_view($delta = '') { 
    $block = array(); 
    
    switch ($delta) { 
        case 'today': $block['subject'] = t('Today\'s theAbysmal date'); 
            if (user_access('access content')) { 
                $date_ce = today(); 
                $block['content'] = date_converter($date_ce); 
            } 
        break; 
        case 'posted': $block['subject'] = t('Converted post date'); 
            if (user_access('access content')) { 
                $date_ce = node_created_date(); 
                $date = date_converter($date_ce); 
                $block['content'] = date_shorten($date); 
            } 
        break; 
    } 
    
    return $block; 
}
?>

Usage instructions

  • create a folder called theabysmal_calendar in sites/all/modules
  • copy theabysmal_calendar.info and theabysmal_calendar.module to files of the same name and save them into the folder you created. Dicth the closing php tags in the module file
  • if you don’t like the month and week days I’m using, change them to whatever you like in the days and month arrays (two of each, just be aware that the date shortening as it is may look odd with week day and month names that start with the same letter, the date formats can be changed easily enough)
  • go to the modules page (I think it’s Admin >> Modules, I use the admin toolbar and it’s just there), scroll down to Date/Time and check theAbysmal Calendar on
  • go to Admin >> Structure >> Blocks and activate and position the blocks you want. There are two provided, one converts today’s date and the other converts and shortens a post date. I’m using <?php echo render[$block['content']; ?> to spit the shortened date into the top of the blog posts

If anyone knows how to write Wordpress plugins and feels like converting it, go nuts.  Then tell Not Jack so theAbysmal Calendar blog can have an Abysmal Calendar :)

Thoughts on DiSo

I’ve had several.

Simultaneously, I’ve had several thoughts on which way this post was going to go. Originally I was going to go along the vein of a blog post from Zauber Paracelsus (which outlines the “nymwars” debacle to the date of that post, people embarking on “Project DeGooglefy” and alternatives to Google services).

Then I was going to do something or other about G+ and Facebook and problems with centralised social networks.