span classes for both field_prefix & field_suffix need to be changed to "input-group-addon" and wrapped in an "input-group" div.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

markhalliwell’s picture

Status: Needs review » Needs work

Sorry to do this, but I was in the middle of #2098175: Clean up files and now the structure has completely changed (to be easier for maintaining). Please re-roll your patch against the latest HEAD. Also, be sure to read the documentation in template.php to understand what has changed. Thanks!

brian_ak’s picture

No prob @mark. updated patch is attached.

brian_ak’s picture

Status: Needs work » Needs review
markhalliwell’s picture

Status: Needs review » Fixed

Thanks @brian_ak!

Committed 313bb6e to 7.x-3.x.

Committed fec0fa5 to 7.x-3.x.

I went ahead and changed some of the logic. One must now specify #input_group => TRUE for it to wrap #field_prefix and #field_suffix with .input-group-addon and the wrapping .input-group div. The reason for this logic change is because there are times when one will need to specify either the prefix or suffix, but not necessary need those wrappers (and subsequent styling). An example of this would be:

  $form['amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Dollar amount'),
    '#input_group' => TRUE,
    '#field_prefix' => '$',
    '#field_suffix' => '.00',
  );
markhalliwell’s picture

On a side note, if there are certain elements in [core] forms that you think would benefit from the new #input_group => TRUE, create new issues to alter the forms :)

brian_ak’s picture

Great, thanks Mark!

markhalliwell’s picture

Version: 7.x-3.x-dev » 7.x-3.0-rc1
heshanlk’s picture

Status: Fixed » Needs work

It make sense to add field settings alter to allow #input_group in field level. So reopening.

heshanlk’s picture

This little work around added an option to the number and text field(text fields don't have prefix/suffix tho) and you can choose to use it with input groups. Hope this will help.

I tried to add this code to alter.inc but it didn't works and it looks like hook_form_alter doesn't look for themes it'll just search for modules.

/**
 * Widget that can be applied to
 */
define('IG_FIELD_WIDGETS', serialize(array('text', 'number')));


/**
 * Implements hook_form_alter().
 */
function CUSTOM_MODULE_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  if (isset($form['instance']['widget']['type']['#value'])) {
    $type = $form['instance']['widget']['type']['#value'];
    if (in_array($type, unserialize(IG_FIELD_WIDGETS))) {
      $field = $form['#field'];
      $instance = field_info_instance($form['instance']['entity_type']['#value'], $form['instance']['field_name']['#value'], $form['instance']['bundle']['#value']);
      $form['instance'] += CUSTOM_MODULE_field_widget_settings_form($field, $instance);
    }
  }
}

/**
 * Implements hook_field_widget_info_alter().
 *
 * A list of settings needed by this module for widgets.
 */
function CUSTOM_MODULE_field_widget_info_alter(&$info) {
  $settings = array(
    'apply_input_group' => 0,
  );

  foreach (unserialize(IG_FIELD_WIDGETS) as $widget) {
    if (isset($info[$widget]['settings'])) {
      $info[$widget]['settings'] += $settings;
    }
  }
}

/**
 * Implements hook_field_widget_settings_form().
 */
function CUSTOM_MODULE_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];

  $form['input_group'] = array(
    '#type' => 'fieldset',
    '#title' => t('Bootstrap Input Group'),
    '#collapsed' => TRUE,
    '#collapsible' => TRUE,
    '#parents' => array('instance', 'widget', 'settings'),
  );
  $form['input_group']['apply_input_group'] = array(
    '#type' => 'checkbox',
    '#title' => t('Apply Bootstrap input group on this field'),
    '#default_value' => isset($settings['apply_input_group']) ? $settings['apply_input_group'] : FALSE,
  );
  return $form;
}

/**
 * Implements hook_field_widget_form_alter().
 */
function CUSTOM_MODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  $type = $context['instance']['widget']['type'];
  if (in_array($type, unserialize(IG_FIELD_WIDGETS))) {
    $settings = $context['instance']['widget']['settings'];
    if (!empty($settings['apply_input_group'])) {
      $element['value']['#input_group'] = TRUE;
    }
  }
}
markhalliwell’s picture

Category: task » bug
Status: Needs work » Fixed

It make sense to add field settings

This would make it a feature then (since this type of functionality didn't exist before). This issue was simply about fixing the existing #field_prefix and #field_suffix properties.

I tried to add this code to alter.inc but it didn't works and it looks like hook_form_alter doesn't look for themes it'll just search for modules.

Alters do work on themes, we have several form alters in Bootstrap already. The caveat though: only works if that theme is the one being used for that page request (ie: admin theme might be different). Regardless, I agree this functionality should probably be handled by a module anyhow, which is why I created https://drupal.org/project/bootstrap_ux, just for this type of thing.

heshanlk’s picture

@Mark Carver, that's great, I'll create a patch for the UX module.

heshanlk’s picture

Status: Fixed » Closed (fixed)

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