• Development

    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.

     

  • Development

    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.

     

  • Development

    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.

  • Development

    Multiple Submit Buttons on a Webform

    multiple-webform-submitI’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().

  • Development

    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.

    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

  • Development

    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.

    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.