-
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.
-
Using hook_form_alter() and #after_build to set maxlength on emvideo title field
A fellow Drupal user posted an issue to the Embedded Media Field project. The user was trying to set the maxlength from 35 characters to some other number for the ‘title’ field within each embedded video widget. I faced the same issue with a site. The client wanted to be able to add more lengthy captions to each video they uploaded. I added the embedded video field (YouTube) as an “unlimited” video field.
The solution involves using hook_form_alter() and the #after_build property.
1234567891011121314151617181920212223242526272829/*** Implementation of hook_form_alter().*/function mymodule_form_alter(&$form, &$form_state, $form_id) {switch ($form_id) {case 'vgallery_node_form':$form['#after_build'][] = '_vgallery_after_build';break;}}/*** Custom after_build callback handler for the video gallery node form*/function _vgallery_after_build($form, &$form_state) {_vgallery_set_title_maxlength($form['field_vgallery_videos']);return $form;}/*** Set the maxlength on the title field of a video gallery emvideo element*/function _vgallery_set_title_maxlength(&$elements) {foreach (element_children($elements) as $key) {if (isset($elements[$key]['emvideo']['title'])) {$elements[$key]['emvideo']['title']['#maxlength'] = 255;}}} -
Moving a field into a (different) group/fieldset using hook_form_alter()
The following demonstrates how to move a field into a different fieldset within a form using hook_form_alter() and a special #after_build form property.
123456789101112131415161718function mymodule_form_alter(&$form, $form_state, $form_id) {if ($form_id == 'status_node_form' && $form['nid']['#value']) {$form['send_notification'] = array('#type' => 'submit','#value' => t('Send Notification'),'#submit' => array('mymodule_notify_submit'),);$form['#after_build'][] = 'mymodule_notification_afterbuild';}}function mymodule_notification_afterbuild($form, $form_state) {// Add the notifications button to the "Notifications" field group// (defined in the content type field manager)$form['group_notifications']['send_notification'] = $form['send_notification'];unset($form['send_notification']);return $form;} -
Show a timestamp as a date in the Drupal user’s timezone
12345$ts = 1288639479;$local_zone = date_default_timezone_name(TRUE);$newdate = date_make_date($ts, 'UTC', DATE_UNIX);date_timezone_set($newdate, timezone_open($local_zone));print date_format_date($newdate, 'custom', 'm/d/Y H:i'); -
db_query() and db_placeholders() example #1
Have you ever wondered how to properly build a query like this in Drupal:
1SELECT nid, type, title FROM node n WHERE n.type IN('page','story');This requires the use of db_placeholders() to create the placeholder ‘ ‘, ‘ ‘, etc.
1$result = db_query('SELECT nid, type, title FROM {node} n WHERE n.type IN(' . db_placeholders($node_types, 'text') . ')', $node_types);where $node_types is an array of node types.
-
Automatically create a Drupal node when a user registers
Sometimes projects come up that have requirements that cannot be solved with an out-of-the-box Drupal install. A project I worked on some time ago required that I store a node every time a user registered for an account. The details of the project are a little fuzzy for me now, so I’ll just skip right to the example.
1234567891011121314function mymodule_user($op, &$edit, &$account, $category = NULL){switch($op){case 'insert':// Automatically create the note settings (content profile) node for this new user$newnode = new stdClass();$newnode->title = "Note settings user {$account->uid}";$newnode->uid = $account->uid;$newnode->type = 'note_settings';$newnode->status = 1;$newnode = node_submit($newnode);node_save($newnode);break; //insert}}This example shows how simple it is to programmatically create a node, and also shows the proper way to hook into the user system.
-
Make menu header a link to its first child link
We use menu block a lot to create sidebar menus with specific depths, starting points, etc. The following snippet shows how we can use jQuery to turn a normal <h2> tag into a link. The link’s destination will be the same as the first child link.
12var href = $('#block-menu_block-1 h2.title').next().find('li a').attr('href');$('#block-menu_block-1 h2.title').html($('<a></a>').html($('#block-menu_block-1 h2.title').html()).attr('href', href)); -
How to check if a jQuery plugin is loaded
The following snippet is one way to determine if a jQuery plugin is loaded an available. There are probably other ways to achieve the same behavior, however this one never seems to fail me.
1234if (jQuery().jcarousel) {// The plugin is here, do something with it.$('.jcarousel-enabled').jcarousel();}