You are here

function fontawesome_library_info_alter in Font Awesome Icons 8.2

Same name and namespace in other branches
  1. 8 fontawesome.module \fontawesome_library_info_alter()

Implements hook_library_info_alter().

File

./fontawesome.module, line 30
Drupal integration with Font Awesome, the iconic font for use with Bootstrap.

Code

function fontawesome_library_info_alter(&$libraries, $extension) {

  // Modify the Font Awesome library to use external file if user chose.
  if ($extension == 'fontawesome') {

    // Load the configuration settings.
    $configuration_settings = \Drupal::config('fontawesome.settings');

    // Have to modify the library if the user is using a CDN.
    if ($configuration_settings
      ->get('use_cdn')) {

      // First check if we're using everything.
      if (isset($libraries['fontawesome.' . $configuration_settings
        ->get('method')])) {
        _fontawesome_modify_library($libraries, NULL, $configuration_settings
          ->get('method'), $configuration_settings
          ->get('external_svg_location'));
      }

      // Determine the base for the CDN.
      $cdnComponents = parse_url($configuration_settings
        ->get('external_svg_location'));
      $cdnComponents['path'] = explode('/', $cdnComponents['path']);
      unset($cdnComponents['path'][count($cdnComponents['path']) - 1]);
      $cdnComponents['path'] = implode('/', $cdnComponents['path']) . '/';
      if (isset($libraries['fontawesome.' . $configuration_settings
        ->get('method') . '.base'])) {

        // Modify settings for the base file.
        $cdnBase = $cdnComponents;
        $cdnBase['path'] .= 'fontawesome.' . ($configuration_settings
          ->get('method') == 'webfonts' ? 'css' : 'js');
        _fontawesome_modify_library($libraries, 'base', $configuration_settings
          ->get('method'), _fontawesome_unparse_url($cdnBase));
      }

      // Modify settings for individual included files.
      foreach ([
        'solid',
        'regular',
        'light',
        'brands',
      ] as $libraryType) {
        if (isset($libraries['fontawesome.' . $configuration_settings
          ->get('method') . '.' . $libraryType])) {
          $cdnBase = $cdnComponents;
          $cdnBase['path'] .= $libraryType . '.' . ($configuration_settings
            ->get('method') == 'webfonts' ? 'css' : 'js');
          _fontawesome_modify_library($libraries, $libraryType, $configuration_settings
            ->get('method'), _fontawesome_unparse_url($cdnBase));
        }
      }

      // Modify the shim as well.
      if (isset($libraries['fontawesome.' . $configuration_settings
        ->get('method') . '.shim'])) {
        _fontawesome_modify_library($libraries, 'shim', $configuration_settings
          ->get('method'), $configuration_settings
          ->get('external_shim_location'));
      }
    }

    // Allow pseudo-elements in JS if selected.
    if ($configuration_settings
      ->get('allow_pseudo_elements') && $configuration_settings
      ->get('method') == 'svg') {

      // Modify the libraries to add pseudo elements tag.
      foreach ($libraries as $key => &$values) {
        if (substr($key, 0, 15) == 'fontawesome.svg') {
          $librarySettings = reset($values['js']);
          $librarySource = key($values['js']);

          // Font Awesome requires this script tag to enable pseudo elements.
          $librarySettings['attributes'] = [
            'data-search-pseudo-elements' => TRUE,
          ];
          $values['js'][$librarySource] = $librarySettings;
        }
      }
    }
  }
}