You are here

function pdb_library_info_build in Decoupled Blocks 8

Implements hook_library_info_build().

File

./pdb.module, line 11
PDB Module.

Code

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;
}