0

I'm trying to add a field that would be displayed after the submit button in the rendered HTML in my hook_form_alter.

e.g.

$form['include_card'] = [
  '#type' => 'checkbox',
  '#title' => t('Include gift card'),
  '#weight' => 25,
];  

Weight doesn't seem to have any bearing on it. How would you set that up?

1
  • I can't remember exactly, but I believe there's a #sorted key somewhere in the $form array. You need to make sure it's set to false. If it's set to true, the form has already been sorted and it won't be re-sorted, i.e. you can add new fields or adjust existing weights and the weights will be ignored. If you set it to false, the form will be re-sorted.
    – sonfd
    Commented Jul 23, 2020 at 18:24

1 Answer 1

0

The submit button will likely be inside an Actions element, which has a default weight of 100.

You could add your field to the actions element:

$form['actions']['include_card'] = ...

At which point the weight of include_card will be relative to the buttons/etc that are inside the actions element (and 25 will probably work).

Another option would be to position your element directly after the actions:

$form['include_card'] = [
  '#type' => 'checkbox',
  '#title' => t('Include gift card'),
  '#weight' => $form['actions']['#weight'] + 1,
];  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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