I am trying to learn more about module development, so far I have a module that attaches Javascript to a certain content type. I am trying to have the data pull from a configuration form field.
SettingsForm.php
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form['thisData'] = [
'#type' => 'textfield',
'#title' => $this->t('Custom data'),
'#default_value' => $this->config('myModule.settings')->get('thisData'),
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
JavaScript file
(function ($, Drupal, once) {
Drupal.behaviors.myModule = {
attach: function (context, settings) {
$(".myModule", context).Widget({
text: drupalSettings.myModule.thisData,
linkText: "Click Here",
});
},
};
})(jQuery, Drupal, once);
This is probably something basic I am missing. I have tried to follow Adding assets (CSS, JS) to a Drupal module via *.libraries.yml / Attaching configurable JavaScript.
$form['#attached']['drupalSettings'], which is what the documentation says. Could that be it?$config = \Drupal::config("module.settings")was what I was looking forbuildForm()method shown in the question is still missing a line setting$form['#attached']['drupalSettings']['myModule']['thisData']or$form['#attached']['drupalSettings']['myModule']. (That is what the first comment by @monalisa said.)