Skip to content

Instantly share code, notes, and snippets.

@JayGeorge
Created March 21, 2018 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayGeorge/83b4305c3eb742b62a1f8f62326868c7 to your computer and use it in GitHub Desktop.
Save JayGeorge/83b4305c3eb742b62a1f8f62326868c7 to your computer and use it in GitHub Desktop.
/* GROUP ALT TEXT FALLBACK
=================================================== */
class PerchTemplateFilter_alt_text_fallback extends PerchTemplateFilter
{
/* Notes...
- If no alt text is filled in, use the file name as a fallback so it's not blank
- e.g. alt="<perch:if exists="alt"><perch:content type="text" id="alt"/><perch:else /><perch:content id="image" filter="convert-filename-to-alt-text"/></perch:if>"
*/
public function filterBeforeProcessing($value, $valueIsMarkup = false)
{
$value = preg_replace(
array('/-[1-9][.].*/','/[.].*/'),
array('',''),
$value
);
$value = str_replace(
array('/perch/resources/', '-'),
array('', ' '),
$value
);
return ucfirst($value);
}
}
PerchSystem::register_template_filter('alt-text-fallback', 'PerchTemplateFilter_alt_text_fallback');
@JayGeorge
Copy link
Author

Handy if you tend to use descriptive text for your file names, this saves you some work adding alternative text for all your images. Drop this in filters.php.

See Perch's Filter documentation for more information.

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