You are here

function libraries_load_files in Libraries API 8.3

Same name and namespace in other branches
  1. 7.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.

Deprecated

Will be removed before a stable Drupal 8 release. Please use the new library load and managment concepts described at: https://www.drupal.org/node/2170763

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

File

./libraries.module, line 745
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 ([
    'js',
    'css',
  ] as $type) {

    // Given the removal of core functions like _drupal_add_js and
    // _drupal_add_css the logic below cannot safely be run anymore.
    // @see https://www.drupal.org/node/2702563
    break;
    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 = [];
        }

        // 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_AGGREGATE_DEFAULT;
        }
        if ($type === 'js') {
          $options['version'] = -1;
        }

        // @todo Avoid the usage of _drupal_add_js() and _drupal_add_css()
        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)) {
        require_once $file_path;
        $count++;
      }
    }
  }

  // Load integration files.
  if (!empty($library['integration files'])) {
    foreach ($library['integration files'] as $module => $files) {
      libraries_load_files([
        'files' => $files,
        'path' => '',
        'library path' => drupal_get_path('module', $module),
      ]);
    }
  }
  return $count;
}