drupal_process_attached

Versions
mediamosa-21
drupal_process_attached($elements, $weight = JS_DEFAULT, $dependency_check = FALSE)

Add to the page all structures attached to a render() structure.

Libraries, JavaScript, CSS and other types of custom structures are attached to elements using the #attached property. The #attached property contains an associative array, where the keys are the the types of the structure, and the value the attached data. For example:

<?php

$build['#attached'] = array(
'js' => array(drupal_get_path('module', 'taxonomy') . '/taxonomy.js'),
'css' => array(drupal_get_path('module', 'taxonomy') . '/taxonomy.css'),
);

?>

'js', 'css', and 'library' are types that get special handling. For any other kind of attached data, the array key must be the full name of the callback function and each value an array of arguments. For example:

<?php

$build['#attached']['drupal_add_http_header'] = array(
array('Content-Type', 'application/rss+xml; charset=utf-8'),
);

?>

See also

drupal_add_library().

@see drupal_add_js().

See also

drupal_add_css().

@see drupal_render().

Parameters

$elements The structured array describing the data being rendered.

$weight The default weight of JavaScript and CSS being added. This is only applied to the stylesheets and JavaScript items that don't have an explicit weight assigned to them.

$dependency_check When TRUE, will exit if a given library's dependencies are missing. When set to FALSE, will continue to add the libraries, even though one of the dependencies are missing. Defaults to FALSE.

Return value

Will return FALSE if there were any missing library dependencies. TRUE will be returned if all library dependencies were met.

▾ 3 functions call drupal_process_attached()

drupal_add_library in includes/common.inc
Adds multiple JavaScript or CSS files at the same time.
drupal_render in includes/common.inc
Renders HTML given a structured array tree.
drupal_render_cache_get in includes/common.inc
Get the rendered output of a renderable element from cache.

Code

includes/common.inc, line 3766

<?php
function drupal_process_attached($elements, $weight = JS_DEFAULT, $dependency_check = FALSE) {
  // Add defaults to the special attached structures that should be processed differently.
  $elements['#attached'] += array(
    'library' => array(),
    'js' => array(),
    'css' => array(),
  );

  // Add the libraries first.
  $success = TRUE;
  foreach ($elements['#attached']['library'] as $library) {
    if (drupal_add_library($library[0], $library[1]) === FALSE) {
      $success = FALSE;
      // Exit if the dependency is missing.
      if ($dependency_check) {
        return $success;
      }
    }
  }
  unset($elements['#attached']['library']);

  // Add both the JavaScript and the CSS.
  // The parameters for drupal_add_js() and drupal_add_css() require special
  // handling.
  foreach (array('js', 'css') as $type) {
    foreach ($elements['#attached'][$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 = NULL;
      }
      // 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']);
      }
      // Apply the default weight if the weight isn't explicitly given.
      if (!isset($options['weight'])) {
        $options['weight'] = $weight;
      }
      call_user_func('drupal_add_' . $type, $data, $options);
    }
    unset($elements['#attached'][$type]);
  }

  // Add additional types of attachments specified in the render() structure.
  // Libraries, Javascript and CSS have been added already, as they require
  // special handling.
  foreach ($elements['#attached'] as $callback => $options) {
    if (function_exists($callback)) {
      foreach ($elements['#attached'][$callback] as $args) {
        call_user_func_array($callback, $args);
      }
    }
  }

  return $success;
}
?>