University of Oregon

Updating select lists in bulk – w/ jquery, drupal

So I wrote this script 1 year and 1/2 ago about bulk label operations (Drupal 7 CCK – Creating ‘Label Bulk Operations’). Well the updateLabels() function from that code came in handy today.
I needed to update dozens of CCK formats from various defaults to editable. If you’ve used editable fields you know it can come in handy but if you have a lot of fields you’ll be in a carpel tunnel conundrum if you try to change then all.
So I pulled up the updateLabels() function opened the Firebug console and set all the various defaults to editable. And then back again. (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 hide/show a sidebar with jQuery

I’ve hid the right sidebar on the HR website today.
I used jQuery so that it degrades gracefully when users don’t have JS enabled.

The sidebar-right region is used for ancillary information; links, the audience for the node content, etc… So it was determined that we could hide it but make the information available if people wanted more. (more…)

Drupal 7: jQuery and Subtotaling Views Calc Tables

As a follow up to Drupal 6: Using jQuery to pick up where views_calc falls short, I worked on a project this week that used views_calc in D7 and we wanted to leverage the same subtotaling that I demonstrated in D6.

There are a lot of similarities in the code so it didn’t take much tweaking. (more…)

Drupal 6: Using jQuery to pick up where views_calc falls short

Views Calc is a great Drupal module but there are a few things it doesn’t do.
It doesn’t:

  1. Accommodate totals by group
  2. Total CCK viewfield field values
  3. Total Views Customfield PHP values

I wanted to fix all three of those issue in a views report I’m using.
To be clear: My solution is uses jQuery as a hack/fix, specifically addressing the fields I want to count and sum. It does not address the inherent issue in views_calc. (more…)

Dynamic link destinations

I have an old Drupal 5 site that I inherited and I wanted to find or create users account quickly from committee lists.
The site is built in such a way that the committee member data is stored in text fields and not node references.
So when I’m notified that a committee group site is ready I have to check each member to see if they have an account or not. Then edit or create the user and add permissions to view the new group…

This was getting old… and yes… this site should be ported to Drupal 7; but since there is funding for a 3rd party replacement of this web application, spending money/time on the conversion isn’t in my short term plans. Making administration more efficient is…

So I wrote some javascript to do a quick look up of each committee member and determine if their account exists. If so, load the user page. If not, create the user and trigger a script that generates a new password.

Here’s a quick look at the committee member table layout:
Committee member table layout

I click this link:
Committee member link
which triggers the script to check the committee members.

Here’s the script:

<a href="#">0){
		iterator = Number(iterator);
		if(linkArray.length &gt;= iterator){
			thisEmail = '';
			thisEmail = linkArray[iterator].toString().replace('mailto:','');
			thisLink = '';
			thisLink = '/vpfa-searches/search-test/users/'+thisEmail.replace('@uoregon.edu','');

			checkLink(thisEmail, thisLink, iterator);
		}
	}
}

function checkLink(thisEmail, thisLink, iterator){
	iterator = Number(iterator);
  getUrlStatus(thisLink, thisEmail, function(status) {
    //alert(status);
    if(status != '404'){
      window.open(thisLink);
    }else{
      /*
       If page has a h2.title of Contact Information then it's a search committee page.
       Capture name and tax id and append link
      */
      committeeName='';
      committeeHeader='Contact Information';
      if($('h2.title:contains('+committeeHeader+')').length&gt;0){
        if($('.breadcrumb a:last').text()!=='Home'){
          committeeName = $('.breadcrumb a:nth-child(2n)').text();
        }else{
          committeeName = $('h1.title').text();
        }
      }
      window.open(encodeURI('/vpfa-searches/search-test/admin/user/user/create?genEmail='+thisEmail+'&amp;genRole='+committeeName));
    }
    triggerCheckLink(iterator+1);
  });
}
triggerCheckLink('0'); return false;"&gt;Open all Committee Members</a>

All of the js is in the link itself. Initially I had an external script for the ajax call back telling me the status of the link before acting on it, but it works in the link as well.
Allow me to explain the code a bit;
first off, I use jQuery to capture all the committee member email addresses.
Then I layout my functions and trigger the first link check.

The link check function “checklink()” calls the getUrlStatus ajax function which uses a callback. The callback returns the page status.
I wait for that status and then proceed accordingly. 404’s get the ‘create user’ page and everything else is sent to the user page.

In D6 or D7 I probably would have added a view to do this but I’m trying not to add much more server-side code to this old site.

I mentioned above that once on the create user page, I generate a new password and assign the user role. To do that I pass the user email and committee name to the create user page.
You may have noticed that I dig through the breadcrumbs to get the ‘committeeName’. Again views would make this easier but that’s how it goes.

I’ll describe the user creation script some other time.

Used jQuery to add a header for a group of courses

We had a group of courses for Take our Daughter and Sons to Work Day added to Making Tracks that all used the abbreviation of the name in their title: TODSTWD. They didn’t spell out the event name anywhere. So I added a jQuery script to the header that prepended the listing with the full title.
I limited this to course listing pages and confined it to a date range so it stopped running after the event.

Here’s a screenshot of the course list summary with out the jQuery header (before):

And here’s a screenshot of the course list summary with the jQuery header (after):

It also runs on the indiv. course details page.

Here’s the code:

&lt;?php
if(strtotime(date(&#039;Y-m-d&#039;)) &lt;= strtotime(date(&#039;2011-4-29&#039;))){ //display until 4-29-11
	$todstwd_script = &lt;&lt;&lt;javascript
	/* ########## Add TODSWD JS ########## */
	Drupal.behaviors.testBehavior=function(context){
		//run only if there is a context var
		if(typeof context !== &#039;undefined&#039;){
			/*
			Check the type of the context var and type. Process if context = [object HTMLDocument]
			Note: when context == [object HTMLDocument] then this is page load. You may want some script to run only on page load. Some not and some all the time.
			*/
			if(window.location.href.search(/course_view|course_list/)!==-1){
				$(&#039;.courseSummary h3 a&#039;).each(function(){
					if($(this).text().search(/TODSTWD/)!==-1){
						$(this).parent().parent().prepend(&#039;<h2 class="todstwd" style="color:#006600">Take Our Daughters and Sons To Work Day - 4-28-2011</h2>');
					}
				});

				$('#t_content h2').each(function(){
					if($(this).text().search(/TODSTWD/)!==-1){
						if(!$('#todstwd').length){
							$('#main').prepend('<h1 id="todstwd" style="color:#006600">Take Our Daughters and Sons To Work Day - 4-28-2011</h1>');
						}
					}
				});
			} //end if(window.location.href.search(/course_view|course_list/)!==-1){
		} //end if(typeof context !== 'undefined'){
	} //end Drupal.behaviors.testBehavior=function(context){
	/* ########## end Add JS ########## */
JAVASCRIPT;
	drupal_add_js($todstwd_script, 'inline');
}
?&gt;

Here’s a link:
Making Tracks | Organizational Development and Training ODT.

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

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

Drupal6 VBO Open all Edit Links Bookmarklet

I needed to edit some custom content in a large number of nodes and while Views Bulk Operations (VBO) allowed me to filter down to those nodes there isn’t a custom operation to edit text in custom fields. Not that I would expect there to be one.
So I wanted to open all the edit links in one fell swoop. I have a bookmarklet: ‘Open Selected Links’ which would work OK if the edit link was the only link for each node but it’s not. So I thought Why don’t I update the script to use just the edit links.
And so I did.
The working script is all JS with one line of jQuery.
The line in question previously set the available links using

dl = document.links;

and now uses:

dl = $('#views-bulk-operations-form--1 .views-field-edit-node a');

Which is a collection of all the edit links on the page.

I could probably keep in the old default and prompt the user to use just the edit links but quick and dirty is the name of the game right now.
(more…)