Created
May 16, 2011 13:01
-
-
Save dsnopek/974403 to your computer and use it in GitHub Desktop.
Drupal settings snippet
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 | |
* Stub code to fake drupal variable settings/getting functions | |
*/ | |
$variables; | |
function variable_get($name, $default) { | |
global $variables; | |
return isset($variables[$name]) ? $variables[$name] : $default; | |
} | |
function variable_set($name, $value) { | |
global $variables; | |
$variables[$name] = $value; | |
} |
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 | |
* | |
* For all of my modules, I create a class that represents the modules public API. | |
* | |
* Of course, I don't have to do this, this part isn't a normal Drupal convention, but its | |
* a little nicer to me as the programmer, because the classes static functions act sort of | |
* like a namespace, I get private functions and variables. Also, the class frequently doubles | |
* as an instantiable object (I'm breaking a serious OOP principle in doing that, I know -- each | |
* class should have a single responsibility -- but PHP is a pain in the ass and so cut some corners | |
* for convenience). | |
* | |
* Anyway, here I declare the concrete settings class and instantiate it. In the end, I get to | |
* do: | |
* | |
* ExampleModule::$settings->config_option | |
*/ | |
require_once('settings.inc'); | |
class ExampleModuleSettings extends LingwoSettings { | |
protected $base_name = 'example_module'; | |
/* taken from a real module: | |
* https://github.com/dsnopek/lingwo/blob/master/lingwo_entry/lingwo_entry.api.inc | |
*/ | |
private $defaults = array( | |
'lookup_path' => 'dictionary', | |
'node_title_munging' => array('language' => TRUE, 'pos' => TRUE), | |
'redirect_node' => TRUE, | |
'content_type' => 'lingwo_entry', | |
'pos_allowed_values' => "noun|Noun\nadjective|Adjective\nverb|Verb\nadverb|Adverb\nother|Other", | |
// except this one | |
'automatic' => NULL, | |
); | |
/* made up code to show how automatic generation can happen */ | |
public function getDefault($name) { | |
switch ($name) { | |
case 'automatic': | |
// the default is to depend on the value of a different variable | |
return $this->lookup_path . "/automatic"; | |
default: | |
return $this->defaults[$name]; | |
} | |
} | |
public function getNames() { | |
return array_map(array($this, 'name'), array_keys($this->defaults)); | |
} | |
public function isValid($name) { | |
return array_key_exists($name, $this->defaults); | |
} | |
// singleton boilerplate | |
private static $settings = NULL; | |
public static function get() { | |
if (is_null(self::$settings)) { | |
self::$settings = new self(); | |
} | |
return self::$settings; | |
} | |
} | |
class ExampleModule { | |
static public $settings; | |
} | |
ExampleModule::$settings = ExampleModuleSettings::get(); |
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 | |
* Helper code for managing module variables | |
* | |
* Taken directly from: | |
* | |
* https://github.com/dsnopek/lingwo/blob/master/includes/settings.inc | |
*/ | |
class LingwoSettings { | |
protected $base_name = ''; | |
function __construct() { | |
if (empty($this->base_name)) { | |
throw new Exception('You must sub-class LingwoSettings and change $base_name!'); | |
} | |
} | |
public function __get($name) { | |
$res = variable_get($this->name($name), NULL); | |
if (is_null($res)) { | |
$res = $this->getDefault($name); | |
} | |
return $res; | |
} | |
public function __set($name, $value) { | |
variable_set($this->name($name), $value); | |
} | |
public function __isset($name) { | |
return !is_null(variable_get($this->name($name), NULL)); | |
} | |
public function __unset($name) { | |
variable_del($this->name($name)); | |
} | |
public function name($name) { | |
if (!$this->isValid($name)) { | |
throw new Exception('Invalid setting name: ' . $name); | |
} | |
return $this->base_name . '_' . $name; | |
} | |
public function getDefault($name) { | |
return NULL; | |
} | |
public function isValid($name) { | |
return TRUE; | |
} | |
} |
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 | |
require_once('drupal_stubs.inc'); | |
require_once('example.api.inc'); | |
print 'lookup_path: ' . ExampleModule::$settings->lookup_path . "\n"; | |
print 'automatic: ' . ExampleModule::$settings->automatic . "\n"; | |
ExampleModule::$settings->lookup_path = 'zumma'; | |
print 'lookup_path: ' . ExampleModule::$settings->lookup_path . "\n"; | |
print 'automatic: ' . ExampleModule::$settings->automatic . "\n"; | |
ExampleModule::$settings->automatic = 'zumma/foo'; | |
print 'automatic: ' . ExampleModule::$settings->automatic . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment