-
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.
-
Programmatically displaying an image using an imagecache preset
The following illustrates one way to render an image using an Imagecache preset. Naturally, if your imagefield has multiple uploads you could wrap this in a foreach.
1234$path = $node->field_pgallery_images[0]['filepath'];$alt = $node->field_pgallery_images[0]['data']['alt'];$title = $node->field_pgallery_images[0]['data']['title'];print theme('imagecache', ‘presetname’, $node->field_pgallery_images[0]['filepath'], $alt, $title, $attributes); -
Custom Voting API Calculation
The Voting API is really nice when used in conjunction with a module like Fivestar. It takes all of the complexity out of setting up a voting/rating system. On occasion, however, you need it to do some things that aren’t built in. Using the VotingAPI’s API, I’ll show you how to trigger a function (send an email, write a message to the screen, etc.) when a piece of content receives a fifth vote of four stars or greater. That is, a node may have four votes of five stars, and two votes of two stars. As soon as the next greater-than-four vote goes in, we want to do something about it. In our case, the client wants to receive an email when a node is popular, and has had five four-star-or-greater votes.
-
Show Menu Description Field in Node Add/Edit Form
Many components in Drupal have an optional title or description field. This text usually displays when a user hovers over an item. We had an interesting request: a client wanted to have a box that displayed text which would change every time you roll over a menu item. This can be accomplished using just a small amount of jQuery, and Drupal’s built-in menu handling. The menu system in Drupal can handle descriptions on each menu item, however you have to do this through the menu admin. The problem is that the client needed to be able to edit these descriptions at will.
-
Views Taxonomy: Get Terms with Associated Nodes
This example serves as both an example of how to alter a Views2 query, as well as how to use the get_terms_by_count() function I’ve written.
Unfortunately there is not (at present) a Views2 taxonomy filter that lets you “Get only terms with at least X associated nodes.” We had a client request that terms without associated nodes be hidden. This was actually more complex than it sounds, but the solution led me to a whole new level of Views2 understanding. Views2 has a hook called hook_views_query_alter() that lets you alter a Views2 query before it is executed. This is exactly what we needed to do in order to only pull terms with associated nodes. Specifically, we needed to add an additional WHERE clause to the query.
-
Rebuilding the Drupal Navigation Menu (Automatically)
The following steps will get you a completely rebuilt Navigation menu.
- Clear your cache
- Backup your database
- In the database, remove any records in {menu_links} where menu_name = navigation
- Create a file somewhere in your site’s directory structure
- Add this code to that file and then save it
123456789<?php//uncomment the next two lines to automatically use the admin user (not advised)//global $user;//$user-?-->uid = 1;require_once './includes/bootstrap.inc';drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);menu_rebuild();?> - Tell Drupal to ignore this file using this method.
- Browse to the file in your web browser (it will run quickly, with no indication of completion). Wait for the page to stop loading, then check your navigation menu settings in the admin.
- When finished, delete or make-inaccessible the PHP file you just made!
-
$user Conditionals
This is one example of performing an action based on whether or not the current user belongs to a certain role or roles. Note that the admin user (UID #1) matches as well.
12345678global $user;$roles = array_values($user->roles);if (in_array('admin', $roles)) || in_array('developer', $roles)) || $user->uid == 1) {print $content;}else{header("location:/not-allowed");}Note: You may want to try using drupal_goto() (or drupal_access_denied() in this case) instead of a header redirect.