Skip to content

Instantly share code, notes, and snippets.

@alex-moreno
Created January 8, 2015 15:53
Show Gist options
  • Save alex-moreno/f5054178781830f45ff9 to your computer and use it in GitHub Desktop.
Save alex-moreno/f5054178781830f45ff9 to your computer and use it in GitHub Desktop.
Creating a Drupal context programatically
<?php
/**
* Implements hook_context_plugins().
*/
function mymodule_context_plugins() {
$plugins = array();
// Pre search context.
$plugins['mymoduleContextConditionPreSearch'] = array(
'handler' => array(
// plugins folder removed as gist does not support subdirs.
'path' => drupal_get_path('module', 'mymodule') . '/plugins',
'file' => 'mymoduleContextConditionPreSearch.inc',
'class' => 'mymoduleContextConditionPreSearch',
'parent' => 'context_condition',
),
);
// Post search context.
$plugins['mymoduleContextConditionPostSearch'] = array(
'handler' => array(
// plugins folder removed as gist does not support subdirs.
'path' => drupal_get_path('module', 'mymodule') . '/plugins',
'file' => 'mymoduleContextConditionPostSearch.inc',
'class' => 'mymoduleContextConditionPostSearch',
'parent' => 'context_condition',
),
);
return $plugins;
}
<?php
/**
* @file
* B2B Track and trace context post-search conditions.
*/
// @codingStandardsIgnoreFile
/**
* Expose the values as a context condition.
*/
class mymoduleContextConditionPostSearch extends context_condition {
/**
* Implements condition_values().
*/
public function condition_values() {
$values = array(
'TRUE' => t('Active', array(), array('context' => 'mymodule')),
);
return $values;
}
/**
* Implements hook_execute().
*/
public function execute() {
// Since value = 2, we'll get relevant context(s) returned below.
$some_value = 2;
foreach ($this->get_contexts($some_value) as $context) {
$this->condition_met($context);
}
}
}
<?php
/**
* @file
* B2B Track and trace context pre-search conditions.
*/
// @codingStandardsIgnoreFile
/**
* Expose the values as a context condition.
*/
class B2bTrackandTraceContextConditionPreSearch extends context_condition {
/**
* Implements condition_values().
*/
public function condition_values() {
$values = array(
'TRUE' => t('Active', array(), array('context' => 'mymodule')),
);
return $values;
}
/**
* Implements hook_execute().
*/
public function execute() {
// Since value = 2, we'll get relevant context(s) returned below.
$some_value = 2;
foreach ($this->get_contexts($some_value) as $context) {
$this->condition_met($context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment