social_font.module in Open Social 8.9
Same filename and directory in other branches
- 8 modules/custom/social_font/social_font.module
- 8.2 modules/custom/social_font/social_font.module
- 8.3 modules/custom/social_font/social_font.module
- 8.4 modules/custom/social_font/social_font.module
- 8.5 modules/custom/social_font/social_font.module
- 8.6 modules/custom/social_font/social_font.module
- 8.7 modules/custom/social_font/social_font.module
- 8.8 modules/custom/social_font/social_font.module
- 10.3.x modules/custom/social_font/social_font.module
- 10.0.x modules/custom/social_font/social_font.module
- 10.1.x modules/custom/social_font/social_font.module
- 10.2.x modules/custom/social_font/social_font.module
Contains social_font.module.
File
modules/custom/social_font/social_font.moduleView source
<?php
/**
* @file
* Contains social_font.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\file\Entity\File;
use Drupal\social_font\Entity\Font;
/**
* Implements hook_help().
*/
function social_font_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.social_font':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Social font module.') . '</p>';
return $output;
default:
}
}
/**
* The font renderer.
*
* @param int $font_id
* The id of the font.
*
* @return string
* Returns a string.
*/
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; } ";
// font-weight is reset because uploaded fonts don't yet support
// multiple font-weights which breaks strong tags.
// TODO: See issue #3045765.
$css .= "strong { font-weight: initial !important; }";
}
}
return $css;
}
/**
* The fallback on the font renderer.
*
* @param string $fallback_value
* The string value for the fallback.
*
* @return string
* Returns a string.
*/
function social_font_fallback_render($fallback_value) {
if ($fallback_value === '0') {
return 'serif';
}
return 'sans-serif';
}
/**
* The font file renderer.
*
* @param array $fonts
* Array with fonts.
*
* @return string
* Returns a string.
*/
function social_font_fontfiles_render(array $fonts) {
$fontstring = '';
foreach ($fonts as $key => $font) {
if (pathinfo($font, PATHINFO_EXTENSION) == 'eot') {
$fontstring .= "src: url('" . $font . "'), url('" . $font . "#iefix') format('embedded-opentype');";
// Remove EOT.
unset($fonts[$key]);
}
}
// Loop 2.
foreach ($fonts as $key => $font) {
$format = pathinfo($font, PATHINFO_EXTENSION);
if (pathinfo($font, PATHINFO_EXTENSION) == 'ttf') {
$format = 'truetype';
}
$fontstring .= "src: url('" . $font . "') format('" . $format . "');";
}
return $fontstring;
}
Functions
Name | Description |
---|---|
social_font_fallback_render | The fallback on the font renderer. |
social_font_fontfiles_render | The font file renderer. |
social_font_help | Implements hook_help(). |
social_font_render | The font renderer. |