2
function dental_form_simplenews_block_form_2_alter(&$form, &$form_state, $form_id)
{
    global $user;
    $form = array();
    $form['#attributes'] = array('class' => array('newsletter'));
    /*$form['subscribe'] = array(
        '#type' => 'fieldset', 
        '#title' => '', 
        '#weight' => 5, 
        '#collapsible' => TRUE
    );*/
    if ($user->uid) {

    }
    else {
        $form['mail'] = array(
            '#type' => 'textfield',
            '#title' => t('Email'),
            '#size' => 20,
            '#maxlength' => 128,
            '#required' => TRUE,
            '#attributes' => array('class' => array('text_ip')),
        );
        $submit_text = t('Subscribe');
        $form['action'] = array(
          '#type' => 'value',
          '#value' => 'subscribe',
          '#attributes' => array('class' => 'submit'),
        );
    }

    return $form;

}

Above is the code i am using to alter form produced by simplenews plugin. But i am getting various error messages.

Notice: Undefined index: #parents in form_builder() (line 1713 of /home/sjd/www/example.com/includes/form.inc).
Warning: implode() [function.implode]: Invalid arguments passed in form_builder() (line 1713 of /home/sjd/www/example.com/includes/form.inc).
Notice: Undefined index: #tree in form_builder() (line 1743 of /home/sjd/www/example.com/includes/form.inc).
Notice: Undefined index: #array_parents in form_builder() (line 1766 of /home/sjd/www/example.com/includes/form.inc).
Notice: Undefined index: process_input in _form_builder_handle_input_element() (line 1904 of /home/sjd/www/example.com/includes/form.inc).
Notice: Undefined index: complete form in form_builder() (line 1723 of /home/sjd/www/example.com/includes/form.inc).
Notice: Undefined index: #tree in form_builder() (line 1743 of /home/sjd/www/example.com/includes/form.inc).
Notice: Undefined index: #array_parents in form_builder() (line 1766 of /home/sjd/www/example.com/includes/form.inc).
Notice: Undefined index: process_input in _form_builder_handle_input_element() (line 1904 of /home/sjd/www/example.com/includes/form.inc).
Notice: Undefined index: process_input in drupal_process_form() (line 817 of /home/sjd/www/example.com/includes/form.inc).

Can anybody tell me where i am going wrong.

1 Answer 1

4

You shouldn't reset the form completely by doing $form = array();, since you will delete some of the stuff Drupal expects to be there.

You should instead do something like this:

function dental_form_simplenews_block_form_2_alter(&$form, &$form_state)
{
    global $user;
    // Remove what you don't want..
    unset($form['fields']);
    // Add what you do want..
    $form['#attributes'] = array('class' => array('newsletter'));
   ...
}
3
  • 2
    You actually shouldn't use unset at all. Instead, set #access to false. $form['fields']['#access'] = FALSE;. Or as Dave Reid put it: twitter.com/#!/davereid/status/76792614602547202
    – Berdir
    Jun 23, 2011 at 21:49
  • How do i remove the labels from the form?
    – Yalamber
    Jun 24, 2011 at 7:56
  • 1
    @Berdir I've actually seen cases where setting access to false would cause error but using unset would work fine. For the most part, however, using access is the best option.
    – googletorp
    Jan 18, 2013 at 12:02

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.