You are here

function social_font_render in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/custom/social_font/social_font.module \social_font_render()
  2. 8.2 modules/custom/social_font/social_font.module \social_font_render()
  3. 8.3 modules/custom/social_font/social_font.module \social_font_render()
  4. 8.4 modules/custom/social_font/social_font.module \social_font_render()
  5. 8.5 modules/custom/social_font/social_font.module \social_font_render()
  6. 8.6 modules/custom/social_font/social_font.module \social_font_render()
  7. 8.7 modules/custom/social_font/social_font.module \social_font_render()
  8. 8.8 modules/custom/social_font/social_font.module \social_font_render()
  9. 10.3.x modules/custom/social_font/social_font.module \social_font_render()
  10. 10.0.x modules/custom/social_font/social_font.module \social_font_render()
  11. 10.1.x modules/custom/social_font/social_font.module \social_font_render()
  12. 10.2.x modules/custom/social_font/social_font.module \social_font_render()

The font renderer.

Parameters

int $font_id: The id of the font.

Return value

string Returns a string.

1 call to social_font_render()
improved_theme_settings_page_attachments in modules/custom/improved_theme_settings/improved_theme_settings.module
Implements hook_page_attachments().

File

modules/custom/social_font/social_font.module, line 38
Contains social_font.module.

Code

function social_font_render($font_id = NULL) {
  if ($font_id == NULL) {
    $system_theme_settings = \Drupal::configFactory()
      ->get('system.theme')
      ->get('default');
    if (array_key_exists('socialbase', \Drupal::service('theme.manager')
      ->getActiveTheme()
      ->getBaseThemes())) {
      $config = \Drupal::config($system_theme_settings . '.settings');
      $font_id = $config
        ->get('font_primary');
    }
  }
  $css = '';

  // Use empty() instead of a NULL comparison to guard for malformed
  // configuration.
  if (empty($font_id)) {
    return $css;
  }
  $font = Font::load($font_id);
  if ($font instanceof Font) {

    // Fontname.
    $fontname = $font
      ->getName();

    // Fallback.
    $fallback = social_font_fallback_render($font
      ->get('field_fallback')
      ->getString());

    // Defaults.
    $fonts = [];

    // Amount of files 0.
    if (count($font
      ->get('field_fonts')) === 0) {
      $css = "@font-face { font-family: '" . $fontname . "'; } body { font-family: '" . $fontname . "', " . $fallback . " !important; }";
    }
    else {
      $css = "@font-face { font-family: '" . $fontname . "'; ";

      // Loop through the font files.
      foreach ($font
        ->get('field_fonts')
        ->getValue() as $files) {

        /** @var \Drupal\file\Entity\File $file */
        if ($file = File::load($files['target_id'])) {
          $url = file_create_url($file
            ->get('uri')->value);
          $fonts[] = $url;
        }
      }
      $css .= social_font_fontfiles_render($fonts);
      $css .= "} body { font-family: '" . $fontname . "', " . $fallback . " !important; }";
    }
  }
  return $css;
}