You are here

function syntaxhighlighter_library_info_build in Syntax Highlighter 8

Implements hook_library_info_build().

NOTE: the assets could have been defined in syntaxhighlighter.library.yml as well using an absolute path, but in that case the library location would have been to be hardcoded, e.g.:

core: js: /libraries/syntaxhighlighter/scripts/shCore.js: { minified: true }

File

./syntaxhighlighter.module, line 65
Syntax highlight code using the SyntaxHighlighter Javascript library.

Code

function syntaxhighlighter_library_info_build() {

  // Make the path absolute.
  //
  // This is needed because:
  // 1. libraries_get_path('syntaxhighlighter') returns the path _relative_ to
  //    the site root path,
  // 2. however hook_library_info_build assume that relative paths for the
  //    assets are relative to the path of the module they are defined in (the
  //    current module),
  // 3. while the external library assets we are defining are NOT from the
  //    current module.
  //
  // So using an absolute path makes sure to look for the assets starting from
  // the site root path.
  $lib_location = '/' . libraries_get_path('syntaxhighlighter');
  $styles_path = $lib_location . '/styles/';
  $scripts_path = $lib_location . '/scripts/';
  $libraries = [];
  $config = \Drupal::config('syntaxhighlighter.settings');
  $theme = $config
    ->get('theme');
  $libraries['core'] = [
    'css' => [
      'theme' => [
        $styles_path . 'shCore.css' => [],
        $styles_path . $theme => [],
      ],
    ],
    'js' => [
      $scripts_path . 'shCore.js' => [
        'minified' => TRUE,
      ],
    ],
  ];
  $libraries['legacy'] = [
    'js' => [
      $scripts_path . 'shLegacy.js' => [
        'minified' => TRUE,
      ],
    ],
  ];
  $libraries['brushes'] = [
    'js' => [],
  ];
  $enabled_languages = $config
    ->get('enabled_languages');
  foreach ($enabled_languages as $lang) {
    if (!empty($lang)) {
      $libraries['brushes']['js'] += [
        $scripts_path . $lang => [],
      ];
    }
  }

  // This also needs to be an absolute path, for the reasons stated above.
  $libraries['autoloader'] = [
    'js' => [
      $scripts_path . 'shAutoloader.js' => [],
      // PublicStream::basePath() does the same thing as the old D6
      // file_directory_path() which is to give the default public 'files'
      // directory path relative the the web root.
      '/' . PublicStream::basePath() . '/js/syntaxhighlighter.autoloader.js' => [],
    ],
  ];
  return $libraries;
}