function TypedDataResolver::getContextFromProperty
Same name in other branches
- 4.0.x src/TypedDataResolver.php \Drupal\ctools\TypedDataResolver::getContextFromProperty()
Convert a property to a context.
This method will respect the value of contexts as well, so if a context object is pass that contains a value, the appropriate value will be extracted and injected into the resulting context object if available.
Parameters
string $property_path: The name of the property.
\Drupal\Core\Plugin\Context\ContextInterface $context: The context from which we will extract values if available.
Return value
\Drupal\Core\Plugin\Context\Context A context object that represents the definition & value of the property.
Throws
\Exception
1 call to TypedDataResolver::getContextFromProperty()
- TypedDataResolver::convertTokenToContext in src/
TypedDataResolver.php - Extracts a context from an array of contexts by a tokenized pattern.
File
-
src/
TypedDataResolver.php, line 66
Class
- TypedDataResolver
- Typed Data Resolver Service.
Namespace
Drupal\ctoolsCode
public function getContextFromProperty(string $property_path, ContextInterface $context) {
$value = NULL;
$data_definition = NULL;
if ($context->hasContextValue()) {
/** @var \Drupal\Core\TypedData\ComplexDataInterface $data */
$data = $context->getContextData();
foreach (explode(':', $property_path) as $name) {
if ($data instanceof ListInterface) {
if (!is_numeric($name)) {
// Implicitly default to delta 0 for lists when not specified.
$data = $data->first();
}
else {
// If we have a delta, fetch it and continue with the next part.
$data = $data->get($name);
continue;
}
}
// Forward to the target value if this is a data reference.
if ($data instanceof DataReferenceInterface) {
$data = $data->getTarget();
}
if (!$data->getDataDefinition()
->getPropertyDefinition($name)) {
throw new \Exception("Unknown property {$name} in property path {$property_path}");
}
$data = $data->get($name);
}
$value = $data->getValue();
$data_definition = $data instanceof DataReferenceInterface ? $data->getDataDefinition()
->getTargetDefinition() : $data->getDataDefinition();
}
else {
/** @var \Drupal\Core\TypedData\ComplexDataDefinitionInterface $data_definition */
$data_definition = $context->getContextDefinition()
->getDataDefinition();
foreach (explode(':', $property_path) as $name) {
if ($data_definition instanceof ListDataDefinitionInterface) {
$data_definition = $data_definition->getItemDefinition();
// If the delta was specified explicitly, continue with the next part.
if (is_numeric($name)) {
continue;
}
}
// Forward to the target definition if this is a data reference
// definition.
if ($data_definition instanceof DataReferenceDefinitionInterface) {
$data_definition = $data_definition->getTargetDefinition();
}
if (!$data_definition->getPropertyDefinition($name)) {
throw new \Exception("Unknown property {$name} in property path {$property_path}");
}
$data_definition = $data_definition->getPropertyDefinition($name);
}
// Forward to the target definition if this is a data reference
// definition.
if ($data_definition instanceof DataReferenceDefinitionInterface) {
$data_definition = $data_definition->getTargetDefinition();
}
}
if (strpos($data_definition->getDataType(), 'entity:') === 0) {
$context_definition = new EntityContextDefinition($data_definition->getDataType(), $data_definition->getLabel(), $data_definition->isRequired(), FALSE, $data_definition->getDescription());
}
else {
$context_definition = new ContextDefinition($data_definition->getDataType(), $data_definition->getLabel(), $data_definition->isRequired(), FALSE, $data_definition->getDescription());
}
return new Context($context_definition, $value);
}