You are here

function libraries_load_files in Libraries API 7.3

Same name and namespace in other branches
  1. 8.3 libraries.module \libraries_load_files()
  2. 7.2 libraries.module \libraries_load_files()

Loads a library's files.

Parameters

$library: An array of library information as returned by libraries_info().

Return value

The number of loaded files.

1 call to libraries_load_files()
libraries_load in ./libraries.module
Loads a library.

File

./libraries.module, line 741
External library handling for Drupal modules.

Code

function libraries_load_files($library) {

  // Construct the full path to the library for later use.
  $path = $library['library path'];
  $path = $library['path'] !== '' ? $path . '/' . $library['path'] : $path;

  // Count the number of loaded files for the return value.
  $count = 0;

  // Load both the JavaScript and the CSS files.
  // The parameters for drupal_add_js() and drupal_add_css() require special
  // handling.
  // @see drupal_process_attached()
  foreach (array(
    'js',
    'css',
  ) as $type) {
    if (!empty($library['files'][$type])) {
      foreach ($library['files'][$type] as $data => $options) {

        // If the value is not an array, it's a filename and passed as first
        // (and only) argument.
        if (!is_array($options)) {
          $data = $options;
          $options = array();
        }

        // In some cases, the first parameter ($data) is an array. Arrays can't
        // be passed as keys in PHP, so we have to get $data from the value
        // array.
        if (is_numeric($data)) {
          $data = $options['data'];
          unset($options['data']);
        }

        // Prepend the library path to the file name.
        $data = "{$path}/{$data}";

        // Apply the default group if the group isn't explicitly given.
        if (!isset($options['group'])) {
          $options['group'] = $type == 'js' ? JS_DEFAULT : CSS_DEFAULT;
        }
        call_user_func('drupal_add_' . $type, $data, $options);
        $count++;
      }
    }
  }

  // Load PHP files.
  if (!empty($library['files']['php'])) {
    foreach ($library['files']['php'] as $file => $array) {
      $file_path = DRUPAL_ROOT . '/' . $path . '/' . $file;
      if (file_exists($file_path)) {
        _libraries_require_once($file_path);
        $count++;
      }
    }
  }

  // Load integration files.
  if (!empty($library['integration files'])) {
    $enabled_themes = array();
    foreach (list_themes() as $theme_name => $theme) {
      if ($theme->status) {
        $enabled_themes[] = $theme_name;
      }
    }
    foreach ($library['integration files'] as $provider => $files) {
      if (module_exists($provider)) {
        libraries_load_files(array(
          'files' => $files,
          'path' => '',
          'library path' => drupal_get_path('module', $provider),
        ));
      }
      elseif (in_array($provider, $enabled_themes)) {
        libraries_load_files(array(
          'files' => $files,
          'path' => '',
          'library path' => drupal_get_path('theme', $provider),
        ));
      }
    }
  }
  return $count;
}