Skip to content

Instantly share code, notes, and snippets.

@airways
Last active July 30, 2018 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airways/1dfcb8c4f617f7fb2285ada8bd269943 to your computer and use it in GitHub Desktop.
Save airways/1dfcb8c4f617f7fb2285ada8bd269943 to your computer and use it in GitHub Desktop.
Custom hook to allow plugins for Craft CMS to inject custom "file kinds" into IOHelper. Patch is at very end of snipped IOHelper.php. Example plugin hook at end of YourPlugin.php
<?php
namespace Craft;
/**
* Class IOHelper
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @copyright Copyright (c) 2014, Pixel & Tonic, Inc.
* @license http://craftcms.com/license Craft License Agreement
* @see http://craftcms.com
* @package craft.app.helpers
* @since 1.0
*/
class IOHelper
{
... snip ...
/**
* Builds the internal file kinds array, if it hasn't been built already.
*
* @return void
*/
private static function _buildFileKinds()
{
if (!isset(self::$_fileKinds))
{
self::$_fileKinds = array(
'access' => array('label' => Craft::t('Access'), 'extensions' => array('adp','accdb','mdb','accde','accdt','accdr')),
'audio' => array('label' => Craft::t('Audio'), 'extensions' => array('3gp','aac','act','aif','aiff','aifc','alac','amr','au','dct','dss','dvf','flac','gsm','iklax','ivs','m4a','m4p','mmf','mp3','mpc','msv','oga','ogg','opus','ra','tta','vox','wav','wma','wv')),
'compressed' => array('label' => Craft::t('Compressed'), 'extensions' => array('bz2', 'tar', 'gz', '7z', 's7z', 'dmg', 'rar', 'zip', 'tgz', 'zipx')),
'excel' => array('label' => Craft::t('Excel'), 'extensions' => array('xls', 'xlsx','xlsm','xltx','xltm')),
'flash' => array('label' => Craft::t('Flash'), 'extensions' => array('fla','flv','swf','swt','swc')),
'html' => array('label' => Craft::t('HTML'), 'extensions' => array('html','htm')),
'illustrator' => array('label' => Craft::t('Illustrator'), 'extensions' => array('ai')),
'image' => array('label' => Craft::t('Image'), 'extensions' => array('jfif','jp2','jpx','jpg','jpeg','jpe','tiff','tif','png','gif','bmp','webp','ppm','pgm','pnm','pfm','pam','svg')),
'javascript' => array('label' => Craft::t('Javascript'), 'extensions' => array('js')),
'json' => array('label' => Craft::t('JSON'), 'extensions' => array('json')),
'pdf' => array('label' => Craft::t('PDF'), 'extensions' => array('pdf')),
'photoshop' => array('label' => Craft::t('Photoshop'), 'extensions' => array('psd','psb')),
'php' => array('label' => Craft::t('PHP'), 'extensions' => array('php')),
'powerpoint' => array('label' => Craft::t('PowerPoint'), 'extensions' => array('pps','ppsm','ppsx','ppt','pptm','pptx','potx')),
'text' => array('label' => Craft::t('Text'), 'extensions' => array('txt','text')),
'video' => array('label' => Craft::t('Video'), 'extensions' => array('avchd','asf','asx','avi','flv','fla','mov','m4v','mng','mpeg','mpg','m1s','mp2v','m2v','m2s','mp4','mkv','qt','flv','mp4','ogg','ogv','rm','wmv','webm','vob')),
'word' => array('label' => Craft::t('Word'), 'extensions' => array('doc','docx','dot','docm','dotm')),
'xml' => array('label' => Craft::t('XML'), 'extensions' => array('xml')),
);
// 2016-12-19 -- IJR -- Added hook to allow for custom file kinds
// Merge in any custom file kinds from plugins
$customKinds = craft()->plugins->call('registerFileKinds');
foreach($customKinds as $plugin => $kinds)
{
self::$_fileKinds = array_merge(self::$_fileKinds, $kinds);
}
// Sort combined array by label values
uasort(self::$_fileKinds, function($a, $b) {
return strcmp($a['label'], $b['label']);
});
// END 2016-12-19 -- IJR
}
}
}
<?php
namespace Craft;
class YourPlugin extends BasePlugin
{
public function init()
{
}
public function getName()
{
return 'Your Plugin';
}
public function getVersion()
{
return '1.0';
}
public function getDeveloper()
{
return 'RawaySmith';
}
public function getDeveloperUrl()
{
return 'http://rawaysmith.com';
}
/**
* Custom hook added to allow us to inject a file kind into IOHelper.
*/
public function registerFileKinds()
{
return array(
'csv' => array('label' => Craft::t('CSV'), 'extensions' => array('csv'))
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment