You are here

function pdb_vue_library_info_alter in Decoupled Blocks: Vue.js 8

Implements hook_library_info_alter().

File

./pdb_vue.module, line 29
Any procedural Vue PHP work lives here.

Code

function pdb_vue_library_info_alter(&$libraries, $extension) {
  $config = \Drupal::config('pdb_vue.settings');
  if ($extension == 'pdb') {

    /** @var \Drupal\pdb\ComponentDiscovery $discovery */
    $discovery = \Drupal::service('pdb.component_discovery');
    $components = $discovery
      ->getComponents();

    // Loop through each component to look for any added library dependencies.
    foreach ($components as $component) {
      $info = $component->info;
      if ($info['presentation'] != 'vue') {
        continue;
      }

      // Look in Vue blocks with a library added and attach it.
      if (!empty($info['libraries'])) {

        // Look into both header and footer locations.
        foreach ([
          'header',
          'footer',
        ] as $location) {

          // Get the key name of the vue component library we will alter.
          $key = $info['machine_name'] . '/' . $location;

          // If the component library is set, add in our library dependencies.
          if (array_key_exists($key, $libraries)) {
            $libraries[$key]['dependencies'] = array_merge($libraries[$key]['dependencies'], $info['libraries']);
          }
        }
      }

      // Replace Vue library if using version 3.
      if (isset($config) && $config
        ->get('version') == 'vue3') {

        // Look into both header and footer locations.
        foreach ([
          'header',
          'footer',
        ] as $location) {

          // Get the key name of the vue component library we will alter.
          $key = $info['machine_name'] . '/' . $location;

          // If the component library is set, add in our library dependencies.
          if (array_key_exists($key, $libraries)) {
            foreach ($libraries[$key]['dependencies'] as $dep_key => $deb_lib) {

              // Exit if already a vue3 library.
              if (preg_match('/^pdb_vue\\/vue3/', $deb_lib)) {
                continue;
              }
              $libraries[$key]['dependencies'][$dep_key] = preg_replace('/^pdb_vue\\/vue/', '${0}3', $deb_lib);
            }

            // Vue 3 needs the "create" library fired first to create an app.
            if (isset($config) && $config
              ->get('use_spa') && isset($info['component']) && $info['component']) {
              $libraries[$key]['dependencies'][] = "pdb_vue/vue3.spa-create";
            }
          }
        }
      }
    }

    // // Use the development version of vue.js or vuex.js if set.
    // if (isset($config) && $config->get('development_mode')) {
    //   // Look into both header and footer locations.
    //   foreach (['header', 'footer'] as $location) {
    //     // Get the key name of the vue component library we will alter.
    //     $key = $info['machine_name'] . '/' . $location;
    //     // If the component library is set, replace the libraries with the
    //     // dev version.
    //     if (array_key_exists($key, $libraries)) {
    //       foreach ($libraries[$key]['dependencies'] as $dep_key => $deb_lib) {
    //         if ($deb_lib == 'pdb_vue/vue') {
    //           $libraries[$key]['dependencies'][$dep_key] = 'pdb_vue/vue.dev';
    //         }
    //         if ($deb_lib == 'pdb_vue/vue3') {
    //           $libraries[$key]['dependencies'][$dep_key] = 'pdb_vue/vue3.dev';
    //         }
    //         if ($deb_lib == 'pdb_vue/vue.vuex') {
    //           $libraries[$key]['dependencies'][$dep_key] = 'pdb_vue/vue.vuex.dev';
    //         }
    //         if ($deb_lib == 'pdb_vue/vue3.vuex') {
    //           $libraries[$key]['dependencies'][$dep_key] = 'pdb_vue/vue3.vuex.dev';
    //         }
    //       }
    //     }
    //   }
    // }
  }

  // Only look to progressively decoupled vue libraries.
  if ($extension == 'pdb_vue') {

    // Use the development version of vue.js or vuex.js if set.
    $prod_suffix = $config
      ->get('version') == 'vue3' ? '.prod.js' : '.min.js';
    if (isset($config) && $config
      ->get('development_mode')) {
      foreach ($libraries as $name => $params) {
        $key = key($params['js']);
        if (strpos($key, $prod_suffix) !== FALSE) {
          $dev = str_replace($prod_suffix, '.js', $key);
          $libraries[$name]['js'][$dev] = $libraries[$name]['js'][$key];
          unset($libraries[$name]['js'][$key]);
        }
      }
    }
  }
}