![]() * Provide a description to each option in the element Writing our custom callback to take advantage of this, we end up with: That shared key is the glue between each option and its description. Each option in the #options array shares a key with the items in the #options_descriptions array. Instead, it's a custom convention we'll use that our callback function will expect. ![]() As mentioned earlier, #options_descriptions isn't a standard FAPI option. Custom #after_build callback with #options_descriptionsĪll functions defined in #after_build are expected to return the $element argument after we've made our changes. We'll get to that shortly, but first let's look at our callback function. Some of you might have noticed the #options_descriptions key, which isn't in the FAPI documentation. This will call a function named _option_descriptions, and pass it arguments for the $element and the $form_state that we can make alterations on. $form = array ( '#type' => 'checkboxes', '#title' => t ( 'My Checkboxes' ), '#options' => array ( 'option_1' => 'Option 1', 'option_2' => 'Option 2', ), '#options_descriptions' => array ( 'option_1' => 'Description 1', 'option_2' => 'Description 2', ), '#default_value' => variable_get ( 'form_checkboxes', array ( ) ), '#after_build' => array ( '_option_descriptions' ), ) This can be done either through a new form element definition (as shown below), or by adding new elements to #after_build through hook_form_alter ( ). To get started, add some #after_build callbacks to your element. That means in order to alter those specific elements, we'll need to access the form array after the initial build. ![]() In the case of checkboxes and radios, the FAPI elements for each individual checkbox/radio are inserted by Drupal during the initial form build. The FAPI documentation describes #after_build pretty well: “An array of function names which will be called after the form or element is built.” This allows us to make further alterations to our form elements after they're processed by Drupal, but before they're rendered to HTML. ![]() I discovered the FAPI's #after_build callbacks, which turned out to be just the solution I needed. After a lot of looking, there didn't seem to be a way that allowed for passing descriptions into each item in the #options array that is expected. While the Form API in Drupal 7 has a #description attribute, for checkboxes and radios it applies that as text for the entire group. Recently, I was creating a form that provided a list of options as checkboxes and needed to include helper text for each individual checkbox. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |