You are here

function javascript_libraries_default_form in JavaScript Libraries Manager 7

Form builder function for page callback.

1 string reference to 'javascript_libraries_default_form'
javascript_libraries_menu in ./javascript_libraries.module
Implements hook_menu().

File

./javascript_libraries.admin.inc, line 11
Administrative management forms for JavaScript libraries.

Code

function javascript_libraries_default_form($form, &$form_state) {

  // Add a class and attach CSS to the form for styling.
  $path = drupal_get_path('module', 'javascript_libraries');
  $form = array(
    '#attached' => array(
      'css' => array(
        $path . '/css/javascript_libraries.admin.css',
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'javascript-libraries',
      ),
    ),
  );
  $libraries = array();

  // Display the libraries in fieldset groups. The groups will include any
  // library whose title matches one of the terms in the array associated with
  // each group.
  $groups = array(
    'jQuery' => array(
      'library' => array(
        'jquery',
        'jquery.once',
      ),
      'collapsed' => FALSE,
    ),
    'jQuery UI' => array(
      'library' => array(
        'ui',
        'ui.accordion',
        'ui.autocomplete',
        'ui.button',
        'ui.datepicker',
        'ui.dialog',
        'ui.slider',
        'ui.tabs',
        'effects.comprehensive',
      ),
      'collapsed' => FALSE,
    ),
  );

  // Allow other modules to add or remove available libraries.
  drupal_alter('javascript_libraries_available', $groups);
  foreach ($groups as $group => $value) {
    $form['libraries'][$group] = array();
  }

  // Get the current enabled states for the libraries
  $library_states = variable_get('javascript_libraries_drupal_libraries', array());

  // Assemble a list of all the libraries implemented by system or other modules.
  foreach (module_implements('library') as $module) {

    // drupal_get_library returns an associative array of all the libraries that
    // a module implements.
    foreach (drupal_get_library($module) as $name => $library) {

      // Only add the allowed libraries based on keys.
      foreach ($groups as $group => $value) {
        $idx = array_search($name, $value['library']);
        if ($idx !== FALSE) {
          $library['name'] = $name;
          $library['module'] = $module;

          // Change the dot characters in the name to hyphens.
          // @see http://ca.php.net/manual/en/language.variables.external.php#81080
          // Prepend the module name to the key to deal with libraries with the same
          // name registered by different modules.
          $key = $module . '-' . strtr($name, array(
            '.' => '-',
          ));

          // Enable the checkbox in the form for this library if it is listed
          // in the $library_states variable.
          $library['checked'] = !empty($library_states[$key]);

          // Some libraries, like jquery, are loaded on every page already, so we
          // make them seem impossible to turn off.
          if (in_array($library['name'], array(
            'jquery',
            'jquery.once',
          ))) {
            $library['checked'] = TRUE;
            $library['disabled'] = TRUE;
          }

          // Get the HTML for this library's row in the tableselect element.
          $form['libraries'][$group][$idx] = _javascript_libraries_build_default_row($key, $library);

          // End the foreach loop since we matched.
          break;
        }
      }
    }
  }

  // Create fieldsets for each group.
  foreach ($groups as $group => $value) {

    // Sory by key, so the order is the same as specified in the array above.
    ksort($form['libraries'][$group]);
    $form['libraries'][$group] += array(
      '#type' => 'fieldset',
      '#title' => t($group),
      '#collapsible' => TRUE,
      '#collapsed' => isset($value['collapsed']) ? $value['collapsed'] : TRUE,
      '#theme' => 'javascript_libraries_library_fieldset',
      '#header' => array(
        array(
          'data' => t('Enabled'),
          'class' => array(
            'checkbox',
          ),
        ),
        array(
          'data' => t('Name'),
          'class' => array(
            'name',
          ),
        ),
        array(
          'data' => t('Version'),
          'class' => array(
            'version',
          ),
        ),
      ),
    );
  }

  // The submit button.
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}