Automatically fill value of one field with a copy of what was just entered in another Drupal 9
- Drupal 8 copy field value to another field
https://www.drupal.org/project/safeword
- drupal behaviors attach in order
There’s several patches to do this, with various approaches (weight, specifying before / after) that nod_ in particular has put a lot of work into but none are in Drupal core yet.
So yeah we’ll just do everything we need to do in one module.
- drupal 8 drupalSettings
To add your variable to drupalSettings it’s simply (in our case for the form where we are attaching our JavaScript file to, it’s the second line that passes in a variable):
use Drupal\Component\Utility\Html;
/**
* Implements hook_form_alter().
*/
function titlecasesuggestions_form_alter(&$form, &$form_state, $form_id) {
$form['#attached']['library'][] = 'titlecasesuggestions/titlecasesuggestions';
$form['#attached']['drupalSettings']['titlecasesuggestions']['formId'] = Html::cleanCssIdentifier($form_id);
}
Response { type: “cors”, url: “https://title-case-converter.p.rapidapi.com/v1/TitleCase?tit…ghtTime%20wants%20To%20get%20UP%20NOW%20got%20it%3F&style=AP”, redirected: false, status: 200, ok: true, statusText: “OK”, headers: Headers, body: ReadableStream, bodyUsed: false } body: ReadableStream { locked: false } bodyUsed: false headers: Headers { } : HeadersPrototype { append: append(), delete: delete(), get: get(), … } ok: true redirected: false status: 200 statusText: “OK” type: “cors” url: “https://title-case-converter.p.rapidapi.com/v1/TitleCase?tit…ghtTime%20wants%20To%20get%20UP%20NOW%20got%20it%3F&style=AP” : ResponsePrototype { clone: clone(), arrayBuffer: arrayBuffer(), blob: blob(), … } titlecasesuggestions.js:20:37