diff --git a/includes/webform.components.inc b/includes/webform.components.inc index 250b868..4455edc 100644 --- a/includes/webform.components.inc +++ b/includes/webform.components.inc @@ -454,6 +454,27 @@ function webform_component_edit_form($form, $form_state, $node, $component, $clo ); } + if (webform_component_feature($component['type'], 'wrapper_classes')) { + $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'), + ); + } + if (webform_component_feature($component['type'], 'css_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', @@ -919,6 +940,8 @@ function webform_component_feature($type, $feature) { 'group' => FALSE, 'attachment' => FALSE, 'private' => TRUE, + 'wrapper_classes' => TRUE, + 'css_classes' => TRUE, ); return isset($components[$type]['features'][$feature]) ? $components[$type]['features'][$feature] : !empty($defaults[$feature]); } diff --git a/webform.module b/webform.module index d09a59f..a1b52fe 100644 --- a/webform.module +++ b/webform.module @@ -733,6 +733,7 @@ function webform_webform_component_info() { 'description' => t('Presents month, day, and year fields.'), 'features' => array( 'conditional' => FALSE, + 'css_classes' => FALSE, ), 'file' => 'components/date.inc', ), @@ -755,6 +756,7 @@ function webform_webform_component_info() { 'conditional' => FALSE, 'group' => TRUE, 'title_inline' => FALSE, + 'wrapper_classes' => FALSE, ), 'file' => 'components/fieldset.inc', ), @@ -765,6 +767,7 @@ function webform_webform_component_info() { 'conditional' => FALSE, 'default_value' => FALSE, 'title_inline' => FALSE, + 'css_classes' => FALSE, ), 'file' => 'components/grid.inc', ), @@ -779,6 +782,8 @@ function webform_webform_component_info() { 'email_name' => TRUE, 'title_display' => FALSE, 'private' => FALSE, + 'wrapper_classes' => FALSE, + 'css_classes' => FALSE, ), ), 'markup' => array( @@ -793,6 +798,7 @@ function webform_webform_component_info() { 'conditional' => FALSE, 'title_display' => FALSE, 'private' => FALSE, + 'css_classes' => FALSE, ), 'file' => 'components/markup.inc', ), @@ -813,6 +819,8 @@ function webform_webform_component_info() { 'private' => FALSE, 'required' => FALSE, 'title_display' => FALSE, + 'wrapper_classes' => FALSE, + 'css_classes' => FALSE, ), 'file' => 'components/pagebreak.inc', ), @@ -849,6 +857,7 @@ function webform_webform_component_info() { 'description' => t('Presents the user with hour and minute fields. Optional am/pm fields.'), 'features' => array( 'conditional' => FALSE, + 'css_classes' => FALSE, ), 'file' => 'components/time.inc', ), @@ -2162,6 +2171,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); @@ -2198,6 +2210,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); @@ -2729,6 +2744,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', @@ -2737,7 +2753,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. @@ -3600,6 +3618,20 @@ function webform_component_implements($type, $callback) { } /** + * 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() {