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.

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
    Mar 14 '12 at 10:43
  • @Junaid I've just tested it, the above works perfectly outside of the form context as well :)
    – Clive
    Mar 14 '12 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
    Mar 14 '12 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
    Mar 14 '12 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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