You are here

function datalayer_preprocess_html in dataLayer 7

Implements hook_preprocess_HOOK().

Outputs data inclusions for the page.

File

./datalayer.module, line 78
Client-side data space.

Code

function datalayer_preprocess_html(&$variables) {

  // Get active data, includes defaults.
  $output_data = datalayer_add();

  // Allow modules to alter data with hook_datalayer_alter().
  drupal_alter('datalayer', $output_data);
  $output = !empty($output_data) ? drupal_json_encode($output_data) : '';
  $render = array(
    '#type' => 'html_tag',
    '#tag' => 'script',
    '#value' => 'dataLayer = [' . $output . '];',
    '#attributes' => array(
      'type' => 'text/javascript',
    ),
  );
  drupal_add_html_head($render, 'datalayer_meta');

  // Include data-layer-helper library.
  if (variable_get('datalayer_lib_helper', FALSE)) {

    // Libraries 2.0.
    if (function_exists('libraries_load')) {
      if (($library = libraries_detect('data-layer-helper')) && !empty($library['installed'])) {
        libraries_load('data-layer-helper');
      }
      else {

        // Something went wrong.
        $error = $library['error'];
        $error_message = $library['error message'];
      }
    }
    else {

      // Libraies 1.0.
      $path = libraries_get_path('data-layer-helper');
      if (!empty($path) && file_exists($path . '/data-layer-helper.js')) {
        drupal_add_js($path . '/data-layer-helper.js');
      }
    }
  }

  // Output configred language data.
  $languages = language_list();
  if (count($languages)) {
    drupal_add_js(array(
      'dataLayer' => array(
        'languages' => $languages,
        'defaultLang' => language_default('language'),
      ),
    ), 'setting');
  }

  // Common datalayer JS.
  drupal_add_js(drupal_get_path('module', 'datalayer') . '/datalayer.js');
}