Add JavaScript file and JavaScript setting in Drupal 7 Form API by using $form['#attached']

Last updated on 07/02/2016

Here is an example which is self explanatory:

$form['events']['#attached']['js'] = [
  // Add a JS file.
  'car_visibility' => drupal_get_path('module', 'car') . '/car.admin.js',
  // Add the JS configurations accessible as Drupal.settings.car
  [
    'data' => [
      'car' => ['hasData' => $has_data],
    ],
    'type' => 'setting',
  ],
];