You are here

public function ctools_context_required::filter in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 includes/context.inc \ctools_context_required::filter()

Filter the contexts to determine which apply in the current environment.

A context passes the filter if:

  • the context matches 'type' of the required keywords (uses ctools_context::is_type(), so includes 'any' matches, etc).
  • AND if restrictions are present, there are some common elements between the requirement and the context.

Parameters

array $contexts: An array of ctools_context objects (or something which will cast to an array of them). The contexts to apply the filter on.

Return value

array An array of context objects, keyed with the same keys used for $contexts, which pass the filter.

See also

ctools_context::is_type()

2 calls to ctools_context_required::filter()
ctools_context_optional::filter in includes/context.inc
Filter the contexts to determine which apply in the current environment.
ctools_context_required::select in includes/context.inc
Select one context from the list of contexts, accounting for changed IDs.
1 method overrides ctools_context_required::filter()
ctools_context_optional::filter in includes/context.inc
Filter the contexts to determine which apply in the current environment.

File

includes/context.inc, line 293
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

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

  /**
   * See which of these contexts are valid.
   * @var ctools_context $context
   */
  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])) {

            // Break out to check next context; this one fails the filter.
            continue 2;
          }
        }
      }

      // This context passes the filter.
      $result[$cid] = $context;
    }
  }
  return $result;
}