Drupal Articles

Import form values from one form to another via AJAX

Drupal 7's new and improved Form API makes using AJAX a breeze. It enables you to add, replace, or remove form elements via AJAX without ever really having to get your hands dirty.

I recently had the opportunity to give it a whirl. Here's a the deal:
I have content type Resume and Profile2 type Job Seeker Profile. I'd like my users to be able to create up to 5 resumes, and I'd like them to have the option of importing information from their profile into their brand-spanking new resume. So, I'm going to create an 'Import from profile' button that will populate the resume fields with profile field values via AJAX.

Note: It is possible to accomplish something like this by setting default values via a token. However, in this case, I'd like to be able to import things like field collections, addresses, and fields with unlimited cardinality. Here's the code that accomplished it!

Creating a full screen, resizable slideshow in Drupal

The Views Slideshow module makes the process of creating a slideshow in Drupal extremely easy, but I've always found one important feature to be missing: a fullscreen setting.

Here is a script that take a slide image (or any HTML element for that matter) and set it to be the size of the viewer's browser window (preserving the aspect ratio). This works with single elements or a set of elements. It's particularly useful if you'd like to set an image to be the background of a given page-- this will ensure that the image always covers the entire viewport.

I'd recommend dropping this code into a resize.js file located in your Drupal theme's folder. From there, just add the filename to your theme's .info file, and it will be loaded into every page of your site (if that's what you're going for).

Theming Faceted Search result

The Faceted Search module produces a result set that looks almost exactly like Drupal's core search results. However, it does not use the search-results.tpl.php file. Rather, it uses it's own theming function: theme_faceted_search_ui_search_item().

You can, of course, set your own theme function to override this.

Here's a quick example of how I modified the way that users are displayed in the results:

Drupal - Conditionally hiding CCK fieldgroups

Conditional Fields is a great Drupal module for conditionally hiding CCK fields.

Unfortunately, it can't hide CCK fieldgroups. It's also not ideal if you're concerned about security— it simply hides fields; it doesn't deny access. A recent project of mine required that I conditionally deny access to field groups, so I decided to implement a programmatic solution.

In this case, I needed to deny access to specific fieldgroups in content type 'company' based on:

  • The role of the acting user
  • The value of the field_company_type CCK field

The downside to this approach is that you must save and return to the node edit page before seeing a change in fieldgroup visibility.

Here's my solution:

Drupal - overriding filefield theming

Drupal's default 'Generic File' format can be a bit ugly. Luckily, it's not too hard to override. If you'd like to change the default filefield theming for 'Generic File', try using the theme_filefield_item() function in your template.php file.

Note: You may also be interested in checking out my post on customizing the filefield format in views.

In the example below, I overrode the default theming for all cck fieldfields belonging to node type 'publication'.

Using imagecache with the Image or Embedded Media Field modules

Actually, there is a way to use imagecache presets with the image module without patches. It involves creating a View that manually applies the imagecache preset to the image field (not imagefield). You'll need views customfield.

Create a view and add the relationships "Image:File" and "Image:Node." Then add fields "File:path," "Node->title," and "Customfield: PHP Code." Add this to your php customfield script:

//print var_export($data, TRUE);  //reveals all available variables

Add address field to Signup form, save to Content Profile

Alright, so this a bit nuanced, but I thought that I'd share it anyway. Maybe this snippet will help someone somewhere.


I'm using the Signup module to permit users to signup for events, but I'd like them to be able to enter more than the standard name and phone number. I'd like my users to be able to provide their address.

Beyond that, if the user has already provided their address on their user profile (generated using Content Profile and the Location CCK module), then I'd like the fields to prepopulate.

Furthermore (we're getting crazy here), if they enter a new address on the signup form, I'd like them to be able to save that address to their profile.


Actually, with the power of the Forms API, this becomes an easy task.

Drupal: dealing with nested children that share their parent's path

Let's say that you'd like to create a nested menu in which one of the children links to the same path as its parent. Something like:

  •  people/friends
    • people/friends
    • people/family
    • people/neighbors

This works in theory-- the menu items will take you to the correct page. But if you're relying on Drupal to correctly set the active trail and expand the correct sub menus, you'll have to use a bit of a workaround. You're basically going to create dummy nodes that will redirect to the desired page rather than creating two menu links that have the exact same path. The parent (in menu) will redirect to the child's node.

Drupal: Add body classes based on user permissions

This one is a little bit more esoteric. I found the need change the page style (via CSS) based on whether the current user had permission to update the current node. Drupal preprocess_page to the rescue!

Just add this snippet to the yourTheme_preprocess_page function in your template.php file. It will add body classes (e.g., user-node-update, user-node-delete) to your page depending on the current user's permission to access the node being viewed.