diff --git a/includes/webform.components.inc b/includes/webform.components.inc index c45f2bd..9ee6c38 100644 --- a/includes/webform.components.inc +++ b/includes/webform.components.inc @@ -453,6 +453,23 @@ function webform_component_edit_form($form, $form_state, $node, $component, $clo ); } + $form['display']['wrapper_classes'] = array( + '#type' => 'textfield', + '#title' => t('Wrapper CSS classes'), + '#default_value' => isset($component['extra']['wrapper_classes']) ? $component['extra']['wrapper_classes'] : '', + '#description' => t('Apply a class to the wrapper around both the field and its label. Separate multiple by spaces.'), + '#weight' => 50, + '#parents' => array('extra', 'wrapper_classes'), + ); + $form['display']['css_classes'] = array( + '#type' => 'textfield', + '#title' => t('CSS classes'), + '#default_value' => isset($component['extra']['css_classes']) ? $component['extra']['css_classes'] : '', + '#description' => t('Apply a class to the field. Separate multiple by spaces.'), + '#weight' => 51, + '#parents' => array('extra', 'css_classes'), + ); + // Validation settings. $form['validation'] = array( '#type' => 'fieldset', diff --git a/webform.module b/webform.module index 89428f8..6a6f2e1 100644 --- a/webform.module +++ b/webform.module @@ -2412,6 +2412,9 @@ function _webform_client_form_add_component($node, $component, $component_value, // Ensure the component is added as a property. $display_element['#webform_component'] = $component; + // Add custom CSS classes to the field and wrapper. + _webform_component_classes($element, $component); + // Allow modules to modify a "display only" webform component. drupal_alter('webform_component_display', $display_element, $component); @@ -2445,6 +2448,9 @@ function _webform_client_form_add_component($node, $component, $component_value, $element['#webform_private'] = $component['extra']['private']; } + // Add custom CSS classes to the field and wrapper. + _webform_component_classes($element, $component); + // Allow modules to modify a webform component that is going to be render in a form. drupal_alter('webform_component_render', $element, $component); @@ -2998,6 +3004,7 @@ function theme_webform_element($variables) { $nested_level = $element['#parents'][0] == 'submitted' ? 1 : 0; $parents = str_replace('_', '-', implode('--', array_slice($element['#parents'], $nested_level))); + $wrapper_attributes = isset($element['#wrapper_attributes']) ? $element['#wrapper_attributes'] : array('class' => array()); $wrapper_classes = array( 'form-item', 'webform-component', @@ -3006,7 +3013,9 @@ function theme_webform_element($variables) { if (isset($element['#title_display']) && strcmp($element['#title_display'], 'inline') === 0) { $wrapper_classes[] = 'webform-container-inline'; } - $output = '
' . "\n"; + $wrapper_attributes['class'] = array_merge($wrapper_classes, $wrapper_attributes['class']); + $wrapper_attributes['id'] = 'webform-component-' . $parents; + $output = '
' . "\n"; // If #title_display is none, set it to invisible instead - none only used if // we have no title at all to use. @@ -3673,6 +3682,20 @@ function webform_conditional_expand($element) { } /** + * Add class and wrapper class attributes to an element. + */ +function _webform_component_classes(&$element, $component) { + if (isset($component['extra']['css_classes']) && drupal_strlen($component['extra']['css_classes'])) { + $element['#attributes']['class'] = isset($element['#attributes']['class']) ? $element['#attributes']['class'] : array(); + $element['#attributes']['class'] = array_merge($element['#attributes']['class'], explode(' ' , $component['extra']['css_classes'])); + } + if (isset($component['extra']['wrapper_classes']) && drupal_strlen($component['extra']['wrapper_classes'])) { + $element['#wrapper_attributes']['class'] = isset($element['#wrapper_attributes']['class']) ? $element['#wrapper_attributes']['class'] : array(); + $element['#wrapper_attributes']['class'] = array_merge($element['#wrapper_attributes']['class'], explode(' ' , $component['extra']['wrapper_classes'])); + } +} + +/** * Disable the Drupal page cache. */ function webform_disable_page_cache() {