1

I've been trying for awhile to add a custom form to a custom module so as to make it show in drupal profile Edit page. Using the normal $form['submit'] messes up the other forms in the page, so tried to use $form['#submit'][] and append the second submit handler, but still no luck. Hoping someone can see what i am doing wrong.

Here is the code i have so far:

function custom_module_form_alter(&$form, &$form_state, $form_id) {
 if ($form_id == 'user_profile_form' && user_access('delete mods', $form['#user'])) {

$modlist = getcurrentmods(); // this function returns an array of usernames

$form['modlist']['mods'] = array(
'#type'=>'select',
'#options' => $modlist,
'#multiple' => false,
'#weight' => 30,
'#size' => 10,
'#id' => 'modlist',
'#prefix' => '<div id="dropdown-modlist">',
'#suffix' => '</div>',
 );

$form['#submit'][] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#submit' => array('_modlist_delete_callback'),
'#weight' => 32,
'#attributes' => array(
    'class' => array('submit-inline'),
),
'#ajax' => array('callback' => '_modlist_delete_callback',
'wrapper' => 'dropdown-modlist',
'effect' => 'fade',
'method' => 'replace',
  ),
 );

  }
}

The dropdown with the user list shows up fine, however the submit button doesn't.

1 Answer 1

0

Anything with a # preceding it isn't an element, it's a property. Properties are not rendered.

To fix, just give your submit button a unique key without the #, e.g.

$form['foo_submit'] = array(
  '#type' => 'submit',
  ...
);
4
  • I tried changing it to $form['modlist']['submit'] , however this submits the main form and shows the message "The specified passwords do not match." from the Profile page, not part of my custom form.
    – nithintou
    Commented Jan 3, 2016 at 11:29
  • Sorry i am a bit new to drupal so i could be wrong, but isn't it possible to add a form to the profile edit page that does its own form submission unrelated to main Profile2 form?
    – nithintou
    Commented Jan 3, 2016 at 11:32
  • It's...murky :) The form isn't being 'submitted' in Drupal terms here, as in the submit handlers aren't running, but the validation handlers still are. That's where the error message comes from. You can't stop the validation handlers running (without some hackery) but you can suppress the errors by adding '#limit_validation_errors' => array() to the element. I think that'll get you what you're looking for
    – Clive
    Commented Jan 3, 2016 at 12:05
  • Ah, cool. I think that's what we need. Many thanks for the help :)
    – nithintou
    Commented Jan 3, 2016 at 15:35

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.