Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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