0

According to the documentation for Radios, the following code should output a group of radio buttons.

$form['settings']['active'] = array(
  '#type' => 'radios',
  '#title' => $this->t('Poll status'),
  '#default_value' => 1,
  '#options' => array(0 => $this->t('Closed'), 1 => $this->t('Active')),
);

However, when I use this code, I get an error.

public function radioComponent () {
  $form['posting_settings']['comment_preview'] = array(
    '#type' => 'radios',
    '#title' => t('Preview comment'),
    '#default_value' => '1',
    '#options' => array(t('Optional'), t('Required')),
  );

  return $form;
}

Notice: Undefined index: #title_display in Drupal\Core\Render\Element\Radios::preRenderCompositeFormElement() (line 20 of core\lib\Drupal\Core\Render\Element\CompositeFormElementTrait.php).

Notice: Undefined index: #id in Drupal\Core\Render\Element\Radios::preRenderCompositeFormElement() (line 30 of core\lib\Drupal\Core\Render\Element\CompositeFormElementTrait.php).

Even when I use the following code, the radio buttons aren't rendered.

public function radioComponent () {
  $form['posting_settings']['comment_preview'] = array(
    '#type' => 'radios',
    '#title' => t('Preview comment'),
    '#default_value' => '1',
    '#options' => array(t('Optional'), t('Required')),
    '#id' => 'al1',
    '#title_display' =>'title',
  );

  return $form;
}

Does anyone know why the radios don't render?

1
  • Just a side comment, #options should be an array with keys, [0 => 'Opt 01', 1 => 'Opt 02']. Can you provide more info? Maybe the issue its not in your radios code.
    – Vagner
    Nov 4, 2016 at 18:46

1 Answer 1

5

if you look at /core/lib/Drupal/Core/Render/Element/Radios.php you'll see it's not a render element, but a form element.

You can't use it without putting it in a form. If you search the code base for extends FormBase, you'll see many examples of forms that you can start from in building your own form, many of them quite simple.

For example: /core/modules/system/tests/modules/form_test/src/Form/FormTestProgrammaticForm.php

3
  • Thanks for taking time to respond @oknate as i came to the end of typing up the question i did wonder whether the issue was because the code was not housed in a form component ... Thanks for confirming the situation... will give it a go before confirm the situation... thanks again
    – Mo-ster
    Nov 3, 2016 at 23:48
  • @oknate, Radios IS a RenderElement object, a FormElement extends RenderElement. A Radios element to work properly needs to be inside a form (for a couple of reasons), but it will be rendered even without a form (just #options will be not rendered, but drupal will try to render because its a RenderElement). Check its namespace ...\Render\Element\... how will it not be a RenderElement?
    – Vagner
    Nov 4, 2016 at 19:09
  • 1
    Ah, right. It's a form render element, not a regular render element.
    – oknate
    Nov 4, 2016 at 19:13

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.