I couldn't find any documentation or examples about this. The implementation is very problematic. Do you a means to do this?
Problems
While trying to create a system settings form
with text_format
input, I get following errors while displaying the form. The form is being submitted normally and the values are being saved but when the page refreshes the text editor is not populated with the saved values and the errors are always there.
User error: "value" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
User error: "format" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
The code in buildForm()
is as follows:
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('commerce_distance_terms.distanceterms');
$form['title'] = [
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#description' => $this->t('Title for the terms'),
'#maxlength' => 64,
'#size' => 64,
'#default_value' => $config->get('title'),
];
$form['distance_selling_terms'] = [
'#type' => 'text_format',
'#title' => $this->t('Distance Selling Terms'),
'#description' => $this->t('Enter Distance Selling terms'),
'#default_value' => [
'value' => $config->get('distance_selling_terms.value'),
'format' => $config->get('distance_selling_terms.format'),
// '#tree' => FALSE,
'#printed' => TRUE,
],
];
return parent::buildForm($form, $form_state);
}
Background information
- This is the error as it links from the code.
- The errors are thrown because of the
#default_value
part of the array. Adding a#value => array()
reduces the errors. But nothing changes. - What I got from the stacktrace is that the form is being rendered, then the textarea is being rendered, and then the source of the error is being rendered (There are small elements inbetween). So I think the source of the error is the
#default_value
element of thetextarea
which is copied and pasted from the above array part while the compound text editor is being created. - In one of the rendering iterations, the
Element::children()
thinks that it should render thevalue
and theformat
elements of the above array. Since they don't start with#
. - There is a word here that system settings form doesn't support
text_area
input from Drupal 7. Also that and some linked issues tell that there are some automatic defaults for system_settings_form but I couldn't find the equal. - The
text_format
input is a compound element that includes atextarea
(to hold and populate the actual value), a dropdown format element to select from, etc. Thetextarea
element comes with the exact#default_value
that I put above (as an array). I tried topreprocess
thistextarea
and change that value to the related string to populate the field but whatever I dotextarea
comes always empty. - The
#printed
and#tree
elements of the array don't change anything. I also put them inside thetextarea
array in thepreprocess
function but nothing changed. - The textarea's
data-editor-value-original
property comes with the above array flattened even tough I change the#default_value
and#value
in the preprocess. - The text editor not being able to be populated may not be related to above errors. I know nothing.