Hi,
I am implementing the hook_view function to display some extra fields in my custom content. One of the fields I want to add it must be a radios or checkboxes elements. I followed the examples available at the drupal doc such as http://api.drupal.org/api/drupal/developer--topics--forms_api_reference...., but I cannot manage it: the title of the field is displayed, but the actual options remain unseen.
My code:

$options = array('1' => t('Enabled'), '0' => t('Disabled'));
      $node->content['extraData'] = array(
            '#type' => 'fieldset',
            '#title' => t('Mark this article'),
            '#weight' => 1000
          );
      $node->content['extraData']['exlOptions']= array(
    		  '#type' => 'radios',
              '#title' =>t('Select one of the following'), 
              //'#default_value' =>  0,
    	      '#options' => $options,
            );  

Could someone, please tell me what I have done wrong? I have been writing and rewriting that fragment , and I cannot get it to work.
Thanks

Comments

jaypan’s picture

You need to call the form elements with drupal_get_form(). So you can do the following. First, in hook view, you use this:

$node->content['extra_data'] = drupal_get_form('extra_data_form');

Then you write the extra_data_form() function:

function extra_data_form($form_state)
{
  $options = array('1' => t('Enabled'), '0' => t('Disabled'));
  $form['extra_data'] = array(
    '#type' => 'fieldset',
    '#title' => t('Mark this article'),
    '#weight' => 1000
  );
  $form['extra_data']['exl_options']= array(
    '#type' => 'radios',
    '#title' =>t('Select one of the following'),
    //'#default_value' =>  0,
    '#options' => $options,
  );
 return $form;
}

Notice that I changed your form elements from camel case to underscores. There are times in Drupal where camel case causes problems, and while this isn't one of them, it's best to get in the habit of not using it with Drupal (except in JavaScript where it's fine).

Contact me to contract me for D7 -> D10/11 migrations.

keos’s picture

Thanks for your explanation (and for the extra camel case tip ). But I do not see how I can use drupal_get_form(), I am trying to modify the way the content type is displayed by implementing hook_view, and content is added by means of $node->content['whatever'], I do not see any forms anywhere: even if I do a source view the content of the node is displayed inside a body tag not inside of a form tag.
Could you please clarify this point?
Thanks

jaypan’s picture

drupal_get_form() creates the form.

Contact me to contract me for D7 -> D10/11 migrations.

keos’s picture

If I use drupal_get_form() I get this error:

Unsupported operand types in [..]/drupal/includes/common.inc on line 2752

The weird thing is that while this code produces the aforementioned error:

$node->content['extra_data'] = drupal_get_form('extra_data_form');

This other does not produce any error, and displays the view:

 $node->content['extra_data'] = $form['extra_data'] = array(
    '#type' => 'fieldset',
    '#title' => t('Mark this article'),
    '#weight' => 1000
  );
  $form['extra_data']['exl_options']= array(
    '#type' => 'radios',
    '#title' =>t('Select one of the following'),
    //'#default_value' =>  0,
    '#options' => $options,
  );

But still, without displaying the checkboxes, and no form is created.

So I guess I need a form to display the checkboxes, but if I pass the output of drupal_get_form('extra_data_form') to $node->content, it just goes breaks.

Please, any idea of how to fix this?

Thanks.

Note: I did a print_r($form) (the var I am returning at drupal_get_form('extra_data_form')), and checked I was not returning null, it has a valor, in the form of an array: Array ( [extra_data] => Array ( [#type] => fieldset [#title] => Mark this article [#weight] => 1000 [exl_options] => ... etc

jaypan’s picture

That just means there is an error in your form definition. How are you setting $options?

Contact me to contract me for D7 -> D10/11 migrations.

keos’s picture

Thanks, looks like the trouble was in the way I was assigning $form.
This fixed it:

$form=drupal_get_form('extra_data_form',$node);
$node->content['extra_data'] = array(
        '#value' => $form
        );