Last active
December 30, 2015 04:59
-
-
Save ewillhite/7779537 to your computer and use it in GitHub Desktop.
Context Condition (.module file)
This file contains hidden or 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 for defining custom context condition | |
*/ | |
/** | |
* Implements hook_context_plugins(). | |
*/ | |
function MYMODULE_context_plugins() { | |
$plugins = array(); | |
$plugins['MYPLUGIN'] = array( | |
'handler' => array( | |
'path' => drupal_get_path('module', 'MYMODULE'), | |
'file' => 'MYPLUGIN.inc', | |
'class' => 'MYPLUGIN', | |
'parent' => 'context_condition', | |
), | |
); | |
return $plugins; | |
} | |
/** | |
* Implements hook_context_registry(). | |
*/ | |
function MYMODULE_context_registry() { | |
return array( | |
'conditions' => array( | |
'PLUGIN' => array( | |
'title' => t('TITLE IN CONTEXT CONDITIONS ADMIN UI'), | |
'description' => t('DESCRIPTION IN CONTEXT CONDITIONS ADMIN UI'), | |
'plugin' => 'MYPLUGIN', | |
), | |
), | |
); | |
} | |
/** | |
* Implements hook_init(). | |
*/ | |
function MYMODULE_init() { | |
if ($plugin = context_get_plugin('condition', 'MYMODULE')) { | |
$plugin->execute($key); | |
} | |
} |
This file contains hidden or 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 | |
* Extend context condition | |
*/ | |
class MYPLUGIN extends context_condition { | |
/** | |
* Condition values (example from sitewide context). | |
*/ | |
function condition_values() { | |
return array(1 => t('Always active')); | |
} | |
/** | |
* Condition form (example from sitewide context). | |
*/ | |
function editor_form($context = NULL) { | |
$form = parent::editor_form($context); | |
$form[1]['#title'] = t('Always active'); | |
$form['#weight'] = -10; | |
return $form; | |
} | |
/** | |
* Condition execution. | |
*/ | |
function execute($value) { | |
foreach ($this->get_contexts($value) as $context) { | |
$this->condition_met($context, $value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment