function drupal_process_attached

Adds attachments to a render() structure.

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

$build['#attached'] = array(
  'library' => array(
    array(
      'taxonomy',
      'taxonomy',
    ),
  ),
  'css' => array(
    drupal_get_path('module', 'taxonomy') . '/css/taxonomy.module.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:

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

External 'js' and 'css' files can also be loaded. For example:

$build['#attached']['js'] = array(
  'http://code.jquery.com/jquery-1.4.2.min.js' => array(
    'type' => 'external',
  ),
);

Parameters

$elements: The structured array describing the data being rendered.

$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 or more dependencies are missing. Defaults to FALSE.

Return value

FALSE if there were any missing library dependencies; TRUE if all library dependencies were met.

See also

drupal_add_library()

drupal_add_js()

drupal_add_css()

drupal_render()

4 calls to drupal_process_attached()
drupal_add_library in drupal/core/includes/common.inc
Adds multiple JavaScript or CSS files at the same time.
drupal_render in drupal/core/includes/common.inc
Renders HTML given a structured array tree.
drupal_render_cache_get in drupal/core/includes/common.inc
Gets the rendered output of a renderable element from the cache.
EditController::attachments in drupal/core/modules/edit/lib/Drupal/edit/EditController.php
Returns AJAX commands to load in-place editors' attachments.

File

drupal/core/includes/common.inc, line 3448
Common functions that many Drupal modules will need to reference.

Code

function drupal_process_attached($elements, $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']);
      }
      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) {
    foreach ($elements['#attached'][$callback] as $args) {
      call_user_func_array($callback, $args);
    }
  }
  return $success;
}