You are here

function magic_assets_generate_mapping in Magic 7.2

Helper function for generating a map of assets based on the data attribute.

We can not rely on the array keys of the JS and CSS file arrays in Drupal because in case of inline JS or CSS (which uses numerical array keys) and due to potential overrides of the 'data' attribute which holds the actual, reliable path of the file. This function returns a single-level array of reliable JS/CSS file paths using the original array keys as keys. Elements of type 'inline' or 'setting' are ignored.

Parameters

$elements: An array of JS or CSS files as given in hook_css_alter() or hook_js_alter().

Return value

array A map of file paths generated from $elements.

See also

hook_js_alter()

hook_css_alter()

1 call to magic_assets_generate_mapping()
magic_assets_exclude in includes/magic.assets.inc
Eliminates elements from an array using a simplified regex pattern.

File

includes/magic.assets.inc, line 173
A file to contain functions for the magic module to abuse.

Code

function magic_assets_generate_mapping($elements) {
  $mapping = array();
  foreach ($elements as $key => $item) {
    if ($item['type'] == 'inline' || $item['type'] == 'setting') {

      // Naturally, in-line CSS is not supported.
      continue;
    }

    // We need to build an array containing just the 'data' attribute because
    // that's the actual path of the file. The array key of the elements can
    // be something else if someone is sneaky enough to use drupal_add_js() or
    // drupal_add_css() with a bogus first argument (normally, that is the
    // path to the file) and then specify the actual path through the 'data'
    // attribute in the $options array.
    $mapping[$key] = $item['data'];
  }
  return $mapping;
}