You are here

function fontawesome_page_attachments in Font Awesome Icons 8.2

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

Implements hook_page_attachments().

Purposefully only load on page requests and not hook_init(). This is required so it does not increase the bootstrap time of Drupal when it isn't necessary.

File

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

Code

function fontawesome_page_attachments(array &$page) {

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

  // Don't include fontawesome if the user has opted out of loading it.
  if (!$configuration_settings
    ->get('load_assets')) {
    return TRUE;
  }

  // Throw error if library file not found.
  if (!fontawesome_check_installed()) {
    \Drupal::messenger()
      ->addWarning(t('The Font Awesome library could not be found. Please verify Font Awesome is installed correctly or that the CDN has been activated and properly configured. Please see the @adminPage and the Font Awesome module README file for more details.', [
      '@adminPage' => Link::createFromRoute(t('admin page'), 'fontawesome.admin_settings')
        ->toString(),
    ]));
    return;
  }

  // Determine which files we are using.
  $activeFiles = [
    'use_solid_file' => is_null($configuration_settings
      ->get('use_solid_file')) === TRUE ? TRUE : $configuration_settings
      ->get('use_solid_file'),
    'use_regular_file' => is_null($configuration_settings
      ->get('use_regular_file')) === TRUE ? TRUE : $configuration_settings
      ->get('use_regular_file'),
    'use_light_file' => is_null($configuration_settings
      ->get('use_light_file')) === TRUE ? TRUE : $configuration_settings
      ->get('use_light_file'),
    'use_brands_file' => is_null($configuration_settings
      ->get('use_brands_file')) === TRUE ? TRUE : $configuration_settings
      ->get('use_brands_file'),
    'use_duotone_file' => is_null($configuration_settings
      ->get('use_duotone_file')) === TRUE ? TRUE : $configuration_settings
      ->get('use_duotone_file'),
  ];

  // First check if we're using everything.
  if (count(array_unique($activeFiles)) == 1) {

    // Attach the main library.
    $page['#attached']['library'][] = 'fontawesome/fontawesome.' . $configuration_settings
      ->get('method');
  }
  else {
    if ($activeFiles['use_solid_file']) {
      $page['#attached']['library'][] = 'fontawesome/fontawesome.' . $configuration_settings
        ->get('method') . '.solid';
    }
    if ($activeFiles['use_regular_file']) {
      $page['#attached']['library'][] = 'fontawesome/fontawesome.' . $configuration_settings
        ->get('method') . '.regular';
    }
    if ($activeFiles['use_light_file']) {
      $page['#attached']['library'][] = 'fontawesome/fontawesome.' . $configuration_settings
        ->get('method') . '.light';
    }
    if ($activeFiles['use_brands_file']) {
      $page['#attached']['library'][] = 'fontawesome/fontawesome.' . $configuration_settings
        ->get('method') . '.brands';
    }
    if ($activeFiles['use_duotone_file']) {
      $page['#attached']['library'][] = 'fontawesome/fontawesome.' . $configuration_settings
        ->get('method') . '.duotone';
    }
  }

  // Attach the shim file if needed.
  if ($configuration_settings
    ->get('use_shim')) {
    $page['#attached']['library'][] = 'fontawesome/fontawesome.' . $configuration_settings
      ->get('method') . '.shim';
  }
}