1

I'm modifying a form by adding another text input, and I need to add this input right before a field. How do i do it? this is my code and it's working correctly:

function provintegra_form_alter(&$form, &$form_state, $form_id) {

    $form['input_latitude'] = array(
      '#type' => 'textfield', 
      '#title' => t('Search address'), 
      '#default_value' => '',
      '#prefix' => '<div id="map">Mappa qui</div>',
    );

}

I just need to position this before $form['field_latitude'].

6

Set the weight property of your required lower than other.

$form['field_latitude']['#weight'] = 0; $form['input_latitude']['#weight'] = -10;

or function provintegra_form_alter(&$form, &$form_state, $form_id) {

$form['input_latitude'] = array(
  '#type' => 'textfield', 
  '#title' => t('Search address'), 
  '#default_value' => '',
  '#prefix' => '<div id="map">Mappa qui</div>',
  '#weight' => -10,
);

}

1

Each form element can have a weight.

Description: Used to sort the list of form elements before being output; lower numbers appear before higher numbers.

See the example on the api.drupal.org page

1

Setting the "#weight" property for the form field works, but it doesn't give you the certainty the form fields are consecutive. If you want to be sure they are consecutive, you should use code similar to the following one (I suppose the form is not already using "latitude" as array index):

function provintegra_form_alter(&$form, &$form_state, $form_id) {
    $form['latitude']['input_latitude'] = array(
      '#type' => 'textfield', 
      '#title' => t('Search address'), 
      '#default_value' => '',
      '#prefix' => '<div id="map">Mappa qui</div>',
    );

    $form['latitude']['field_latitude'] = $form['field_latitude'];

    unset($form['field_latitude']);
}

This is similarly to what done in node_content_form(), where the form field for the node body, and the fields to select the input format are placed in the same array index ($form['body_field']). The structure of the node edit form array is the following:

  $form['body_field']['teaser_js'] = array(
    '#type' => 'textarea', 
    '#rows' => 10, 
    '#teaser' => 'edit-body', 
    '#teaser_checkbox' => 'edit-teaser-include', 
    '#disabled' => TRUE,
  );

  $form['body_field']['teaser_include'] = array(
    '#type' => 'checkbox', 
    '#title' => t('Show summary in full view'), 
    '#default_value' => $include, 
    '#prefix' => '<div class="teaser-checkbox">', 
    '#suffix' => '</div>',
  );

  $form['body_field']['body'] = array(
    '#type' => 'textarea', 
    '#title' => check_plain($label), 
    '#default_value' => $include ? $node->body : ($node->teaser . $node->body), 
    '#rows' => 20, 
    '#required' => ($word_count > 0),
  );

  $form['body_field']['format'] = filter_form($node->format);

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.