function local_fonts_add_form_submit in @font-your-face 7
Same name and namespace in other branches
- 7.2 modules/local_fonts/local_fonts.module \local_fonts_add_form_submit()
Implements hook_form_submit().
File
- modules/
local_fonts/ local_fonts.module, line 224
Code
function local_fonts_add_form_submit($form, &$form_state) {
$stream_wrappers = file_get_stream_wrappers();
if ($stream_wrappers['public']) {
$scheme = 'public';
}
$values = $form_state['values'];
$font_path = strtr(preg_replace('#[^a-zA-Z0-9]+#', ' ', $values['css_family']), ' ', '_') . '-' . $values['css_style'] . '-' . $values['css_weight'];
$font_family = $values['css_family'];
$font_style = $values['css_style'];
$font_weight = $values['css_weight'];
$local_fonts_directory = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_stream_wrapper_get_instance_by_scheme('public')
->getDirectoryPath() . '/fontyourface/local_fonts';
$destination_directory = $local_fonts_directory . '/' . $font_path;
$validators['file_validate_extensions'][] = 'eot ttf svg woff css';
$files = $_FILES['files']['name'];
$combined_sources = array();
$src = '';
if (file_prepare_directory($local_fonts_directory, FILE_CREATE_DIRECTORY) && file_prepare_directory($destination_directory, FILE_CREATE_DIRECTORY)) {
$destination_directory = $scheme . '://' . 'fontyourface/local_fonts/' . $font_path;
$destination_css_file = $destination_directory . '/stylesheet.css';
$css_path = file_stream_wrapper_get_instance_by_scheme('public')
->getDirectoryPath() . '/fontyourface/local_fonts/' . $font_path . '/stylesheet.css';
foreach ($files as $key => $filename) {
$format = $key;
// Everything but TTF matches the extension.
if ($format == 'ttf') {
$format = 'truetype';
}
// if
if (!empty($filename)) {
if ($file = file_save_upload($key, $validators, $destination_directory, FILE_EXISTS_REPLACE)) {
// Copy the temporary file to permanent location.
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);
if ($format == 'eot') {
// EOT goes on its own line and doesn't take a format.
// Because IE is a "special needs" browser.
$src .= ' src: url("' . $file->filename . '");' . "\n";
}
elseif ($format == 'svg') {
// Get the font ID from the SVG file.
$svg = file_get_contents($file->uri);
$hash = '';
if (preg_match('#<font([^>]+)>#i', $svg, $font_tag_match)) {
if (preg_match('#id="([^"]+)"#i', $font_tag_match[1], $id_match)) {
$hash = $id_match[1];
}
elseif (preg_match("#id='([^']+)'#i", $font_tag_match[1], $id_match)) {
$hash = $id_match[1];
}
// elseif
}
// if
$url = $file->filename . '#' . $hash;
$combined_sources[] = 'url("' . $url . '") format("' . $format . '")';
}
else {
$combined_sources[] = 'url("' . $file->filename . '") format("' . $format . '")';
}
// else
}
// if
}
// if
}
// foreach
if (count($combined_sources) > 0) {
// Add a fake local font name first to prevent name conflicts with local fonts.
$src .= '' . implode(', ', $combined_sources) . ';' . "\n";
}
// if
$generated_css = '@font-face {' . "\n";
$generated_css .= " font-family: '" . $font_family . "';" . "\n";
$generated_css .= $src;
$generated_css .= ' font-style: ' . $font_style . ';' . "\n";
$generated_css .= ' font-weight: ' . $font_weight . ';' . "\n";
$generated_css .= '}' . "\n";
$css_file = file_unmanaged_save_data($generated_css, $destination_css_file, FILE_EXISTS_REPLACE);
$local_font = new stdClass();
$local_font->name = $font_family . ' ' . $font_style . ' ' . $font_weight;
$local_font->url = 'http://localhost/#' . md5($local_font->name);
$local_font->css_family = "'" . $font_family . "'";
$local_font->css_style = $font_style;
$local_font->css_weight = $font_weight;
$local_font->provider = 'local_fonts';
$local_font->metadata = serialize(array(
'path' => $css_path,
));
$local_font->tags = array();
fontyourface_save_font($local_font);
drupal_set_message(t('@font added.', array(
'@font' => $font_family,
)));
}
// if
drupal_goto('admin/config/fontyourface');
}