Search code examples
drupal-7twitter-bootstrap-3

How to add a class to entityform inputs


I have an entityform block and I want to add bootstrap classes to its input! How can I do this and what files should I edit?

I'm using latest version of entityform and entity block on drupal.org

thank you


Solution

  • You need to add these functions to your template.php file inside your theme folder (replace THEME_NAME with the actual name of your theme):

    function THEME_NAME_textfield($variables) {
      $element = $variables['element'];
      $element['#attributes']['type'] = 'text';
      element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));
      _form_set_class($element, array('form-text'));
    
      $element['#attributes']['class'][] = 'form-control';
    
      $extra = '';
      if ($element['#autocomplete_path'] && drupal_valid_path($element['#autocomplete_path'])) {
        drupal_add_library('system', 'drupal.autocomplete');
        $element['#attributes']['class'][] = 'form-autocomplete';
    
        $attributes = array();
        $attributes['type'] = 'hidden';
        $attributes['id'] = $element['#attributes']['id'] . '-autocomplete';
        $attributes['value'] = url($element['#autocomplete_path'], array('absolute' => TRUE));
        $attributes['disabled'] = 'disabled';
        $attributes['class'][] = 'autocomplete';
        $extra = '<input' . drupal_attributes($attributes) . ' />';
      }
    
      $output = "<div class='input-wrapper'>";
      $output .= '<input' . drupal_attributes($element['#attributes']) . ' />';
      $output .= "</div>";
      return $output . $extra;
    }
    
    
    function THEME_NAME_button($variables) {
      $element = $variables['element'];
      $element['#attributes']['type'] = 'submit';
      element_set_attributes($element, array('id', 'name', 'value'));
    
      $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
      $element['#attributes']['class'][] = 'btn btn-default';
      if (!empty($element['#attributes']['disabled'])) {
        $element['#attributes']['class'][] = 'form-button-disabled';
      }
      $value = $element['#attributes']["value"];
      if( isset($element['#attributes']["button-type"]) && $element['#attributes']["button-type"] == "search" ) {
        $value = $element['#attributes']["icon"];
        unset($element['#attributes']["icon"]);
      }  
      return '<button' . drupal_attributes($element['#attributes']) . '>' . $value . '</button>';
    }
    
    
    function THEME_NAME_select($variables) {
      $element = $variables['element'];
      element_set_attributes($element, array('id', 'name', 'size'));
      _form_set_class($element, array('form-select', 'form-control'));
    
      return '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
    }
    

    These functions will add the classes you need for the button, textfield and select input types. Also you will need to include the bootstrap files in your theme.