I want to add class "row" to my "form-group" for an input field. Unfortunately with out overriding theme hooks, there's currently no way to get this done easily.

I added a little snippet into bootstrap_form_element() which will allow fields to add attributes to their parent wrappers.

It's a pretty small snippet, but adds a lot of extra flexibility for those who need it.

PS. Thx for #field_prefix and #field_suffix :) Helped me with another problem.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

markhalliwell’s picture

Status: Needs review » Postponed (maintainer needs more info)

I do not see how this is useful, where else is #parent_attributes being used? This is definitely not a Drupal Best Practices™ method. What exactly is it you are attempting to accomplish here? Please provide code examples.

j0rd’s picture

form elements in grid layout and adding classes to the parent form-group, which I needed for some JS widgets.

Drupal is not build really well to handle the flexibility of styling in theme frameworks like bootstrap. So you can nurf the theme for Drupal, or you can extend Drupal to handle more options of bootstrap.

Here's an example of the form I was able to create using this method. With out this method, a bunch of theme overwrites would have had to of been written.

Really the patch just allows for some added (and optional) flexibility.

As for where "#parent_attributes" is being used, it's just used to initialize the $attributes variable, which you then use in the parent group.

You can see that here

  // ...
  $description = FALSE;                                                                                                                                       
  $tooltip = FALSE;  
  // Convert some descriptions to tooltips.
  // @see bootstrap_tooltip_descriptions setting in _bootstrap_settings_form()
  if (!empty($element['#description'])) {
    $description = $element['#description'];
    if (theme_get_setting('bootstrap_tooltip_enabled') && theme_get_setting('bootstrap_tooltip_descriptions') && $description === strip_tags($description) && strlen($description) <= 200) { 
      $tooltip = TRUE;  
      $attributes['data-toggle'] = 'tooltip'; 
      $attributes['title'] = $description; 
    } 
  }

 $output = '<div' . drupal_attributes($attributes) . '>' . "\n";  

 // ....

So really the patch just allows me to extend the parent div how ever I want in form API, instead of having to override it for each specific case (of which there are lots)

Example of my use in code:

  // setting class "row" and "star-rating" via #parent_attributes
  $form['review']['rating'] = array( 
    '#type' => 'numberfield', 
    '#title' => t('Overall Rating'),  
    '#title_display' => 'none',            
    '#required' => TRUE, 
    '#parent_attributes' => array(
      'class' => array('star-rating', 'row'),   
    ),     
    '#field_prefix' => '<div class="col-md-2 left"><p><b>'. t('Overall Rating') .'</b></p></div><div class="col-md-8 text-center"><div class="well well-sm">',
    '#field_suffix' => '</div></div>',   
    '#attributes' => array(
      'class' => array('rating'), 
      'data-min' => 1, 
      'data-max' => 5,
    ),  
  );  

markhalliwell’s picture

Title: Add flexiblity by allowing form fields, to add attributes to form_element parent wrappers. » Allow preprocessed wrapper attributes on bootstrap_form_element()
Category: Feature request » Bug report
Status: Postponed (maintainer needs more info) » Fixed
FileSize
3.65 KB

Ahh. Gotcha. The whole "parent" thing is what threw me, sorry for the misunderstanding. You are correct, however it should really be called #wrapper_attributes.

Committed 3b7a855 to 7.x-3.x:

Issue #2189117 by Mark Carver | j0rd: Allow preprocessed wrapper attributes on bootstrap_form_element().

j0rd’s picture

(y) thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

  • Commit 3b7a855 on 7.x-3.x, 8.x-3.x by Mark Carver:
    Issue #2189117 by Mark Carver | j0rd: Allow preprocessed wrapper...
ryan.armstrong’s picture

Version: 7.x-3.x-dev » 8.x-3.x-dev
Status: Closed (fixed) » Needs review

Moving this to the 8.x-3.x branch as it has been committed to that branch. Making sure the fix gets tested in the Drupal 8 release.

ryan.armstrong’s picture

Assigned: Unassigned » ryan.armstrong

Status: Needs review » Needs work

The last submitted patch, 3: form_element_wrapper_attributes-bootstrap-2189117-3.patch, failed testing.

  • Mark Carver committed 3b7a855 on 8.x-3.x.x
    Issue #2189117 by Mark Carver | j0rd: Allow preprocessed wrapper...
markhalliwell’s picture

Version: 8.x-3.x-dev » 7.x-3.x-dev
Assigned: ryan.armstrong » Unassigned
Status: Needs work » Closed (fixed)

I'm just moving this back to 7.x. If there needs to be any re-evaluation for 8.x, create a new issue.