1

I have to override the standard field wrappers which add additional divs.

Drupal uses the following code, in field_multiple_value_form().

$field_elements += array(
  '#theme' => 'field_multiple_value_form',
  '#field_name' => $field['field_name'],
  '#cardinality' => $field['cardinality'],
  '#title' => $title,
  '#required' => $instance['required'],
  '#description' => $description,
  '#prefix' => '<div id="' . $wrapper_id . '">',
  '#suffix' => '</div>',
  '#max_delta' => $max,
); 

I implemented hook_form_alter(), but this form needs extra data that are passed as argument: $field, $instance, $langcode, $items, $form, and $form_state.

How do I define hook_form_alter() in this case?

1 Answer 1

1

You need to implement hook_field_widget_form_alter(), which is invoked from field_multiple_value_form() using the following code.

  $element = array(
    '#entity_type' => $instance['entity_type'], 
    '#entity' => $form['#entity'], 
    '#bundle' => $instance['bundle'], 
    '#field_name' => $field_name, 
    '#language' => $langcode, 
    '#field_parents' => $parents, 
    '#columns' => array_keys($field['columns']),
    // For multiple fields, title and description are handled by the wrapping table. 
    '#title' => $multiple ? '' : $title, 
    '#description' => $multiple ? '' : $description,
    // Only the first widget should be required. 
    '#required' => $delta == 0 && $instance['required'], 
    '#delta' => $delta, 
    '#weight' => $delta,
  );
  if ($element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element)) {
    // Input field for the delta (drag-n-drop reordering).
    if ($multiple) {
      // We name the element '_weight' to avoid clashing with elements
      // defined by widget.
      $element['_weight'] = array(
        '#type' => 'weight', 
        '#title' => t('Weight for row @number', array('@number' => $delta + 1)), 
        '#title_display' => 'invisible',
        // Note: this 'delta' is the FAPI 'weight' element's property. 
        '#delta' => $max, 
        '#default_value' => isset($items[$delta]['_weight']) ? $items[$delta]['_weight'] : $delta, 
        '#weight' => 100,
      );
    }

    // Allow modules to alter the field widget form element.
    $context = array(
      'form' => $form, 
      'field' => $field, 
      'instance' => $instance, 
      'langcode' => $langcode, 
      'items' => $items, 
      'delta' => $delta,
    );
    drupal_alter(array('field_widget_form', 'field_widget_' . $instance['widget']['type'] . '_form'), $element, $form_state, $context);

    $field_elements[$delta] = $element;
  }

For example, you could implement it using the following code.

function mymodule_field_widget_form_alter(&$element, &$form_state, $context) {
  $element['#prefix'] = $element['#suffix'] = '';
}

The arguments passed to field_multiple_value_form() are also passed to hook_field_widget_form_alter(), directly, or contained in $context.

1
  • better solution in my case imho to render field as $form['fieldname']['language'][0] then this wrappers are not appearing. thank you kiamlaluno for hook field_widget_form_alter seems it will be helpful!
    – werqious
    Jul 2, 2012 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.