How to default checkboxes to checked while maintaining custom return value I'm working on a migration project. I have a form in a custom module that creates a series of checkboxes for URLs to scrape. The form logic looks like this:

foreach ( \Drupal\scrape\Api\Scraper::loadUrls() as $index =&gt; $url ) {
  $form['url_' . $index] = [
    '#type' =&gt; 'checkbox',
    '#title' =&gt; $url . ' &amp;mdash; ' . \Drupal::l('(link)', Url::fromUri($url)),
    '#return_value' =&gt; $url,
  ];
}


This code does what I want it to do, but all the checkboxes default to 'unchecked'.

'url_0' =&gt; integer0
'url_1' =&gt; string(27) "https://myurl1.com"
'url_2' =&gt; integer0


I want these checkboxes to default to checked. I couldn't find documentation on how to do it in Drupal 8, so I tried several things, and finally hit upon '#checked' =&gt; TRUE, (or any value that evaluates truthily). Now the checkboxes are defaulting to checked.

However, even though I have #return_value specified, the value returned in the form submission is now TRUE (from kint):

'url_0' =&gt; boolTRUE
'url_1' =&gt; boolTRUE
'url_2' =&gt; boolTRUE


Since any truthy value defaults the checkbox to checked, I tried '#value' =&gt;  $url, but then upon submission, the values don't indicate their checked status:

'url_0' =&gt; string(86) "https://myurl0.com"
'url_1' =&gt; string(27) "https://myurl1.com"
'url_2' =&gt; string(29) "https://myurl2.com"


I guess my final option is to have a separate, hidden value for each URL, and map checkbox values to see if the user wants that URL included. 

Is there a way I can default my checkboxes to TRUE, while maintaining their #return_value?
Sorry for such a late response but it is possible, I ran into this same issue using 8.5.3 the below configuration worked for me.  You just have to use both together, i successfully passed numbers, text, bool (becomes 0 or 1), and arrays through #return_value.  The class was just for formatting, you can leave that out if you're not using it.

'checkbox_1' =&gt; [
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('Checkbox 1'),
    '#default_value' =&gt; 1,         // Default to checked
    '#return_value' =&gt; "checked",  // Return the value if checked
    '#attributes' =&gt; ['class'=&gt;['check_group']],
],
'checkbox_2' =&gt; [
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('Checkbox 2'),
    '#default_value' =&gt; 0,         // Default to unchecked
    '#return_value' =&gt; "checked",  // Return the value if checked
    '#attributes' =&gt; ['class'=&gt;['check_group']],
],


Output will look like this if you dpm($form_state->getValues()) in your submit or validate function.

Array
(
    [checkbox_1] =&gt; checked
    [checkbox_2] =&gt; 0
)