Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Last active January 5, 2019 17:43
Show Gist options
  • Save sepiariver/198b02d44351f129721a to your computer and use it in GitHub Desktop.
Save sepiariver/198b02d44351f129721a to your computer and use it in GitHub Desktop.
Font Awesome Input Options for MODX CMS
<?php
/*
* fontAwesomeInputOptions
* MODX Snippet
* @author YJ Tso @sepiariver
* GPL, no warranties, etc.
*
* Usage: execute in TV input options, preferably with @CHUNK binding
* alternatively install as Content Blocks input (link to repo coming soon)
*/
// source file
$cssUrl = $modx->getOption('cssUrl', $scriptProperties, 'https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css');
// scan options
$regexPrefix = $modx->getOption('regexPrefix', $scriptProperties, 'fa-');
// label text output options
$mode = $modx->getOption('mode', $scriptProperties, 'tv'); // can be 'tv' or 'cb'
$titleCaseLabels = $modx->getOption('titleCaseLabels', $scriptProperties, 1);
$operator = $modx->getOption('operator', $scriptProperties, '');
if (empty($operator)) {
$operator = ($mode === 'cb') ? '=' : '==';
}
// value text output options
$outputPrefix = $modx->getOption('classPrefix', $scriptProperties, 'fa-');
// list output options
$separator = $modx->getOption('separator', $scriptProperties, '');
if (empty($separator)) {
$separator = ($mode === 'cb') ? "\n" : '||';
}
$excludeClasses = array_filter(array_map('trim', explode(',', $modx->getOption('excludeClasses', $scriptProperties, 'ul,li'))));
// check cache
$cacheKey = $modx->getOption('cacheKey', $scriptProperties, 'fontawesomecsssource');
$provider = $modx->cacheManager->getCacheProvider('default');
$css = $provider->get($cacheKey);
if (!$css) {
// get source file
$css = file_get_contents($cssUrl);
if ($css) {
$provider->set($cacheKey, $css, 0);
} else {
$modx->log(modX::LOG_LEVEL_ERROR, '[fontAwesomeInputOptions] could not get css source!');
return '';
}
}
// output
$output = array();
$regex = "/\." . $regexPrefix . "([\w-]*)/";
if (preg_match_all($regex, $css, $matches)) {
$icons = array_diff($matches[1], $excludeClasses);
foreach($icons as $icon) {
$label = ($titleCaseLabels) ? ucwords(str_replace('-', ' ', $icon)) : $icon;
$output[] = $label . $operator . $outputPrefix . $icon;
}
}
return implode($separator, $output);
@sepiariver
Copy link
Author

TODOs:

  • package for content blocks
  • package as TV input type
  • include CSS in the form render event, and show icons in dropdown

Copy link

ghost commented Sep 16, 2015

👍 show icons in dropdown!

Copy link

ghost commented Sep 16, 2015

Also allow a property for a path and only use the CDN as the default if no path is specified.

Awe, dumb. It already does! " $modx->getOption('cssUrl', $scriptProperties..."

Copy link

ghost commented Sep 16, 2015

I added a minor improvement, at line 53 I added sort($icons); to sort the array.

Copy link

ghost commented Sep 16, 2015

    $icons = array_diff($matches[1], $excludeClasses);
    sort($icons);
    foreach($icons as $icon) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment