You are here

function javascript_libraries_edit_form in JavaScript Libraries Manager 7

Form builder function for page callback.

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

File

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

Code

function javascript_libraries_edit_form($form, &$form_state, $library = array()) {
  $form['#library'] = $library;
  if (!isset($form['#library']['weight'])) {
    $form['#library']['weight'] = 5;
  }
  $form['library_type'] = array(
    '#type' => 'radios',
    '#title' => t('Source'),
    '#required' => TRUE,
    '#options' => array(
      'external' => t('External URL'),
      'file' => t('File'),
    ),
    '#default_value' => isset($library['type']) ? $library['type'] : 'external',
    '#disabled' => isset($library['type']),
  );
  $external_access = empty($library['type']) || $library['type'] == 'external';
  $form['external_url'] = array(
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#description' => t('Enter the full URL of a JavaScript library. URL must start with http:// or https:// and end with .js or .txt.'),
    '#states' => array(
      'visible' => array(
        ':input[name="library_type"]' => array(
          'value' => 'external',
        ),
      ),
    ),
    '#default_value' => isset($library['uri']) ? $library['uri'] : '',
    '#access' => $external_access,
  );
  $form['cache_external'] = array(
    '#type' => 'checkbox',
    '#title' => t('Cache script locally'),
    '#description' => t('This option only takes effect if JavaScript aggregation is enabled. You must verify that the license or terms of service for the script permit local caching and aggregation.'),
    '#default_value' => isset($library['cache']) ? $library['cache'] : FALSE,
    '#access' => $external_access,
    '#states' => array(
      'visible' => array(
        ':input[name="library_type"]' => array(
          'value' => 'external',
        ),
      ),
    ),
  );
  $file_access = empty($library['type']) || $library['type'] == 'file';
  $form['js_file_upload'] = array(
    '#type' => 'managed_file',
    '#title' => t('File'),
    '#description' => t('Upload a JavaScript file from your computer. It must end in .js or .txt. It will be renamed to have a .txt extension.'),
    '#upload_location' => 'public://javascript_libraries',
    '#upload_validators' => array(
      'file_validate_extensions' => array(
        0 => 'js txt',
      ),
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="library_type"]' => array(
          'value' => 'file',
        ),
      ),
    ),
    '#default_value' => $file_access && isset($library['fid']) ? $library['fid'] : NULL,
    '#access' => empty($library['type']) || $library['type'] == 'file',
  );
  $form['scope'] = array(
    '#type' => 'select',
    '#title' => t('Region on page'),
    '#required' => TRUE,
    '#options' => array(
      'header' => t('Head'),
      'footer' => t('Footer'),
      'disabled' => '<' . t('Disabled') . '>',
    ),
    '#default_value' => isset($library['scope']) ? $library['scope'] : 'footer',
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Library description'),
    '#default_value' => isset($library['name']) ? $library['name'] : '',
    '#description' => 'Defaults to the file name or URL.',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => 'admin/config/system/javascript-libraries/custom',
  );
  return $form;
}