University of Oregon

Drupal 6 – Set the Relative Weight of a Custom Module

I converted a template.tpl.php override to a module override for attachment_links recently. One of the interesting functions I had to use was hook_enable. Hook_enable is handy for setting the module weight and ensuring that my overrides run after the original module.

The default module weight in the system table is 0. In this case, attachement_links uses 99. So rather than set my module’s weight to 100 I thought I’d just reference the other module and increment it’s weight by 1. That way if they change it in the future I’ll be good to go.
(more…)

Drupal 7 Field Collection for build an order form

I’ve been leveraging the Field Collection module in a few projects lately and today I created a quick demo of how it could be used in a print shop order form.
Thanks to Joseph (here at the UO and directcommunities.com) who demonstrated the Field Collection module recently.

The form needed to allow an individual to upload a file and then choose certain options for that file. There is a requirement that the user would be able to upload an infinite number of files and each file can have unique options. This is the current version: image of form fields required

Here’s what my Field Collection option looked like:
Screen shot of form with field collection
(more…)

Drupal DB backup bash script with Drush and Git

I like to make incremental backups of my drupal databases. To do that I’ve tried combine the best practices* into one bash script that I can run periodically and easily.
The end result is a bash function that’s easy and user friendly.
It works by leveraging Drush aliases and the drush sql-dump command in combination with git.

Usage is simple. From the command line:

$ dg_db_backup drush.alias "commit message" 

Note: dg_db_backup -> drush git database backup
(more…)

Drupal 7 CCK – Creating ‘Label Bulk Operations’

I have a new D7 site in the works and I found that I wanted the majority of my content type field labels to display: inline as opposed to the default: ‘above’.
Several of my content types have dozens and dozens of fields to I wanted a quick way to automate changing all the selectors at once.

I suppose ‘repetition and redundancy are the Aunt and uncle of invention’.

Here’s what I came up with:
image of the new fieldset and buttons
(more…)

Branching with Git – keeping remote and local sync'd

Max pointing me to his method of branching, inspired by: http://www.zorched.net/2008/04/14/start-a-new-branch-on-your-remote-git-repository/

Which creates a branch on the remote origin and then pulls it down and tracks it locally.

The author created a bash script and I wanted to make that a function to keep in my .bashrc file;

(more…)

Drupal 6 – Add css with drupal_add_js

I’ve used this method for years and I wanted to post a complete example as a reference.

I love the css_injector module but I like to keep my views’ css with my view.
So this code example has all my required elements for inserting css into a views php header;

  • CSS is readable during editing thanks to php’s HEREDOC
  • function are not re-declared
  • css is minified (Mostly, I keep the white space…)
  • the Drupal.behaviors syntax that ensures code is only ran on the page load
  • and it leverages drupal_add_js

Here it is with in-line comments: (more…)

Redirect an entire Drupal site – .htaccess

I’ve migrated several Drupal sites to new servers lately and since some of those sites were in development they didn’t have a proper cName. So I’ve been using a .htaccess redirect to channel all traffic from the old site to the new ones.
Here’s an example of the new .htaccess file I placed in the /hr_diac directory on my old server:
##Redirect everything that pointed to /hr_diac to http://access-guide.hrstg.uoregon.edu/$1

RewriteEngine on
RewriteCond %{REQUEST_URI} /hr_diac [nc]
RewriteRule ^(.*)$ http://access-guide.hrstg.uoregon.edu/$1 [r=301,nc]

To clarify; I completely replaced the drupal .htaccess file with this file.
It’s pretty simple but it works.

Drush Make post processing – Adding module type

We’re using drush make files to keep our Drupal sites updated.
Aegir will build a platform (base install for a multisite) from a make file. I believe it also re-builds the platform from that makeFile each time the platform is verified.

This is pretty handy but requires some leg work up front to generate the makeFile. http://drushmake.me is the go-to site to get a fresh makeFile built.
Though I just looked at the drush help file and saw that there’s a drush make-generate command ($ drush make-generate myMakeFile.make) that

Attempts to generate a makefile from the current Drupal install, specifying project version numbers unless not known or otherwise specified. Unversioned projects will be interpreted later by drush make as “most recent stable release”

So I gave it a shot and it did a great job.
(more…)

Handle sites/default/files redirect in Drupal 6 with Custom Errors module

When I’ve migrated sites into Aegir recently, I find that the users have many links in the content hard-coded to files in the default directory.
While it is possible to use the Scanner search & replace module to ferret those out. It’s not always thorough enough. For example, it doesn’t search and replace menu link urls, or content in blocks, views, headers, footers or custom fields. You can specify custom fields and locations to search but that’s another bag of worms that fraught with issues.
So I wanted to find a simple way to catch any hard coded links that I missed.
Enter the Custom Error module.

I’ve been using it to provide a populated search form on the 404 ‘Not found’ page as well as a site map but I also have a few custom redirects in there so adding an option for sites/default/files was simple.

Here’s the code: (more…)

Drupal 6 capture the alias and path_redirects upon deleting a node

So, I’m in the process of deleting some node but I wanted a quick way to be sure that I had all the url aliases for that node first.
That way I could redirect them to the appropriate places instead of leaving users with 404 errors.

I’m using path_auto for clean urls and the path_redirect module to allow more than one redirect to a node. This particularly handy when the node alias is changed often or when migrating content from another site.

I tested the idea of creating a view to capture the node’s path and all the path_redirects, but it would require some custom mysql as path_redirect is not exposed to views right now.
That seems silly anyway, as the data is already in the node form.

Here’s the long term plan:

  • Create a module that hooks the node delete process
  • include some admin options to turn on and off features
  • Upon deletion
    1. Capture the path alias and all the path redirects from the form (checking that each module is installed and enabled
    2. Open a new window for each path prompting the admin to select a new destination for that path (even is better than a 404 error page). url: admin/build/path-redirect/edit/[rid]

Perhaps there are some merits to saving more data in the db about the history of that link but I suspect it’s unnecessary.

So stage one, I wrote a small javascript that I placed in a bookmarklet to capture the data from the node edit form and put it into a list in a new window.
<a href="javascript:(function(){linkList=new%20Array();currentPath=document.location+'';currentPath=currentPath.replace(/[https]+://.[^/]+/,'');currentPath=currentPath.replace(//edit/,'');linkList.push(currentPath);linkList.push($('#edit-path').val());$(%22#node-formfieldsetlegenda:contains('URL%20redirects')%22).parent().parent().addClass(%22urlRedirectSection%22);$('.urlRedirectSection%20table%20tr%20td:first%20a').each(this,function(){thisText='';thisText=$(this).text();linkListDup=false;/*check%20for%20dupes.*/for(linkListI=0;linkListI<linkList.length;linkListI++){if(linkList[linkListI]==thisText){linkListDup=true;}}if(!linkListDup){linkList.push(thisText);}});wnd=open('','Redirect%20List%20for%20this%20node','width=400,height=400,top=0,left=0,scrollbars,resizable');with(wnd.document){writeln('

');for(linkListJ=0;linkListJ<linkList.length;linkListJ++){writeln(linkList[linkListJ]);}writeln('

‘);}wnd.document.close();})()”>Display paths to this node

javascript:
(function(){
  linkList= new Array();
  currentPath=document.location+'';
  currentPath=currentPath.replace(/[https]+://.[^/]+/, '');
  currentPath=currentPath.replace(//edit/, '');
  linkList.push(currentPath);
  linkList.push($('#edit-path').val());
  $(%22#node-form fieldset legend a:contains('URL redirects')%22).parent().parent().addClass(%22urlRedirectSection%22);
  $('.urlRedirectSection table tr td:first a').each(this, function(){
    thisText = '';
    thisText = $(this).text();
    linkListDup=false;
    /*check for dupes.*/
    for(linkListI=0;linkListI &lt; linkList.length;linkListI++){
     if(linkList[linkListI] == thisText){
        linkListDup=true;
      }
    }
    if(!linkListDup){
      linkList.push(thisText);
    }
  });
  wnd=open(&#039;&#039;,&#039;Redirect List for this node&#039;,&#039;width=400,height=400,top=0,left=0,scrollbars,resizable&#039;);
  with(wnd.document){
    writeln(&#039;<pre>');
    for(linkListJ=0;linkListJ &lt; linkList.length;linkListJ++){
      writeln(linkList[linkListJ]);
    }
    writeln(&#039;</pre>');
  }
  wnd.document.close();
}
)()

If I run that on my front page I get a pop up window with these contents:

/node/12
home

So the next stage would be to open devel and determine the field value addresses.
But that may have to wait a while. In the interim I’ve got my bookmarklet and I may add the code to open each path in the path-redirect/edit/ window next.