Drupal 6 – Contact Override custom module

Just created a quick and dirty custom module: Contact Override.
I posted it on bitbucket: bitbucket.org/_vid/contact_override

Nothing too exciting, just an override to the D6 contact form. It appends the senders email into the body of the email.

We had noticed that while emails from the Drupal contact form contained the senders email in the reply field that email address was often lost when forwarding the message.
Here’s the readme:

Contact Override: Use hook_mail_alter to add the sender’s email address to the body of the email.

Contact

Current maintainers:

Drupal Version

  • Drupal 6

Purpose

The contact_override module is a simple custom module that adds the senders email to the body of the contact page message. For example, the standard email message would be appended with this:

– Mail sent out from Drupal on behalf of vid@test-site.test.

  • Note: ‘Drupal’ is substituted with the site_name if set.

Demonstrated Need

  • It was noticed that while emails from the Drupal 6 contact form contained the senders email in the reply field that email address was often lost when forwarding the message.
    For example when an email is received from the ‘Test Site’ contact form the reply line may look like this:

    From: test@test-site.test on behalf of Vid < vid@test-site.test >

    When that message is forwarded the email address in the brackets is lost in the body of the message, like so:

    —–Original Message—– From: test@test-site.test [mailto:test@test-site.test] On Behalf Of Vid

    This module adds that line to the message body to work around this issue. So now we would see this at the end of the email:

    – Mail sent out from Test Site on behalf of vid@test-site.test.

Scope: What this module does

  • Uses hook_mail_alter to add content to the body of the email

Out of Scope: What this module does not do

  • This module does not have an admin settings page

Requirements

  • None

References

  • Ref: http://api.drupal.org/api/drupal/includes!mail.inc/function/drupal_mail/6
  • Ref: http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_mail_alter/6

Repositories

  • bitbucket.com
    • General info: https://bitbucket.org/_vid/contact_override
    • Clone ssh: git@bitbucket.org:_vid/contact_override.git
    • Clone http: https://_vid@bitbucket.org/_vid/contact_override.git
  • git.uoregon.edu
    • General info: https://git.uoregon.edu/projects/UO_DRPL_PROD/repos/contact_override/browse
    • Clone ssh: ssh://git@git.uoregon.edu/UO_DRPL_PROD/contact_override.git
    • Clone http: http://vid@git.uoregon.edu/scm/UO_DRPL_PROD/contact_override.git

Installation

  • Install module as usual, see [Installing contributed modules](http://drupal.org/node/70151 for further information.

Usage

In short:

  • Enable the module. Once enabled the body of the contact page template will be modified.

Git.uoregon.edu Repo Notes:

  • The master branch can only be written to by Vid.
  • Feel free to create new branches and push them up. The dev branch can be written to by anyone.

Reponsive design and the iPad viewport

I had been noticing that the iPad’s viewport in webkit browsers was odd but I couldn’t put my finger on it.

So I opened Safari on the iPad and ran some JS in firebug lite; the result: 768×504 in landscape and 768×928 in portrait mode.
Not what I expected at all.

The code I ran in firebug lite was:

alert(jQuery(window).width() + 'x ' + jQuery(window).height());

(prepend javascript: for use in the URL bar)

Bottom line: the viewport width is 768px in both portrait and landscape mode. That’s a little odd.
(more…)

Working on a file in two git branches at one time

I have a file in two different branches of a project that is so drastically different (hundreds upon hundreds of changes) that merging was becoming unreasonable. I’ll call that file CONVOLUTED.module.
I wanted to go old-school and compare the files side by side. Cutting and pasting changes with regular commits so I could test the incremental changes as I manually merged the files.
I figured there are several options to do this.

Option 1

One might be to move out of my working directory and re-clone the repository. In which case I would also specify the branch that I wanted to clone and use a new directory name to clone into.
Then I could open the both version of the CONVOLUTED.module file.

To do that I’d use a normal ‘git clone’ with the -b option to specify the branch and then I’d append the command with the new directory name.

To illustrate that; here’s the code to pull down a clone of the remote repo: ssh://git@GIT.HOSTING.URL/PROJECT_NAME/REPO_NAME.git;

$ git clone ssh://git@GIT.HOSTING.URL/PROJECT_NAME/REPO_NAME.git

No fancy stuff, just a clone.

Now I’ll insert ‘-b DEV_BRANCH’ to choose my branch and I’ll append a new directory name: ‘REPO_NAME_DIRECTORY_NAME’ to clone into.

$ git clone -b DEV_BRANCH ssh://git@GIT.HOSTING.URL/PROJECT_NAME/REPO_NAME.git REPO_NAME_DIRECTORY_NAME

Done.
Now I have a copy of the different versions of the file(s) that I can open simultaneously.

This may be ideal in some cases; I use it all the time with my web servers when I have a dev and live versions of a site that live in different directories but they share the same repo so I can share assets and check out code changes as needed.

Note: of course you can achieve this same result without using a remote repository; by duplicating the entire working directory and then changing the active branch in the directory copy.

But that’s not what we’re looking to do here. So in summary, this option has it’s drawbacks if you only need to compare one file. If your project is large, you’re using up storage space for no reason.

Option 2

We could create an empty directory and pull down just the one file.
Steps overview:

  1. Create the new dir. (repo. name – branch name)
  2. git initialize the new dir.
  3. Set the git remote
  4. Fetch (don’t pull) the files
  5. Check out the single file from the specified branch

On the command line that could look like this:

$ mkdir REPO_NAME-DEV_BRANCH
$ cd REPO_NAME-DEV_BRANCH/
$ git init
Initialized empty Git repository in /file/path/REPO_NAME-DEV_BRANCH/.git/
$ git remote add origin ssh://git@GIT.HOSTING.URL/PROJECT_NAME/REPO_NAME.git
$ git fetch
remote: Counting objects: 738, done.
remote: Compressing objects: 100% (580/580), done.
remote: Total 738 (delta 318), reused 470 (delta 149)
Receiving objects: 100% (738/738), 10.89 MiB | 19.52 MiB/s, done.
Resolving deltas: 100% (318/318), done.
From ssh://git@GIT.HOSTING.URL/PROJECT_NAME/REPO_NAME.git
 * [new branch]      master     -> origin/master
 * [new branch]      MAIN_BRANCH    -> origin/MAIN_BRANCH
 * [new branch]      DEV_BRANCH -> origin/DEV_BRANCH

At this point you can see I’m down to step 4. I’ve fetched but my directory is all but empty.

$ ls -al
total 0
drwxr-xr-x   3 vid  admin   102 May  3 13:27 .
drwxr-xr-x+ 30 vid  admin  1020 May  3 13:27 ..
drwxr-xr-x  12 vid  admin   408 May  3 13:31 .git

Just one .git dir in there. That’s quite a bit smaller than if I’d done a clone or pull and had all the files in the branch.

To continue, I’ll checkout the file I want (CONVOLUTED.module) from the remote branch (DEV_BRANCH):

$ git checkout origin/DEV_BRANCH -- CONVOLUTED.module
$ ls -al
total 104
drwxr-xr-x   4 vid  admin    136 May  3 13:31 .
drwxr-xr-x+ 30 vid  admin   1020 May  3 13:27 ..
drwxr-xr-x  13 vid  admin    442 May  3 13:31 .git
-rw-r--r--   1 vid  admin  49183 May  3 13:31 CONVOLUTED.module

There it is. Now I’ve basically achieved the same thing in two different ways. Perhaps saving some space, but not any time.

Yet there’s another option.

Option 3

In this scenario I stay in my working branch and create a temporary copy of a file from the other branch.

If I simply checkout the file from the other branch I would overwrite my file. So a better way would be to ‘show’ the file contents and pipe them into a new file.

Let’s break that down;
To show the file we can use ‘
git show‘ and specify the branch:file name:

git show DEV_BRANCH:CONVOLUTED.module

If you are not in your repository’s root, you can put in the absolute path to the file or just use the relative link (./).

git show DEV_BRANCH:./CONVOLUTED.module

‘git show’ displays the file contents in std out. Give it a shot and you’ll get a screen full of code.

So to pipe that into a file it’s a simple as appending ‘>’ and a new file name*.
I choose the name CONVOLUTED_DEV_BRANCH.module;

git show DEV_BRANCH:./CONVOLUTED.module > CONVOLUTED_DEV_BRANCH.module

You could also keep the same name and pipe the file into a temp dir.

*Note it doesn’t have to be simple if you don’t want it to be… you can play with different pipe options: &> or 2> among them.

Bottom line

You have several ways to go old-school with your file comparison. These are a few that I played with today.
I suppose you are only limited by your imagination.

git branch – auto setup rebase = always

I was revisiting Randy Fay’s Rebase Workflow post and went down the ‘rebase’ rabbit hole again. Each time I learn a little more. Probably because I understand a little more…

This time I landed here: ‘Simpler Rebasing …’ http://www.randyfay.com/node/103 and as a result I’ve updated my git config to rebase on pull:
git config –global branch.autosetuprebase always

Note: this use of rebase isn’t the history altering version, rather an adjustment to the order of operations when pulling from a remote repo. The goal being to lay your local changes on top of the remote code each time you pull.

Here’s how it looks in my git config now:

[branch]
  autosetuprebase = always #Ref: http://www.randyfay.com/node/103 #alternatively use: git config --global branch.autosetuprebase always

We’ll see how it goes. I’ll add this to my public git_bash_files repository if I like it.

Clean up your curl – fixing relative links in scraped content

A few years ago I Added jQuery browser check for ODT and Skillport eLearning. This week I updated that functionality and noticed a relative link in some remote content I was scraping via cURL.
The problem being that the relative link needed to be fully qualified.

Here’s the old table that I cURL from the Skillport support site:
Screen Shot of the old compatibility table

Here’s the updated table:
a view of the table pulled in from skillport
The culprit is the “Click here to view Skillsoft’s Browser Support Statement” at the top of the table. “here” links to /44007.htm which isn’t helpful.
(more…)

Re-wrap a modified JS function

The question came up recently about the possibility of modifying an existing javascript without duplicating it. Yesterday, I found out that it’s indeed possible. The key to the solutions is to re-wrap an existing function with it’s modifications in an ‘anonymous function.’

Here’s the scenario:
I have a page where I check for some required browser plugins before sending users on to our eLearning site; Adobe Flash, Reader and Java.
I also check for optimal OS / browser combinations.
So in this scenario I was leveraging available JS & jQuery functions as much as possible but in one particular case I need to accurately display the users browser but jQuery.brower treats all webkit browsers as Safari.

So instead of installing a new jQuery plug in or write a script to parse the user’s browser info I took advantage of a function in the Java detection script I pulled in.
(more…)

Drupal 7 – resizing the content area based on publish date

I was asked to look at a site where the content created before Jan. 15th 2013 was crafted to display nicely in a 600px wide area.
New content is being added with a much wider area and with some thought given to the possibility of a fluid width (potentially responsive).

So while setting a fixed width isn’t ideal I came up with a proof of concept that demonstrated the possibility using jQuery to resize the content area on page load.
The content in question has two key elements to identify for jQuery:

  1. The publish date:
    $('.field-name-field-publish-date .date-display-single')
  2. The content area:
    $('.field-name-body')

Here’s a sample of the code that could re-format the older content based on the published date:

$('.field-name-field-publish-date .date-display-single').length
//if( (currentContentDate < narrowContentCutOffDate)) {
if ( ( new Date($('.field-name-field-publish-date .date-display-single').text()).getTime() < new Date('January 15, 2013').getTime() ) ) {
        $('.field-name-body').css('width','600px');
}//end if

Wrapped in a Drupal.behaviors function it could be added to the site in a js file like so, to run once on each page load:
(more…)

Drupal 6 – Pre-Popluate Menu Parents with jQuery

Here’s a fun one.
I wanted the menu parent to be pre-populated on node creation.

Use case:

  • When a content editor creates a new ‘Electronic Flyer’ it will be added to the per-determined menu: Primary Links >> Resources >> Electronic Flyers.

Here’s a screenshot demonstrating the pre-filled out menu parent: (more…)

Drupal 7 Views – Manipulating Args with php

I wanted to pass a string to a view that only accepts an entity id as an argument.
I the past I’d been able to use a string based argument after adding relationships but that wasn’t possible in this case.

So I decided to override the incoming argument during validation and replacing the string with the relevant entity id.

The use case:

I mentioned that the view only takes a entity id (nid) as an argument. That nid isn’t available to me when I call the view, but I do know a ‘location’ that I can associate with entities.
Ideally the town name “Eugene” is passed to the view but the entity id (237) is what’s used in the view.

The key was to call another view from within my contextual filter argument validation field.
There I pass the ‘location’ name argument to another view (nid_from_townname) using php and the views_get_view() function.
That view returns the nid for the entity in question. So then I override the original view’s argument with the nid and we’re in business.

Here’s a screenshot of the (view display’s contextual filter argument) validation field: (more…)

Drupal 7 – list taxonomy terms

I’m running some tests and thought I’d post the php code to display the terms for a taxonomy:

< ?php
$terms = taxonomy_get_tree(taxonomy_vocabulary_machine_name_load('temp_auth_temp_agency')->vid);
if ($terms) {
  foreach ($terms as $term) {
    $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
  }
}
echo '<pre>$options: '. print_r($options,true) . '</pre>';

/* Returns:
$options: Array
(
    [439] => DePaul
    [441] => Express
    [440] => Galt Foundation
    [442] => Kelly Services
    [443] => Manpower
    [444] => Personnel Source
)
*/
?>

At the moment I’m considering leveraging the benefits of CCK Select Other for ease of entry with taxonomies for term management. Not sure if it’ll be worth it but this was an interesting code hunt.
(more…)