Last active
April 25, 2016 19:24
-
-
Save KeyboardCowboy/6541d1278c25b63988ef4b55b060d760 to your computer and use it in GitHub Desktop.
Drupal module class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_preprocess_HOOK(). | |
*/ | |
function awesome_code_process_comment_wrapper(&$vars) { | |
$module = Module::fetch('awesome_code'); | |
// If there are comments, add some JS to spice things up. | |
if (isset($vars['content']['comments'])) { | |
$module->attachFile($vars['content']['comments'], array( | |
'drupal.alCommentsScrollToForm.js', | |
'drupal.alCommentsCollapseAll.js', | |
'drupal.alCommentsLoadMore.js', | |
'drupal.alCommentsThreadLoader.js', | |
)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Module class for the AwesomeCode module. | |
*/ | |
/** | |
* Class AwesomeCode. | |
* | |
* Container class for any custom functionality in the Awesome Code module. | |
*/ | |
class AwesomeCode extends Module { | |
// Set a default number of comments per page. | |
const DEFAULT_PER_PAGE = 25; | |
/** | |
* AwesomeCode constructor. | |
*/ | |
public function __construct() { | |
parent::__construct('awesome_code'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Object wrapper for organizing modules. | |
*/ | |
/** | |
* Class Module. | |
*/ | |
class Module { | |
// Module machine name. | |
private $modName; | |
// Path to the module. | |
public $path; | |
// Default directory registry for files. | |
private $dirs = array( | |
'admin' => 'admin', | |
'inc' => 'includes', | |
'classes' => 'classes', | |
'tpl' => 'templates', | |
'js' => 'js', | |
'css' => 'css', | |
); | |
/** | |
* Module constructor. | |
* | |
* @param string $mod_name | |
* The name of the module being instantiated. | |
*/ | |
public function __construct($mod_name) { | |
$this->modName = $mod_name; | |
$this->path = drupal_get_path('module', $mod_name); | |
// @ToDo: Add ctools etc directories to the dir registry lookup. | |
} | |
/** | |
* Fetch an module object from memory so it doesn't have to be reloaded. | |
* | |
* @param string $mod_name | |
* Module machine name. | |
* | |
* @return object | |
* A module object. | |
*/ | |
public static function fetch($mod_name) { | |
// Attempt to load an existing module object from memory. | |
$class_name = self::_modToClass($mod_name); | |
$module = &drupal_static("ModuleClass:{$class_name}"); | |
// If we don't have a module object, create one. | |
if (!$module && class_exists($class_name)) { | |
$module = new $class_name(); | |
} | |
return $module; | |
} | |
/** | |
* Get the relative path for a specific file type. | |
* | |
* @param string $type | |
* The type of subdirectory to get. | |
* @param bool $inc_module | |
* Include the module path. | |
* | |
* @return string | |
* The desired path if it exists. | |
*/ | |
public function getDir($type, $inc_module = FALSE) { | |
$path = ''; | |
if ($inc_module && !empty($this->path)) { | |
$path .= $this->path; | |
} | |
return isset($this->dirs[$type]) ? "{$path}/{$this->dirs[$type]}" : $path; | |
} | |
/** | |
* Set the relative directory for a set of files. | |
* | |
* @param string $type | |
* Type of files. | |
* @param string $val | |
* Directory name relative to the module root. | |
*/ | |
public function setDir($type, $val) { | |
$this->dirs[$type] = $val; | |
} | |
/** | |
* Get the full path to a module's file. | |
* | |
* @param string $filename | |
* A module's file name. | |
* @param string $type | |
* An optional string describing the type of file, if it should not be | |
* gleaned from the file extension. | |
* | |
* @return string | |
* The path to the file. | |
*/ | |
public function getPath($filename, $type = NULL) { | |
// If the type of file was not provided, try to get it from the file | |
// extension. | |
if (!$type && ($ext = $this->_getFileExt($filename))) { | |
$type = $ext; | |
} | |
return $this->getDir($type, TRUE) . "/{$filename}"; | |
} | |
/** | |
* Attach a file to a render array. | |
* | |
* @param array $array | |
* A render array. | |
* @param string|array $filename | |
* The name of the file to attach. | |
*/ | |
public function attachFile(array &$array, $filename) { | |
if (is_array($filename)) { | |
foreach ($filename as $_filename) { | |
$this->_attachFile($array, $_filename); | |
} | |
} | |
else { | |
$this->_attachFile($array, $filename); | |
} | |
} | |
/** | |
* Helper function to attach a file to a render array. | |
* | |
* @param array $array | |
* A render array. | |
* @param string $filename | |
* A file to attach. | |
*/ | |
private function _attachFile(array &$array, $filename) { | |
$ext = self::_getFileExt($filename); | |
$array['#attached'][$ext][] = $this->getPath($filename); | |
} | |
/** | |
* Convert a module machine name to an appropriate class name. | |
* | |
* @param string $mod_name | |
* Module machine name. | |
* | |
* @return mixed|string | |
* Equivalent class name. | |
*/ | |
private static function _modToClass($mod_name) { | |
$class_name = str_replace('_', ' ', $mod_name); | |
$class_name = ucwords($class_name); | |
$class_name = str_replace(' ', '', $class_name); | |
return $class_name; | |
} | |
/** | |
* Helper function to get a file's extension. | |
* | |
* @param string $filename | |
* A filename. | |
* | |
* @return string | |
* The file's extension. | |
*/ | |
private static function _getFileExt($filename) { | |
return substr(strrchr($filename, "."), 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment