You are here

function _async_js_process_dependencies in Asynchronous JavaScript 8

Same name and namespace in other branches
  1. 7 async_js.module \_async_js_process_dependencies()

Processes script dependencies, creating corresponding dependent references.

Parameters

array $scripts: An array of scripts as returned by async_js_add_js().

See also

async_js_js_alter()

async_js_add_js()

1 call to _async_js_process_dependencies()
async_js_js_alter in ./async_js.module
Implements hook_js_alter().

File

./async_js.module, line 181
Queue JavaScript files to be loaded asynchronously.

Code

function _async_js_process_dependencies(array $scripts = array()) {
  foreach ($scripts as $key => $script) {

    // Make sure every script has an empty async_dependents array by default.
    $scripts[$key]['async_dependents'] = array();
  }
  $scripts_copy = $scripts;

  // Create corresponding dependent references.
  foreach ($scripts_copy as $key => $script) {
    if (!empty($script['async_dependencies'])) {
      foreach ($script['async_dependencies'] as $dependency) {
        if (!empty($scripts[$dependency])) {
          $scripts[$dependency]['async_dependents'][] = $script['data'];
        }
        else {
          unset($scripts[$key]);
          drupal_set_message(t('The script, @script is dependent on @dependency, which does not exist on this page.', array(
            '@script' => $script['data'],
            '@dependency' => $dependency,
          )), 'error');
        }
      }
    }
  }
  return $scripts;
}