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; }
/** * 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); }