pdb.module in Decoupled Blocks 8
PDB Module.
File
pdb.moduleView source
<?php
/**
 * @file
 * PDB Module.
 */
/**
 * Implements hook_library_info_build().
 */
function pdb_library_info_build() {
  $libraries = [];
  $discovery = \Drupal::service('pdb.component_discovery');
  $components = $discovery
    ->getComponents();
  foreach ($components as $component) {
    $info = $component->info;
    $path = $component
      ->getPath();
    $library_header = [];
    $library_footer = [];
    if (isset($info['add_css'])) {
      // Build the css assets, grouping them by header and footer.
      $css_assets = _pdb_library_build_css($info, $path);
      if (!empty($css_assets)) {
        if (!empty($css_assets['header'])) {
          $library_header += $css_assets['header'];
        }
        if (!empty($css_assets['footer'])) {
          $library_footer += $css_assets['footer'];
        }
      }
    }
    if (isset($info['add_js'])) {
      // Build the js assets, grouping them by header and footer.
      $js_assets = _pdb_library_build_js($info, $path);
      if (!empty($js_assets)) {
        if (!empty($js_assets['header'])) {
          $library_header += $js_assets['header'];
        }
        if (!empty($js_assets['footer'])) {
          $library_footer += $js_assets['footer'];
        }
      }
    }
    // Build a library to include assets in header.
    if (!empty($library_header)) {
      $library_header['header'] = TRUE;
      // Add dependency on presentation library.
      $pres = $info['presentation'];
      $library_header['dependencies'] = [
        'pdb_' . $pres . '/' . $pres,
      ];
      $libraries += [
        $info['machine_name'] . '/header' => $library_header,
      ];
    }
    // Build a library to include assets in footer.
    if (!empty($library_footer)) {
      // Add dependency on presentation library.
      $pres = $info['presentation'];
      $library_footer['dependencies'] = [
        'pdb_' . $pres . '/' . $pres,
      ];
      $libraries += [
        $info['machine_name'] . '/footer' => $library_footer,
      ];
    }
  }
  return $libraries;
}
/**
 * Helper function to process and build library css assets.
 */
function _pdb_library_build_css($info, $path) {
  $css_assets = [];
  if (isset($info['add_css']['header'])) {
    // Supports current simplest method to add css assets to the library.
    if (!isset($info['add_css']['header']['css'])) {
      // This assumes add_css -> header contains the assets.
      $info['add_css']['header'] = [
        'css' => $info['add_css']['header'],
      ];
    }
    foreach ($info['add_css']['header']['css'] as $group => $css) {
      $header_css = _pdb_library_build_get_assets($css, $path, $group);
      $info['add_css']['header']['css'] = $header_css;
      $css_assets['header'] = $info['add_css']['header'];
    }
  }
  if (isset($info['add_css']['footer'])) {
    if (!isset($info['add_css']['footer']['css'])) {
      // This assumes add_css -> footer contains the assets.
      $info['add_css']['footer'] = [
        'css' => $info['add_css']['footer'],
      ];
    }
    foreach ($info['add_css']['footer']['css'] as $group => $css) {
      $footer_css = _pdb_library_build_get_assets($css, $path, $group);
      $info['add_css']['footer']['css'] = $footer_css;
      $css_assets['footer'] = $info['add_css']['footer'];
    }
  }
  return $css_assets;
}
/**
 * Helper function to process and build library js assets.
 */
function _pdb_library_build_js($info, $path) {
  $js_assets = [];
  if (isset($info['add_js']['header'])) {
    // Supports current simplest method to add js assets to the library.
    if (!isset($info['add_js']['header']['js'])) {
      // This assumes add_js -> header contains the assets.
      $info['add_js']['header'] = [
        'js' => $info['add_js']['header'],
      ];
    }
    $header_js = _pdb_library_build_get_assets($info['add_js']['header']['js'], $path);
    $info['add_js']['header']['js'] = $header_js;
    $js_assets['header'] = $info['add_js']['header'];
  }
  if (!empty($info['add_js']['footer'])) {
    if (!isset($info['add_js']['footer']['js'])) {
      // This assumes add_js -> footer contains the assets.
      $info['add_js']['footer'] = [
        'js' => $info['add_js']['footer'],
      ];
    }
    $footer_js = _pdb_library_build_get_assets($info['add_js']['footer']['js'], $path);
    $info['add_js']['footer']['js'] = $footer_js;
    $js_assets['footer'] = $info['add_js']['footer'];
  }
  return $js_assets;
}
/**
 * Helper function to process and build library assets.
 */
function _pdb_library_build_get_assets($assets, $path, $group = FALSE) {
  $processed = [];
  foreach ($assets as $asset_file => $asset_data) {
    // Allow external assets to use absolute path.
    if (!empty($asset_data['type']) && $asset_data['type'] == 'external') {
      $asset_path = $asset_file;
    }
    else {
      $asset_path = '/' . $path . '/' . $asset_file;
    }
    $processed[$asset_path] = $asset_data;
  }
  // Add a group parent if there is one.
  if ($group) {
    $processed = [
      $group => $processed,
    ];
  }
  return $processed;
}Functions
| Name   | Description | 
|---|---|
| pdb_library_info_build | Implements hook_library_info_build(). | 
| _pdb_library_build_css | Helper function to process and build library css assets. | 
| _pdb_library_build_get_assets | Helper function to process and build library assets. | 
| _pdb_library_build_js | Helper function to process and build library js assets. | 
