Okay, but I don’t know if I have to modify the select field from the core, or create a plugin file? I just understood the url path must be set like this
options: url
url: api/google-fonts.json
(and it’s working) but I don’t know how to link your script to my JSON file
Thank you ! The select field is working fine, I can select all the fonts. I could query their API but I don’t want to ask them an another API-Key again, and this file is regularly updated
By the way, the last step demands some PHP skills, and I don’t have so much : the echo from the select field is returning the font name like Abel-normal and I need to embed it without the “normal/bold/italic”
That’s what I already said above, if you want to get the weights and additional information, you have to go further down into the array. But a second select field doesn’t make that much sense as the number of available weights varies from one font to the other, if you really want to include weights, you would have to flatten the array further.
Or, you would have to query the API for each font to get the correct weights to choose from, with a conditional select.
@Oziris: hm, you wanted the variants information in your select values. Two options: change the generated array to not include the weight. Get the font only with some string extraction from the value.
Ok, but there is so much fonts that just having “regular” does not bother me too much ! and not having enough PHP skills, I would not ask you too much. Maybe an idea of interesting plugins I think!
Anyway, do you know just a simple way to remove “normal / bold or italic” and set “fontname + subname” to the variable?
I think the way i would tackle this is that not every font on google fonts is going to suit the design of the site. Why don’t you just pick 3 of 4 font pairings and weights and allow choices between those, from a select? That way you could just hard wire the appropriate links to the fonts in a snippet or something.
It depends of the template, and I think letting the user choose it could be a great stuff. And I already know how to switch between multiple entries already, so I can learn something on top of that !
Apart from choosing an appropriate font for a design, there’s also the problem that a long list of options to choose from is not really very user friendly. Without the weights, you already have a list of almost 900 options…
How about a config variable with a string of specific fonts that are sympathetic to the design, that you could feed into the code from @texnixe so you didn’t end up with 2201 entries to chose from?
Or maybe it could be really usefull for the programmer to swith quickly bewteen fonts to adapt its design instead of copy-paste the code generated by google ?
If you just want it for testing purposes, to try out different fonts, this might make sense. For a normal user I think this number of choices is overkill.
However, it is not difficult to filter the list by categories like sans-serif etc. to at least limit it some more and require that it has both normal & italic variants etc. Here an example with some conditions applied:
Or here using array_filter with even more specific requirements:
$json = f::read('https://raw.githubusercontent.com/jonathantneal/google-fonts-complete/master/google-fonts.json');
$fontArray = json_decode($json, true);
$callback = function($details, $font) {
return
// we only want sans-serif fonts
$details['category'] === 'sans-serif' &&
// and they should provide the greek and latin subsets
!array_diff(['greek', 'latin'], $details['subsets']) &&
// we want to have fonts that provide both normal and italic variants
!array_diff(['normal', 'italic'],array_keys($details['variants'])) &&
// and the normal variants should have three weights
!array_diff(['400', '500', '700'],array_keys($details['variants']['normal']));
};
$result = array_filter($fontArray, $callback, ARRAY_FILTER_USE_BOTH);
$fonts = [];
foreach($result as $font => $details) {
foreach($details['variants'] as $variant => $var) {
$fonts[$font.'-'.$variant] = $font.'-'.$variant ;
}
}
dump($fonts);
echo count($fonts); // 14