You are here

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

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

Select one context from the list of contexts, accounting for changed IDs.

Fundamentally, this returns $contexts[$context] or FALSE if that does not exist. Additional logic accounts for changes in context names and dealing with a $contexts parameter that is not an array.

If we had requested a $context but that $context doesn't exist in our context list, there is a good chance that what happened is the context IDs changed. Look for another context that satisfies our requirements, unless $skip_name_check is set.

Parameters

ctools_context|array $contexts: A context, or an array of ctools_context.

string $context: A context ID.

Return value

bool|ctools_context The matching ctools_context, or False if no such context was found.

1 call to ctools_context_required::select()
ctools_context_optional::select in includes/context.inc
Select and return one context from the list of applicable contexts.
1 method overrides ctools_context_required::select()
ctools_context_optional::select in includes/context.inc
Select and return one context from the list of applicable contexts.

File

includes/context.inc, line 351
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 select($contexts, $context) {

  // Easier to deal with a standalone object as a 1-element array of objects.
  if (!is_array($contexts)) {
    if (is_object($contexts) && $contexts instanceof ctools_context) {
      $contexts = array(
        $contexts->id => $contexts,
      );
    }
    else {
      $contexts = array(
        $contexts,
      );
    }
  }

  // If we had requested a $context but that $context doesn't exist in our
  // context list, there is a good chance that what happened is the context
  // IDs changed. Check for another context that satisfies our requirements.
  if (!$this->skip_name_check && !empty($context) && !isset($contexts[$context])) {
    $choices = $this
      ->filter($contexts);

    // If we got a hit, take the first one that matches.
    if ($choices) {
      $keys = array_keys($choices);
      $context = reset($keys);
    }
  }
  if (empty($context) || empty($contexts[$context])) {
    return FALSE;
  }
  return $contexts[$context];
}