1

I want to inject a checkbox within a preprocess function and I think it should be a renderable array.

What is the structure of a renderable array for a checkbox?

Update:

I use profile2 module and have created profile checkbox fields. While viewing profile currently the field are show as just text. I want to show it as disabled checkbox with state. So it is not within a form.

1 Answer 1

2

Assuming that your render array is part of a form (outside of which a checkbox doesn't make a whole lot of sense), you would do it like this:

$form['checkbox'] = array(
  '#type' => 'checkbox',
  '#title' => 'Title',
  '#default_value' => 1, // Checked by default
);

Have a look at the checkbox type documentation for all of the available properties.

EDIT

Just to add that render($form['checkbox']); also works perfectly outside of the form context using the above code; the correct checkbox element is rendered, but without the "name" and "id" attributes.

4
  • It is not within a form, I have edited the question. Please see the update.
    – Junaid
    Commented Mar 14, 2012 at 10:43
  • @Junaid I've just tested it, the above works perfectly outside of the form context as well :)
    – Clive
    Commented Mar 14, 2012 at 10:44
  • Thanks. It rendered :) . I have to make it as checked and disabled also, so I have '#default_value' => 1 and '#disabled' => TRUE. But not working! It shows in enabled unchecked state.
    – Junaid
    Commented Mar 14, 2012 at 11:02
  • That'll be because it's not going through the normal processing that would happen if it was part of a form :/ Off the top of my head I can't think of a way around this except to implement a theme function for that specific checkbox which checks for the #disabled flag and sets up the correct markup accordingly
    – Clive
    Commented Mar 14, 2012 at 11:08

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.