function TypedDataResolver::convertTokenToContext
Same name in other branches
- 4.0.x src/TypedDataResolver.php \Drupal\ctools\TypedDataResolver::convertTokenToContext()
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
string $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 164
Class
- TypedDataResolver
- Typed Data Resolver Service.
Namespace
Drupal\ctoolsCode
public function convertTokenToContext(string $token, array $contexts) {
// If the requested token is already a context, just return it.
if (isset($contexts[$token])) {
return $contexts[$token];
}
else {
[
$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.");
}
}