-
Quick Tip: Adding a Reset button to a Drupal form
Using hook_form_alter or hook_form_FORM_ID_alter one can easily add a “Reset” button to a Drupal form.
In this example we’re actually trying to reset anything the user typed after the form loaded. This will not remove the default values.
Here’s a simple example (Drupal 6)
1234567891011121314/*** Implementation of hook_form_alter().*/function mymodule_form_alter(&$form, &$form_state, $form_id) {switch ($form_id) {case 'myform':$form['buttons']['reset_button'] = array('#type' => 'markup','#value' => '<input class="form-button" type="reset" value="Reset">','#weight' => 2000,);break;}}It may not be appropriate for you to put your new reset element into the buttons array. Use dpm() (part of the Devel module) to show what $form looks like. If you don’t understand this, and the code above isn’t working for you, you may try $form[‘reset_button’] = array….
Adjust the value of #weight to move the button around the form. dpm($form) will show you weights (if any) of existing elements so you can make educated decisions about the weight of your new field.
One last note about the ‘#type’ => ‘markup’ line: this is not a requirement, but I like to include it for clarity.
UPDATE:
Here’s what the field might look like for Drupal 7:
1234$form['reset'] = array('#type' => 'markup','#markup' => '<input class="form-submit" type="reset" value="Reset">',); -
Views Exposed Filter: Terms for current language only
I’ve been working on a multilingual site that has a product finder. We have 3 exposed filters that allow a user to select an Activity, IP Category, or Industry. All of these filters are Taxonomy Term filters. Some of the terms throughout those vocabularies have a specific language set, and should only show when that language is active.
The first step is to make sure the output (Views results) only shows products where the node language matches the current language. This is easy using the built in Node translation: Language = Current user’s language filter.
-
Proximity by City or Zip Code in Drupal 6 with Location and Views
The location module for Drupal 6 is a robust module. On most projects, it gives us 100 percent of what we need. Proximity searching based on zip code is built-in and fairly painless to setup. I’ve recently been tasked to also allow proximity searching based on City. As you might imagine, the request is very similar. Both methods require the use of latitude and longitude (decimals). The difference is that instead of querying the database for latitude and longitude based on zip code (it’s usually termed postal code in Views, Location, etc.), we’re asking for the coordinates of a city. You’ll find that many cities have multiple zip codes, each of which is a row in the location module’s zipcodes database table. In this example, I’m not giving a real honest attempt at solving this issue, but rather I just return the first coordinate that matches the city.
-
Webform for Campaign Monitor
Webform is an amazing module. Thankfully, you can extend it to make it even more helpful. We’ve had a lot of clients that offer a “Join Our Mailing List” type of functionality on their sites. These forms are typically one or two fields (email and name, usually) and are for anonymous users. We use Campaign Monitor for most clients.
There is a Campaign Monitor module, but I prefer the following method, for various reasons.
-
Disable Specific States in Ubercart
Here’s a quick example that illustrates how to remove a few State/Province options from the billing and shipping panes of the Ubercart checkout form, as well as the order edit/create form. Please understand there are other ways to do this (like altering the united_states_840_1.cif file and re-importing). The hook_form_alter() method seems less permanent, so I favor it.
123456789101112131415161718192021/*** Implementation of hook_form_alter().*/function mymodule_form_alter(&$form, &$form_state, $form_id) {switch ($form_id) {case 'uc_cart_checkout_form':// Disable some State/Province values in the checkout formunset($form['panes']['delivery']['delivery_zone']['#options'][21]); // Hawaiiunset($form['panes']['delivery']['delivery_zone']['#options'][49]); // Oregonunset($form['panes']['billing']['billing_zone']['#options'][21]); // Hawaiiunset($form['panes']['billing']['billing_zone']['#options'][49]); // Oregonbreak;case 'uc_order_edit_form':// Disable some State/Province values in the order edit/create formunset($form['ship_to']['delivery_zone']['#options'][21]); // Hawaiiunset($form['ship_to']['delivery_zone']['#options'][49]); // Oregonunset($form['bill_to']['billing_zone']['#options'][21]); // Hawaiiunset($form['bill_to']['billing_zone']['#options'][49]); // Oregonbreak;}} -
Get the Value of the Cheapest Option for an Ubercart Product
This is just a quick code example. Imaging you have an Ubercart attribute that has several options and you’d like to show an “As low as $X.XX” price on the frontend. Here’s a simple solution. Note that if you wanted the lowest price across ALL attributes you could loop through each attribute too (instead of just looking at attributes[1]).
123456789if ($node->attributes[1]->options) {// Get lowest price of all "Units" attribute options$prices = array();foreach ($node->attributes[1]->options as $opt) {$prices[] = $opt->price;}$output .= t('As low as !price', array('!price' => uc_currency_format(min($prices))));} -
Lightbox2 Slideshows with Multiple-value Imagefields
This is a pretty simple example that illustrates the use of Lightbox2’s slideshow feature. In the example, we have an imagefield that allows an unlimited number of images. We only want to render an imagecache version of the first image that, when clicked, will provide a slideshow with the rest of the images in the field. In the example, our field is called “field_product_images”.
The two files I’m showing are both within the theme for the site.
There are a few other options for achieving the same end result, but this seemed easier to explain and execute. Depending on your needs, you may need to make this happen at a deeper level (module) or in a field template/function override. If you have another approach, please post a comment!
12345678910111213141516171819202122232425262728/*** Override or insert variables into the node templates.** @param $vars* An array of variables to pass to the theme template.* @param $hook* The name of the template being rendered ("node" in this case.)*/function yourthemename_preprocess_node(&$vars, $hook) {if ($vars['node']->type == 'product') {// Loop through images for the product. Show the first image, and build the lightbox links to the other// images (more efficient than rendering the entire image and hiding it)// The field is configured to be a lightbox slideshow via Display Settings on the content type.if ($vars['node']->field_product_images[0]) {$vars['image_html'] = '';foreach($vars['node']->field_product_images as $key => $item) {if ($key == 0) {$vars['image_html'] .= $item['view'];}else {$vars['image_html'] .= '<div class="for-lightbox" style="display:none;">';$vars['image_html'] .= "<a href='/{$item['filepath']}' rel='lightshow[field_product_images][{$item['data']['title']}]'>Img</a>";$vars['image_html'] .= '</div>';}}}}}12345<?php if (isset($image_html)): ?><div class="field product-thumb"><?php print $image_html; ?></div><?php endif; ?>
12345// Because we may be showing multiple slideshows on a page (the store view// renders many products) let's add a unique identifier to the end of the group$orig_group = 'lightshow[field_product_images]';$new_group = "lightshow[field_product_images_{$vars['node']->nid}]";$vars['image_html'] = str_replace($orig_group, $new_group, $vars['image_html']); -
Get a List of Top-level Taxonomy Terms
Quick Tip: Getting a list of taxonomy terms is easy using a view (type: Taxonomy). Trying to get only terms at the highest level is a bit trickier. One method I came up with is to add a Taxonomy: Parent term argument on the view. The key is to tell the argument to Provide default argument of PHP Code and set the value to return 0;
The result is that only top-level terms (terms without parents) are returned.
-
Showing a nodeasblock block as full content instead of teaser
By default, the Node As Block module displays a node in teaser mode. If you wish to show the full content, add the following to your template.php file. (note I’ve also added an edit link for good measure)
123456789function mytheme_preprocess_nodeasblock(&$vars) {$node = $vars['node'];$vars['content'] = node_view(node_load($node->nid), FALSE, TRUE, TRUE);// Add an edit linkif (node_access('update', $node)) {$vars['edit_link'] = l('['. t('edit') .']', 'node/'. $node->nid .'/edit', array('title' => t('Edit')));}} -
Sending webform submissions to email based on value of field
We recently did a site for a client that allowed patients to submit a webform to request an appointment. The user could choose their physician and based on their choice, the submission would be emailed to the chosen doctor’s secretary. I originally thought about using the key part of the key|value pair for the select list to store the secretary’s email address, with the doctor’s name. This wouldn’t work, however, because the key could not contain standard email address characters. The solution involved isn’t exactly dynamic, but for a select list that won’t change much (the case here), it’s a fairly elegant solution. Here is the select list and associated code. I created this with the webform UI using a simple select list webform field.