You are here

public function VueBlock::attachLibraries in Decoupled Blocks: Vue.js 8

Attaches any libraries required by the component.

Parameters

array $component: The component definition.

Return value

array Array of attachments.

Overrides PdbBlock::attachLibraries

File

src/Plugin/Block/VueBlock.php, line 110

Class

VueBlock
Exposes a Vue component as a block.

Namespace

Drupal\pdb_vue\Plugin\Block

Code

public function attachLibraries(array $component) {
  $config = $this->configFactory
    ->get('pdb_vue.settings');
  $version = $config
    ->get('version') == 'vue3' ? 'vue3' : 'vue';

  // Get inline assets.
  $libraries = parent::attachLibraries($component);

  // Add existing libraries which have already been registered with Drupal.
  if (isset($component['libraries'])) {

    // Add the main Vue library if there are no inline libraries. Normally an
    // inline library will already have Vue as a dependency.
    if (empty($libraries)) {
      $libraries[] = "pdb_vue/{$version}";
    }

    // Add libraries attached in the block's .info file. An inline script will
    // automatically have these libraries as dependencies.
    // @see pdb_vue_library_info_alter().
    $libraries = array_merge($libraries, $component['libraries']);
  }
  if (isset($config) && $config
    ->get('use_spa') && isset($component['component']) && $component['component']) {
    $libraries[] = "pdb_vue/{$version}.spa-init";
  }
  $library_attachments = [
    'library' => $libraries,
  ];
  return $library_attachments;
}