1

I would like to add some checkboxes in my D7-form. For some reason, the snippet below isn't working. Any idea why or any advice how to do it properly?

$options = array('A', 'B', 'C');
foreach ($themas as $thema) {

        // Initialize array
        $ra = array();

        // Fill up the array with different keys
        $key = $prefix.'_thema_'.$thema->tid.'_fiche';
        $ra[$key]['#type'] = 'checkboxes';
        $ra[$key]['#name'] = $prefix.'_thema_'.$thema->tid.'_opties';
        $ra[$key]['#options'] = $options;
}

1 Answer 1

3

I think it's because you're re-initialising $ra in every step of the loop so it will only ever contain one set of checkboxes. Try initialising it outside of the loop:

$options = array('A', 'B', 'C');

// Initialize array
$ra = array();

foreach ($themas as $thema) {
    // Fill up the array with different keys
    $key = $prefix.'_thema_'.$thema->tid.'_fiche';
    $ra[$key]['#type'] = 'checkboxes';
    $ra[$key]['#name'] = $prefix.'_thema_'.$thema->tid.'_opties';
    $ra[$key]['#options'] = $options;
}

$form['some_key'] = $ra;

Also make sure your $prefix string doesn't start with a # symbol or Drupal will consider it a property rather than an element that needs to be rendered.

Sign up to request clarification or add additional context in comments.

1 Comment

It was indeed an initializing issue! The value #options got overwritten every time. Thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.