Beispiel #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);
 }
 protected function getData(ContextInterface $context)
 {
     /** @var \Drupal\Core\TypedData\ComplexDataInterface $base */
     $base = $context->getContextValue();
     $name = $this->getPluginDefinition()['property_name'];
     $data = $base->get($name);
     // @todo add configuration to get N instead of first.
     if ($data instanceof ListInterface) {
         $data = $data->first();
     }
     if ($data instanceof DataReferenceInterface) {
         $data = $data->getTarget();
     }
     return $data;
 }
Beispiel #3
0
 /**
  * {@inheritdoc}
  */
 public static function createFromContext(ContextInterface $old_context, $value)
 {
     $context = new static($old_context->getContextDefinition(), $value);
     $context->addCacheableDependency($old_context);
     if (method_exists($old_context, 'getTypedDataManager')) {
         $context->setTypedDataManager($old_context->getTypedDataManager());
     }
     return $context;
 }
 /**
  * Provides a label for a property by its relationship to a context.
  *
  * @param \Drupal\Core\Field\BaseFieldDefinition $property
  *   The property for which to generate a label.
  * @param \Drupal\Core\Plugin\Context\ContextInterface $context
  *   The context to which the property is related.
  * @param $context_id
  *   The context from the previous parameter's id in the contexts array.
  *
  * @return \Drupal\Core\StringTranslation\TranslatableMarkup
  *   A label for this property for use in user interfaces.
  */
 protected function getRelatedPropertyLabel(BaseFieldDefinition $property, ContextInterface $context, $context_id)
 {
     /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
     $label = $property->getFieldStorageDefinition()->getLabel();
     $string = "@context_id: {$label->getUntranslatedString()}";
     $args = $label->getArguments();
     $args['@context_id'] = !empty($context->getContextDefinition()->getLabel()) ? $context->getContextDefinition()->getLabel() : "{$context_id}";
     $options = $label->getOptions();
     // @todo we need to really think about this label. It informs the UI extensively and should be as clear as possible.
     return $this->translation->translate($string, $args, $options);
 }