University of Oregon

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…)

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…)

Drupal6 VBO – ‘Open Links’ header link option updated

Last month I posted the code I used to add a link to header that opens all the edit links in a view. Developments » Drupal6 VBO – ‘Open Edit Links’ link added to header.
This month I had a view with a workflow link and I wanted to add a link to open those links as well.

So I updated the function to take two variables: the descriptive name of the link type (descName) and the jQuery css selector (cssSelector).
So the original ‘Edit’ link had to be updated from

openEditLinks()

to:

openEditLinks('Edit','.views-field-edit-node a');

But now it can be called side by side with the workflow link option:

openEditLinks('Workflow','a.workflow-link');

. Like so:

<a href="#" title="Click to open ALL edit links or highlight a group of edit links to limit the number of links that open.">Open Edit Links</a> |
<a href="#" title="Click to open ALL Workflow links or highlight a group of workflow links to limit the number of links that open.">Open Workflow Links</a>

(more…)

Drupal 6, Content Profile default username

I have a content profile set up on a number sites and I noticed when I used User / Node Import recently that I couldn’t assign an import value to the title field of the node profile, which I was using as First Name.
After I did the import. I found out why. The “title” field was defaulted with the Drupal user name.
Which is a very good idea.

So I updated my other content profiles to use this same convention but then I realized that there’s no easy way to default that field value to username.
So I considered a

  • Custom module that overrides the content type Profile and defaults the title with the username. – This is the best option
  • jQuery script in a block that runs on the profile edit pages – This is quick but may miss some pages and relies on javascript and possibly my profile set up (show in tabs)
  • jQuery script in a Views block – This may be slightly better than the static script as it pulls the user name from the database, but it’s is more effort

I settled on the simple jQuery script to make it quick.
I pull the user name from the title of the edit page, which in my configuration is the user name.
I don’t know if this is always the case but it was in this case so it made it easy to use jQuery.

I could potentially use this script in a custom module. But staying server-side and preprocessing the fields would make more sense.
I could also set the script to run anytime the edit page is opened.
The jQuery / Block method relies on known, url configurations that == the profile edit page.
For example: Right now the block is set to
Show on only the listed pages.
With these urls:

user/*/edit/profile
user/*/profile/profile*

I couldn’t remember if this would work with out ‘clean URLs’ enabled but a quick test with

$_SERVER['QUERY_STRING']

showed that the query string is still ?q=user/*/edit/profile. So these listed urls should continue to work.
If I ever include the user profile in a page wizard workflow this block probably wouldn’t show up so that’s another point for the custom module method.
(more…)

Added jQuery browser check for ODT and Skillport eLearning

I noticed a mac user getting frustrated when trying to log in to the eLearning site the other day.
The log in process was hung up on the loading screen for a few minutes:

I pointed out that it said ‘click here’ if it doesn’t load after 40 seconds but I realize that’s not very helpful.
It turns out they were using Firefox, which isn’t one of the supported browsers.

We do have the browser compatibility information on the page but that content is collapsed. So the user would have to look for it.
So I decided to add some browser compatibility tests and display a message to mac / firefox users to save them some trouble.
(more…)

Drupal6 VBO – 'Open Edit Links' link added to header

Update 4-13-11: Developments » Drupal6 VBO – ‘Open Links’ header link option updated.

I took the bookmarklet that opens all the ‘Edit’ links on the Views Bulk Operations content management page and updated it to work in the header of the view.

Here are some screenshots of what it looks like and the code I placed in the header of the view.
(more…)

Moved Content Highlight Up Above Links

I moved the content highlight section up above the nav links and administrative content.
I had to do this with javascript/jquery so it’s an enhancement for those with javascript enabled but I think it’s nice to keep the content together. (more…)

Drupal 6 adding styles from a themed view variable output

Here’s an overview of what I did to add a style to the doc head from within a themed view for a specific variables output. I’ll likely post more on that scenario later.
I don’t intend to raise a discussion about whether or not we should be adding one-off styles in a view vs. appending a master stylesheet, but the facts remain that I did it, it came in pretty handy for testing and it requires some configuration worthy of saving for later (so I don’t have to try and figure it out again!). The best part was being able to keep the custom styles in the same page where I was working on theming the view.
To make things a little more complicated I only wanted to append one rule to the head of the page and not adding a style to each row (which could be done with jquery).
So here’s what I did: (more…)

Drupal use jquery to coordinate two cck fields.

I have a module that alters a CCK custom content type and I’m using it to include some jQuery that updates the upper value of paired fields.
In the initial use case the two fields were age range, one for low and one for high. Then I added support for a scenario that uses two date fields as well.
Theoretically the function can be called from anywhere. You only need the ID of the two fields and the type: int or date.

The logic behind the update is simple but optimized for for usability. When it’s invoked, the upper range is checked against the lower range and updated if it’s lower.

I experimented with .blur(), .focus() and .change(). These are shortcuts for jQuery’s .bind() function, which I used to combine blur and change where both are used on one element.

Using jquery is always a learning experiment and I’ve got some fun js stuff in there too like !isNaN(parseInt()) to verify that the value we’re dealing with is a number and not a default option like “None”.
Most importantly I learned that we don’t need to use $(document).ready(function(){}); in Drupal modules. Instead we can use Drupal.behaviors.myModuleName = function (context) {} instead.

updateUpperField is the function I wrote to achieve this:
function updateUpperField(‘lowerFieldID’,’upperFieldID’,type:’int’|’date’)
Usage: call updateUpperField from within the document and include the two id’s of the fields in question as well as the type of field.
(more…)

Drupal and ajax or ahah to load content on demand

I’ve been including an external page on load in one of my nodes and that doesn’t make a lot of sense as it’s a curl of another site and isn’t viewed very often so I’d rather include it on demand.
So now it works with jquery and ahah.
Here’s the page: https://odt.uoregon.edu/content/elearning
The function is called when you open the FAQ section and click the first link: ‘What browsers are supported for PC and Mac?’.
Here’s an example of the code: (more…)