barbajs-1.0.0-alpha1/barbajs.module

barbajs.module
<?php

/**
 * @file
 * Drupal's integration with Barba.js library.
 *
 * Barba.js - Create badass, fluid and smooth transitions
 * between your website’s pages.
 *
 * GitHub: https://github.com/barbajs/barba
 * Website: https://barba.js.org/
 * license: MIT licensed
 *
 * Copyright (C) 2018-2025 Luigi De Rosa
 */

use Drupal\Core\Asset\LibrariesDirectoryFileFinder;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function barbajs_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.barbajs':
      $site = 'https://barba.js.org/';
      $gh = 'https://github.com/barbajs/barba';

      $output  = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Barba JS integrates the Barba.js library to enable smooth page transitions in Drupal. It auto-attaches a local build when found, and falls back to the official CDN otherwise.') . '</p>';

      $output .= '<ul>';
      $output .= '<li>' . t('Website: @url', ['@url' => $site]) . '</li>';
      $output .= '<li>' . t('GitHub: @url', ['@url' => $gh]) . '</li>';
      $output .= '</ul>';

      $output .= '<h3>' . t('Features') . '</h3>';
      $output .= '<ul>';
      $output .= '<li>' . t('SPA-like navigation with minimal setup.') . '</li>';
      $output .= '<li>' . t('No jQuery dependency.') . '</li>';
      $output .= '<li>' . t('Optional plugins: CSS, Prefetch, Router.') . '</li>';
      $output .= '</ul>';

      $output .= '<h3>' . t('How it loads') . '</h3>';
      $output .= '<p>' . t('If a local UMD build exists under the module "dist/" or the "libraries/barba" directory, it will be used. Otherwise the jsDelivr CDN build is attached.') . '</p>';

      $output .= '<h3>' . t('Basic usage') . '</h3>';
      $output .= '<p>' . t('Add your transitions via theme or a custom module.') . '</p>';

      // Use a single literal (HEREDOC) instead of concatenation.
      $example = <<<'HTML'
                    <pre><code>barba.init({
                      transitions: [{
                        name: 'fade',
                        async leave({ current }) {
                          await gsap.to(current.container, { opacity: 0 });
                        },
                        enter({ next }) {
                          return gsap.from(next.container, { opacity: 0 });
                        }
                      }]
                    });</code></pre>
                    HTML;
      $output .= $example;

      $output .= '<p>' . t('For advanced control, enable the @ui module.', ['@ui' => 'Barba JS UI']) . '</p>';

      return $output;
  }
}

/**
 * Finds the installation path of a specified library.
 *
 * This function searches for the library in these directories:
 * - Current site: sites/site_name/libraries
 * - Root: libraries
 * - Installation profile: profiles/my_install_profile/libraries
 * The first location found will be used.
 *
 * @return string|false
 *   The real path to the library file relative to the root directory. FALSE
 *   if not found.
 */
function barbajs_find_library($library_name = 'barba') {
  // Get the current request.
  $request = \Drupal::request();

  // Determine the site path.
  if (\Drupal::hasService('kernel')) {
    $site_path = \Drupal::getContainer()->getParameter('site.path');
  }
  else {
    // If there is no kernel available yet, call the static findSitePath().
    $site_path = DrupalKernel::findSitePath($request);
  }

  // Get the application root.
  $root = DRUPAL_ROOT;

  // Get the profile extension list service.
  $profile_extension_list = \Drupal::service('extension.list.profile');

  // Get the current install profile.
  $install_profile = \Drupal::installProfile();

  // Create an instance of LibrariesDirectoryFileFinder.
  $libraries_finder = new LibrariesDirectoryFileFinder($root, $site_path, $profile_extension_list, $install_profile);

  // Find the library in the supported directories.
  return $libraries_finder->find($library_name);
}

/**
 * Check if the Barba.js library is available locally.
 *
 * The function first checks the module's bundled dist/ files and then
 * searches the /libraries locations via barbajs_find_library().
 *
 * @return bool
 *   TRUE if a local file is present; otherwise FALSE.
 */
function barbajs_check_installed() {
  // Check the bundled file first.
  $module_path = \Drupal::service('module_handler')->getModule('barbajs')->getPath();
  $barba_exist = file_exists($module_path . '/dist/core/barba.umd.min.js');

  // If Barba.js removed from module or moved to libraries.
  if (!$barba_exist) {
    // Look for a libraries-provided installation.
    $library_path = barbajs_find_library();

    // Verify if the library path exists,
    // then check if the library file is present.
    if ($library_path) {
      // Construct the base path to the Barba.js library.
      $barba_path = DRUPAL_ROOT . '/' . $library_path . '/dist/core/barba.umd.min.js';

      // If the library path is found, proceed to check for the required files.
      if (!empty($barba_path)) {
        $barba_exist = file_exists($barba_path);
      }
    }
  }

  // Return the installation status of the Barba.js library.
  return (bool) $barba_exist;
}

/**
 * Implements hook_page_attachments().
 */
function barbajs_page_attachments(array &$attachments) {
  // Do not attach libraries during the installer.
  if (InstallerKernel::installationAttempted()) {
    return;
  }

  // If the UI submodule is disabled, attach automatically.
  $module_handler = \Drupal::service('module_handler');
  if (!$module_handler->moduleExists('barbajs_ui')) {
    // Prefer local file if available; otherwise use the CDN build.
    $attachments['#attached']['library'][] = barbajs_check_installed()
      ? 'barbajs/barba.min'
      : 'barbajs/barba.cdn.min';
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc