-
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 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.
-
Multiple Submit Buttons on a Webform
I’ve spent a lot of time working with hook_form_alter() functions to modify forms, but the other day I was stumped by a simple problem. I needed to have two submit buttons on a form. By default, in the latest branch of Webform, you can change the text label on the default submit button for a webform. If you take a look at the screenshot on the right, you’ll see what my demo form looks like. The “Request More Information” button is the default submit button. The “Refer a Friend” button is the one I’m showing how to add via hook_form_alter().
-
Hiding fieldsets in a form using hook_form_alter()
The following examples shows how to hide fields/fieldsets in forms using hook_form_alter(). I suggest using the devel module to determine how to target your field(s) or fieldset(s). You can add a dpm($form) to the function during development and testing.
12345678910111213<?phpfunction mymodule_form_alter(&$form, $form_state, $form_id) {global $user;// "hide" a few fieldsets for non-admins// (the lead developer only wants UID 1 to see these fields; I suggest doing it by role though!)// Note that you can also check out the $form_id variable for a different way to which form this isif ($form['#id'] == 'node-form' && $user->uid != 1) {$form['author']['#access'] = FALSE;$form['revision_information']['#access'] = FALSE;}}?>Note that this will prevent the value from being submitted. If you need the value to be included in the submission, you’re better off doing something like this: http://stackoverflow.com/questions/4461237/how-to-hide-a-field-in-a-drupal-form/5308924#5308924
-
Changing the Title of a Node Add/Edit Page
The following example shows one way of changing the title on add/edit pages for a specified content type. We’ll accomplish this using hook_form_alter() in a custom module.
123456789<?phpfunction mymodule_form_alter(&$form, $form_state, $form_id) {if (isset($form['type']) && isset($form['#node'])) {if ($form_id == 'mycontenttype_node_form') {drupal_set_title('Request a Sample Note');}}}?>I suggest using the devel module dpm() function, or just php print-ing the $form_id variable at the top of your hook_form_alter() function. This will tell you which form_id you need to target in your conditional.