You are here

function ctools_context_required::filter in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 includes/context.inc \ctools_context_required::filter()
1 call to ctools_context_required::filter()
ctools_context_optional::filter in includes/context.inc
1 method overrides ctools_context_required::filter()
ctools_context_optional::filter in includes/context.inc

File

includes/context.inc, line 131
Contains code related to the ctools system of 'context'.

Class

ctools_context_required
Used to create a method of comparing if a list of contexts match a required context type.

Code

function filter($contexts) {
  $result = array();

  // See which of these contexts are valid
  foreach ((array) $contexts as $cid => $context) {
    if ($context
      ->is_type($this->keywords)) {

      // Compare to see if our contexts were met.
      if (!empty($this->restrictions) && !empty($context->restrictions)) {
        foreach ($this->restrictions as $key => $values) {

          // If we have a restriction, the context must either not have that
          // restriction listed, which means we simply don't know what it is,
          // or there must be an intersection of the restricted values on
          // both sides.
          if (!is_array($values)) {
            $values = array(
              $values,
            );
          }
          if (!empty($context->restrictions[$key]) && !array_intersect($values, $context->restrictions[$key])) {
            continue 2;
          }
        }
      }
      $result[$cid] = $context;
    }
  }
  return $result;
}