0

I have a drupal 8 form as follows:

   $form['extra_fields']['email'] = [
        '#type' => 'checkbox',
        '#options' => ['email' => 'email'],
        '#title' => $this->t('Email')
    ];

   $form['email']['required'] = array(
        '#type' => 'radios',
        '#default_value' => 0,
        '#options' => array(0 => $this->t('Required'), 1 => $this->t('Not Required')),
    );

which shows up as:

indent

I would like to have the radios a bit indented. Is it possible to achieve this? Also, how to make the radio buttons inline?

Thanks.

closed as off-topic by Kevin, Pierre.Vriens, mradcliffe, Clive Sep 30 '17 at 9:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – Kevin, Pierre.Vriens, mradcliffe, Clive
If this question can be reworded to fit the rules in the help center, please edit the question.

  • To make radio buttons inline, use the following in the element definition: '#attributes' => ['class' => ['container-inline']],. To have then indented you need to define another class and provide the required CSS yourself. – Neograph734 Sep 30 '17 at 7:28
3

Assuming you built this form programmatically (custom module):

Wrap your radios field inside a class using #prefix and #suffix

   $form['email']['required'] = array(
        '#type' => 'radios',
        '#default_value' => 0,
        '#options' => array(0 => $this->t('Required'), 1 => $this->t('Not Required')),
        '#prefix' => '<div class="indent">',
        '#suffix' => '</div>',
    );

Use CSS to alter the look.

Create a CSS file.

my_module/css/myform.css

a bit indented

.indent {
  margin-left: 10px;
}

make the radio buttons inline?

.indent .js-form-type-radio {
  display: inline-block;
}

Create a library

my_module/my_module.libraries.yml

form-stuff:
  version: 1.x
  css:
    theme:
      css/myform.css: {}

Attach library to your form so it loads your CSS

$form['#attached']['library'][] = 'my_module/form-stuff';

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