You are here

public function TypedDataResolver::convertTokenToContext in Chaos Tool Suite (ctools) 8.3

Extracts a context from an array of contexts by a tokenized pattern.

This is more than simple isset/empty checks on the contexts array. The pattern could be node:uid:name which will iterate over all provided contexts in the array for one named 'node', it will then load the data definition of 'node' and check for a property named 'uid'. This will then set a new (temporary) context on the array and recursively call itself to navigate through related properties all the way down until the request property is located. At that point the property is passed to a TypedDataResolver which will convert it to an appropriate ContextInterface object.

Parameters

$token: A ":" delimited set of tokens representing

\Drupal\Core\Plugin\Context\ContextInterface[] $contexts: The array of available contexts.

Return value

\Drupal\Core\Plugin\Context\ContextInterface The requested token as a full Context object.

Throws

\Drupal\ctools\ContextNotFoundException

File

src/TypedDataResolver.php, line 160

Class

TypedDataResolver

Namespace

Drupal\ctools

Code

public function convertTokenToContext($token, $contexts) {

  // If the requested token is already a context, just return it.
  if (isset($contexts[$token])) {
    return $contexts[$token];
  }
  else {
    list($base, $property_path) = explode(':', $token, 2);

    // A base must always be set. This method recursively calls itself
    // setting bases for this reason.
    if (!empty($contexts[$base])) {
      return $this
        ->getContextFromProperty($property_path, $contexts[$base]);
    }

    // @todo improve this exception message.
    throw new ContextNotFoundException("The requested context was not found in the supplied array of contexts.");
  }
}