示例#1
0
 /**
  * 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.
  *
  * @param string $property_path
  *   The name of the property.
  * @param \Drupal\Core\Plugin\Context\ContextInterface $context
  *   The context from which we will extract values if available.
  *
  * @return \Drupal\Core\Plugin\Context\Context
  *   A context object that represents the definition & value of the property.
  * @throws \Exception
  */
 public function getContextFromProperty($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();
         }
     }
     $context_definition = new ContextDefinition($data_definition->getDataType(), $data_definition->getLabel(), $data_definition->isRequired(), FALSE, $data_definition->getDescription());
     return new Context($context_definition, $value);
 }
 /**
  * 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.
  *
  * @param string $property_name
  *   The name of the property.
  * @param \Drupal\Core\TypedData\ListDataDefinitionInterface $property
  *   The property object data definition.
  * @param \Drupal\Core\Plugin\Context\ContextInterface $context
  *   The context from which we will extract values if available.
  *
  * @return \Drupal\Core\Plugin\Context\Context
  *   A context object that represents the definition & value of the property.
  */
 public function getContextFromProperty($property_name, ListDataDefinitionInterface $property, ContextInterface $context)
 {
     $value = NULL;
     if ($context->hasContextValue()) {
         $value = $this->getFirstApplicableTypeResolver($property)->getValueFromProperty($context->getContextValue()->{$property_name});
     }
     $context_definition = new ContextDefinition($this->getDataTypeFromProperty($property));
     return new Context($context_definition, $value);
 }