You are here

function local_fonts_save_and_generate_css in @font-your-face 8.3

Saves and generates font file based on font config entity data.

Parameters

Drupal\local_fonts\Entity\LocalFontConfigEntity $font_entity: Custom config font entity.

Return value

bool TRUE if files save successfully. Throw any errors otherwise.

1 call to local_fonts_save_and_generate_css()
local_fonts_entity_presave in modules/local_fonts/local_fonts.module
Implements hook_entity_presave().

File

modules/local_fonts/local_fonts.module, line 101
Local Fonts module file.

Code

function local_fonts_save_and_generate_css(LocalFontConfigEntity $font_entity) {
  $directory = file_build_uri('fontyourface/local_fonts/' . $font_entity
    ->id());
  $css_file = $directory . '/font.css';
  $font_file = $directory . '/font.woff';
  \Drupal::service('file_system')
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  $css_file_data = "@font-face {\n";
  $css_file_data .= "font-family: '" . $font_entity->font_family . "';\n";
  $css_file_data .= "font-weight: " . $font_entity->font_weight . ";\n";
  $css_file_data .= "font-style: " . $font_entity->font_style . ";\n";
  $css_file_data .= "src: url('font.woff') format('woff');\n";
  $css_file_data .= "font-display: auto;\n";
  $css_file_data .= "}\n";
  \Drupal::service('file_system')
    ->saveData(base64_decode($font_entity
    ->getFontWoffData()), $font_file, FileSystemInterface::EXISTS_REPLACE);
  \Drupal::service('file_system')
    ->saveData($css_file_data, $css_file, FileSystemInterface::EXISTS_REPLACE);
  return TRUE;
}