0

According to the Drupal website (Drupal website), 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 put this code into a function (in a bespoke module - in a class), placed in the Controller folder:

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;
}

I initially get an error stating:

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

and

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

But, even adding in those tags as follows doesn't produce radio buttons:

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 will not render as required?

Has anyone generated any code that would generate a radio button group?

Is the issue that the radio component will only generate if placed in a form class - housed in the Form folder? Rather than in a Controller folder.

Thanks.

  • 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 '16 at 18:46
4

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

| improve this answer | |
  • 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 '16 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 '16 at 19:09
  • Ah, right. It's a form render element, not a regular render element. – oknate Nov 4 '16 at 19:13

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.